Implement TypedValue::is_congruent_with and ::matches_type. r=jsantell

This commit is contained in:
Richard Newman 2017-02-09 17:12:55 -08:00
parent b56b7c2a3f
commit 8e2359d3ab

View file

@ -48,6 +48,18 @@ pub enum TypedValue {
}
impl TypedValue {
/// Returns true if the provided type is `Some` and matches this value's type, or if the
/// provided type is `None`.
#[inline]
pub fn is_congruent_with<T: Into<Option<ValueType>>>(&self, t: T) -> bool {
t.into().map_or(true, |x| self.matches_type(x))
}
#[inline]
pub fn matches_type(&self, t: ValueType) -> bool {
self.value_type() == t
}
pub fn value_type(&self) -> ValueType {
match self {
&TypedValue::Ref(_) => ValueType::Ref,
@ -60,6 +72,15 @@ impl TypedValue {
}
}
#[test]
fn test_typed_value() {
assert!(TypedValue::Boolean(false).is_congruent_with(None));
assert!(TypedValue::Boolean(false).is_congruent_with(ValueType::Boolean));
assert!(!TypedValue::String("foo".to_string()).is_congruent_with(ValueType::Boolean));
assert!(TypedValue::String("foo".to_string()).is_congruent_with(ValueType::String));
assert!(TypedValue::String("foo".to_string()).is_congruent_with(None));
}
/// A Mentat schema attribute has a value type and several other flags determining how assertions
/// with the attribute are interpreted.
///