props: deserialize (no nesting yet)

This commit is contained in:
Carter Anderson 2020-05-22 19:01:48 -07:00
parent 6e31b90ec3
commit 284afd4f94
3 changed files with 145 additions and 10 deletions

View File

@ -1,15 +1,14 @@
use std::{collections::HashMap, borrow::Cow};
use crate::{Properties, Property, PropertyIter};
use serde::{Deserialize, Serialize, ser::SerializeMap};
use serde::{Deserialize, Serialize, ser::SerializeMap, de::{self, MapAccess, Visitor}};
#[derive(Default)]
pub struct DynamicProperties {
pub type_name: &'static str,
pub type_name: String,
pub props: Vec<(Cow<'static, str>, Box<dyn Property>)>,
pub prop_indices: HashMap<Cow<'static, str>, usize>,
}
impl DynamicProperties {
fn push(&mut self, name: &str, prop: Box<dyn Property>) {
let name: Cow<'static, str> = Cow::Owned(name.to_string());
@ -32,11 +31,10 @@ impl DynamicProperties {
}
}
impl Properties for DynamicProperties {
#[inline]
fn type_name(&self) -> &str {
self.type_name
&self.type_name
}
#[inline]
fn prop(&self, name: &str) -> Option<&dyn Property> {
@ -98,11 +96,138 @@ impl Serialize for DynamicProperties {
}
}
impl<'a> Deserialize<'a> for DynamicProperties {
fn deserialize<D>(_deserializer: D) -> Result<Self, D::Error>
impl<'de> Deserialize<'de> for DynamicProperties {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'a>,
D: serde::Deserializer<'de>,
{
Ok(DynamicProperties::default())
let mut dynamic_properties = DynamicProperties::default();
deserializer.deserialize_map(PropMapVisiter {
dynamic_properties: &mut dynamic_properties,
})?;
Ok(dynamic_properties)
}
}
struct PropMapVisiter<'a> {
dynamic_properties: &'a mut DynamicProperties,
}
impl<'a, 'de> Visitor<'de> for PropMapVisiter<'a> {
type Value = ();
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("map of properties")
}
fn visit_map<V>(self, mut map: V) -> Result<(), V::Error>
where
V: MapAccess<'de>,
{
let mut type_name: Option<String> = None;
while let Some(key) = map.next_key::<String>()? {
if &key == "type" {
type_name = Some(map.next_value()?);
} else {
let prop = map.next_value()?;
self.dynamic_properties.set_box(&key, prop);
}
}
let type_name = type_name.ok_or_else(|| de::Error::missing_field("type"))?;
self.dynamic_properties.type_name = type_name.to_string();
Ok(())
}
}
impl<'de> Deserialize<'de> for Box<dyn Property> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
deserializer.deserialize_any(AnyPropVisiter)
}
}
struct AnyPropVisiter;
impl<'de> Visitor<'de> for AnyPropVisiter {
type Value = Box<dyn Property>;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("property value")
}
fn visit_u8<E>(self, v: u8) -> Result<Self::Value, E>
where E: de::Error {
Ok(Box::new(v))
}
fn visit_u16<E>(self, v: u16) -> Result<Self::Value, E>
where E: de::Error {
Ok(Box::new(v))
}
fn visit_u32<E>(self, v: u32) -> Result<Self::Value, E>
where E: de::Error {
Ok(Box::new(v))
}
fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
where E: de::Error {
Ok(Box::new(v))
}
fn visit_i8<E>(self, v: i8) -> Result<Self::Value, E>
where E: de::Error {
Ok(Box::new(v))
}
fn visit_i16<E>(self, v: i16) -> Result<Self::Value, E>
where E: de::Error {
Ok(Box::new(v))
}
fn visit_i32<E>(self, v: i32) -> Result<Self::Value, E>
where E: de::Error {
Ok(Box::new(v))
}
fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
where E: de::Error {
Ok(Box::new(v))
}
fn visit_f32<E>(self, v: f32) -> Result<Self::Value, E>
where E: de::Error {
Ok(Box::new(v))
}
fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
where E: de::Error {
Ok(Box::new(v))
}
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
where E: de::Error {
Ok(Box::new(v))
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where E: de::Error {
Ok(Box::new(v.to_string()))
}
fn visit_map<V>(self, mut map: V) -> Result<Self::Value, V::Error>
where
V: MapAccess<'de>,
{
let mut dynamic_properties = DynamicProperties::default();
// while let Some(key) = map.next_key()? {
// let prop = map.next_value()?;
// self.dynamic_properties.set_box(key, prop);
// }
panic!("aaah");
// Ok(Box::new(dynamic_properties))
}
}

View File

@ -31,7 +31,7 @@ pub trait Properties {
dynamic_props.set_box(name, prop.clone_prop());
}
dynamic_props.type_name = std::any::type_name::<Self>();
dynamic_props.type_name = std::any::type_name::<Self>().to_string();
dynamic_props
}
}

View File

@ -17,13 +17,22 @@ pub struct Test {
a: usize,
b: String,
c: f32,
// nest: Nested,
}
// #[derive(Properties, Default, Clone)]
// pub struct Nested {
// d: usize,
// }
fn setup() {
let mut test = Test {
a: 1,
b: "hi".to_string(),
c: 1.0,
// nest: Nested {
// d: 8,
// }
};
test.set_prop_val::<usize>("a", 2);
@ -72,4 +81,5 @@ fn setup() {
let round_tripped = String::from_utf8(buf).unwrap();
println!();
println!("{}", round_tripped);
assert_eq!(ron_string, round_tripped);
}