props: deserialize (no nesting yet)
This commit is contained in:
parent
6e31b90ec3
commit
284afd4f94
@ -1,15 +1,14 @@
|
|||||||
use std::{collections::HashMap, borrow::Cow};
|
use std::{collections::HashMap, borrow::Cow};
|
||||||
use crate::{Properties, Property, PropertyIter};
|
use crate::{Properties, Property, PropertyIter};
|
||||||
use serde::{Deserialize, Serialize, ser::SerializeMap};
|
use serde::{Deserialize, Serialize, ser::SerializeMap, de::{self, MapAccess, Visitor}};
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct DynamicProperties {
|
pub struct DynamicProperties {
|
||||||
pub type_name: &'static str,
|
pub type_name: String,
|
||||||
pub props: Vec<(Cow<'static, str>, Box<dyn Property>)>,
|
pub props: Vec<(Cow<'static, str>, Box<dyn Property>)>,
|
||||||
pub prop_indices: HashMap<Cow<'static, str>, usize>,
|
pub prop_indices: HashMap<Cow<'static, str>, usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl DynamicProperties {
|
impl DynamicProperties {
|
||||||
fn push(&mut self, name: &str, prop: Box<dyn Property>) {
|
fn push(&mut self, name: &str, prop: Box<dyn Property>) {
|
||||||
let name: Cow<'static, str> = Cow::Owned(name.to_string());
|
let name: Cow<'static, str> = Cow::Owned(name.to_string());
|
||||||
@ -32,11 +31,10 @@ impl DynamicProperties {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl Properties for DynamicProperties {
|
impl Properties for DynamicProperties {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn type_name(&self) -> &str {
|
fn type_name(&self) -> &str {
|
||||||
self.type_name
|
&self.type_name
|
||||||
}
|
}
|
||||||
#[inline]
|
#[inline]
|
||||||
fn prop(&self, name: &str) -> Option<&dyn Property> {
|
fn prop(&self, name: &str) -> Option<&dyn Property> {
|
||||||
@ -98,11 +96,138 @@ impl Serialize for DynamicProperties {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Deserialize<'a> for DynamicProperties {
|
impl<'de> Deserialize<'de> for DynamicProperties {
|
||||||
fn deserialize<D>(_deserializer: D) -> Result<Self, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
where
|
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))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -31,7 +31,7 @@ pub trait Properties {
|
|||||||
dynamic_props.set_box(name, prop.clone_prop());
|
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
|
dynamic_props
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,13 +17,22 @@ pub struct Test {
|
|||||||
a: usize,
|
a: usize,
|
||||||
b: String,
|
b: String,
|
||||||
c: f32,
|
c: f32,
|
||||||
|
// nest: Nested,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// #[derive(Properties, Default, Clone)]
|
||||||
|
// pub struct Nested {
|
||||||
|
// d: usize,
|
||||||
|
// }
|
||||||
|
|
||||||
fn setup() {
|
fn setup() {
|
||||||
let mut test = Test {
|
let mut test = Test {
|
||||||
a: 1,
|
a: 1,
|
||||||
b: "hi".to_string(),
|
b: "hi".to_string(),
|
||||||
c: 1.0,
|
c: 1.0,
|
||||||
|
// nest: Nested {
|
||||||
|
// d: 8,
|
||||||
|
// }
|
||||||
};
|
};
|
||||||
|
|
||||||
test.set_prop_val::<usize>("a", 2);
|
test.set_prop_val::<usize>("a", 2);
|
||||||
@ -72,4 +81,5 @@ fn setup() {
|
|||||||
let round_tripped = String::from_utf8(buf).unwrap();
|
let round_tripped = String::from_utf8(buf).unwrap();
|
||||||
println!();
|
println!();
|
||||||
println!("{}", round_tripped);
|
println!("{}", round_tripped);
|
||||||
|
assert_eq!(ron_string, round_tripped);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user