de: use Struct instead of Map since they are almost identical

This commit is contained in:
Matt Bilker
2018-07-19 15:00:59 +00:00
parent b4422adf97
commit 3ed8d8a9fd
3 changed files with 5 additions and 77 deletions

View File

@@ -1,67 +0,0 @@
use serde::de::{DeserializeSeed, MapAccess};
use de::{Deserializer, Result};
use error::{Error, KbinErrorKind};
use node_types::StandardType;
pub struct Map<'a, 'de: 'a> {
de: &'a mut Deserializer<'de>,
}
impl<'de, 'a> Map<'a, 'de> {
pub fn new(de: &'a mut Deserializer<'de>) -> Self {
Self { de }
}
}
// TODO: FIX THIS, it's pretty broken, but it will deserialize correctly
impl<'de, 'a> MapAccess<'de> for Map<'a, 'de> {
type Error = Error;
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>>
where K: DeserializeSeed<'de>
{
trace!("--> <Map as MapAccess>::next_key_seed()");
let (node_type, _is_array) = self.de.reader.read_node_type()?;
debug!("<Map as MapAccess>::next_key_seed() => node_type: {:?}", node_type);
if node_type == StandardType::NodeEnd ||
node_type == StandardType::FileEnd
{
trace!("<Map as MapAccess>::next_key_seed() => end of map");
return Ok(None);
}
let key = seed.deserialize(&mut *self.de).map(Some)?;
self.de.node_stack.push(node_type);
Ok(key)
}
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value>
where V: DeserializeSeed<'de>
{
debug!("--> <Map as MapAccess>::next_value_seed()");
let value = seed.deserialize(&mut *self.de)?;
let popped = self.de.node_stack.pop();
debug!("<Map as MapAccess>::next_value_seed() => popped: {:?}, node_stack: {:?}", popped, self.de.node_stack);
// Consume the NodeEnd
match popped {
Some(StandardType::Attribute) |
Some(StandardType::NodeStart) => {},
Some(_) => {
let (node_type, _is_array) = self.de.reader.read_node_type()?;
if node_type != StandardType::NodeEnd {
return Err(KbinErrorKind::TypeMismatch(*StandardType::NodeEnd, *node_type).into());
}
},
None => {},
}
Ok(value)
}
}

View File

@@ -8,11 +8,9 @@ use error::{Error, KbinErrorKind};
use node_types::StandardType;
use reader::Reader;
mod map;
mod seq;
mod structure;
use self::map::Map;
use self::seq::Seq;
use self::structure::Struct;
@@ -280,7 +278,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
let (node_type, _, name) = self.read_node_with_name()?;
debug!("Deserializer::deserialize_map() => node_type: {:?}, name: {:?}", node_type, name);
visitor.visit_map(Map::new(self))
visitor.visit_map(Struct::new(self))
}
fn deserialize_struct<V>(self, name: &'static str, fields: &'static [&'static str], visitor: V) -> Result<V::Value>
@@ -301,7 +299,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
}
self.first_struct = false;
let value = visitor.visit_map(Struct::new(self, fields))?;
let value = visitor.visit_map(Struct::new(self))?;
Ok(value)
}

View File

@@ -10,11 +10,8 @@ pub struct Struct<'a, 'de: 'a> {
}
impl<'de, 'a> Struct<'a, 'de> {
pub fn new(de: &'a mut Deserializer<'de>, _fields: &'static [&'static str]) -> Self {
Self {
de,
//fields,
}
pub fn new(de: &'a mut Deserializer<'de>) -> Self {
Self { de }
}
}
@@ -30,7 +27,7 @@ impl<'de, 'a> MapAccess<'de> for Struct<'a, 'de> {
debug!("Struct::next_key_seed() => node_type: {:?}", node_type);
if node_type == StandardType::NodeEnd {
trace!("Struct::next_key_seed() => end of map");
debug!("<-- <Struct as MapAccess>::next_key_seed() => end of map");
return Ok(None);
}