value: add a method to get the StandardType of a Value

This commit is contained in:
Matt Bilker
2018-08-17 20:15:54 -04:00
parent 284dda9606
commit ec08a408a3
2 changed files with 18 additions and 3 deletions

View File

@@ -86,7 +86,7 @@ impl<'de> Deserialize<'de> for Value {
{
trace!("ValueVisitor::visit_map()");
let mut values: IndexMap<String, Self::Value> = IndexMap::new();
let mut values: IndexMap<String, Value> = IndexMap::new();
while let Some((key, value)) = try!(map.next_entry()) {
// Check to see if this is an attribute

View File

@@ -35,6 +35,20 @@ macro_rules! construct_types {
}
)+
impl Value {
pub fn standard_type(&self) -> Option<StandardType> {
match *self {
$(
Value::$konst(_) => Some(StandardType::$konst),
)+
Value::Time(_) => Some(StandardType::Time),
Value::Attribute(_) => Some(StandardType::Attribute),
Value::Array(_) => None,
Value::Map(_) => Some(StandardType::NodeStart),
}
}
}
impl<'de> DeserializeSeed<'de> for StandardType {
type Value = Value;
@@ -44,12 +58,12 @@ macro_rules! construct_types {
trace!("<StandardType as DeserializeSeed>::deserialize(self: {:?})", self);
match self {
$(
StandardType::$konst => <$($value_type)*>::deserialize(deserializer).map(Value::from),
StandardType::$konst => <$($value_type)*>::deserialize(deserializer).map(Value::$konst),
)+
StandardType::Time => u32::deserialize(deserializer).map(Value::Time),
StandardType::Attribute => String::deserialize(deserializer).map(Value::Attribute),
StandardType::NodeStart => Value::deserialize(deserializer),
StandardType::NodeEnd => unimplemented!(),
StandardType::NodeEnd |
StandardType::FileEnd => unimplemented!(),
}
}
@@ -57,6 +71,7 @@ macro_rules! construct_types {
}
}
impl fmt::Debug for Value {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
macro_rules! field {