Deserialize Block using serde

This commit is contained in:
est31 2019-09-02 22:30:51 +02:00
parent 9b3a561e83
commit e8880a1a57
2 changed files with 16 additions and 13 deletions

View File

@ -1,4 +1,3 @@
use crate::object::base as value;
use crate::prelude::*; use crate::prelude::*;
use log::trace; use log::trace;
@ -106,15 +105,3 @@ impl ExtractType for String {
} }
} }
} }
impl ExtractType for value::Block {
fn extract(value: &Tagged<Value>) -> Result<value::Block, ShellError> {
match value {
Tagged {
item: Value::Block(block),
..
} => Ok(block.clone()),
other => Err(ShellError::type_error("Block", other.tagged_type_name())),
}
}
}

View File

@ -306,6 +306,22 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut ConfigDeserializer<'de> {
} }
let value = self.pop(); let value = self.pop();
if name == "Block" {
let block = match value.val {
Tagged {
item: Value::Block(block),
..
} => block,
other => return Err(ShellError::type_error("Block", other.tagged_type_name())),
};
let json = serde_json::to_string(&block)?;
let json_cursor = std::io::Cursor::new(json.into_bytes());
let mut json_de = serde_json::Deserializer::from_reader(json_cursor);
let r = json_de.deserialize_struct(name, fields, visitor)?;
return Ok(r);
}
let name = std::any::type_name::<V::Value>(); let name = std::any::type_name::<V::Value>();
trace!("Extracting {:?} for {:?}", value.val, name); trace!("Extracting {:?} for {:?}", value.val, name);
V::Value::extract(&value.val) V::Value::extract(&value.val)