Fix serde errors
This commit is contained in:
parent
9762112b3c
commit
f85fed941a
2256
Cargo.lock
generated
2256
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -1,7 +1,4 @@
|
||||
use std::{
|
||||
fmt::{self, Display, Formatter},
|
||||
sync::Arc,
|
||||
};
|
||||
use std::fmt::{self, Display, Formatter};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@ -167,7 +164,7 @@ impl AbstractTree for FunctionNode {
|
||||
}
|
||||
|
||||
fn run(&self, _source: &str, _context: &Map) -> Result<Value> {
|
||||
let self_as_value = Value::Function(Function::ContextDefined(Arc::new(self.clone())));
|
||||
let self_as_value = Value::Function(Function::ContextDefined(self.clone()));
|
||||
|
||||
Ok(self_as_value)
|
||||
}
|
||||
|
@ -1,11 +1,10 @@
|
||||
use std::{cmp::Ordering, collections::BTreeMap, ops::Range, sync::Arc};
|
||||
use std::{cmp::Ordering, collections::BTreeMap, ops::Range};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
AbstractTree, BuiltInValue, Error, Expression, Format, Function, FunctionNode, Identifier,
|
||||
List, Map, Result, Statement, Structure, SyntaxNode, Type, TypeDefintion, TypeSpecification,
|
||||
Value,
|
||||
List, Map, Result, Statement, Structure, SyntaxNode, Type, TypeSpecification, Value,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
|
||||
@ -34,7 +33,7 @@ impl AbstractTree for ValueNode {
|
||||
"function" => {
|
||||
let function_node = FunctionNode::from_syntax(child, source, context)?;
|
||||
|
||||
ValueNode::Function(Function::ContextDefined(Arc::new(function_node)))
|
||||
ValueNode::Function(Function::ContextDefined(function_node))
|
||||
}
|
||||
"integer" => ValueNode::Integer(source[child.byte_range()].to_string()),
|
||||
"string" => {
|
||||
@ -255,7 +254,7 @@ impl AbstractTree for ValueNode {
|
||||
value_map.insert(key.to_string(), (value_option, r#type.clone()));
|
||||
}
|
||||
|
||||
Value::TypeDefinition(TypeDefintion::Structure(Structure::new(value_map)))
|
||||
Value::Structure(Structure::new(value_map))
|
||||
}
|
||||
ValueNode::Range(range) => Value::Range(range.clone()),
|
||||
};
|
||||
|
@ -1,7 +1,4 @@
|
||||
use std::{
|
||||
fmt::{self, Display, Formatter},
|
||||
sync::Arc,
|
||||
};
|
||||
use std::fmt::{self, Display, Formatter};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@ -9,10 +6,10 @@ use crate::{
|
||||
built_in_functions::Callable, BuiltInFunction, Format, FunctionNode, Map, Result, Type, Value,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
|
||||
pub enum Function {
|
||||
BuiltIn(BuiltInFunction),
|
||||
ContextDefined(Arc<FunctionNode>),
|
||||
ContextDefined(FunctionNode),
|
||||
}
|
||||
|
||||
impl Function {
|
||||
|
@ -18,15 +18,12 @@ use std::{
|
||||
ops::{Add, AddAssign, Div, Mul, Range, Rem, Sub, SubAssign},
|
||||
};
|
||||
|
||||
pub use self::{
|
||||
function::Function, list::List, map::Map, structure::Structure, type_definition::TypeDefintion,
|
||||
};
|
||||
pub use self::{function::Function, list::List, map::Map, structure::Structure};
|
||||
|
||||
pub mod function;
|
||||
pub mod list;
|
||||
pub mod map;
|
||||
pub mod structure;
|
||||
pub mod type_definition;
|
||||
|
||||
/// Dust value representation.
|
||||
///
|
||||
@ -44,7 +41,7 @@ pub enum Value {
|
||||
Boolean(bool),
|
||||
Range(Range<i64>),
|
||||
Option(Option<Box<Value>>),
|
||||
TypeDefinition(TypeDefintion),
|
||||
Structure(Structure),
|
||||
}
|
||||
|
||||
impl Default for Value {
|
||||
@ -109,8 +106,8 @@ impl Value {
|
||||
Type::None
|
||||
}
|
||||
}
|
||||
Value::TypeDefinition(_) => todo!(),
|
||||
Value::Range(_) => todo!(),
|
||||
Value::Structure(_) => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -441,7 +438,7 @@ impl PartialEq for Value {
|
||||
(Value::Map(left), Value::Map(right)) => left == right,
|
||||
(Value::Function(left), Value::Function(right)) => left == right,
|
||||
(Value::Option(left), Value::Option(right)) => left == right,
|
||||
(Value::TypeDefinition(left), Value::TypeDefinition(right)) => left == right,
|
||||
(Value::Structure(left), Value::Structure(right)) => left == right,
|
||||
(Value::Range(left), Value::Range(right)) => left == right,
|
||||
_ => false,
|
||||
}
|
||||
@ -479,8 +476,8 @@ impl Ord for Value {
|
||||
(Value::Map(_), _) => Ordering::Greater,
|
||||
(Value::Function(left), Value::Function(right)) => left.cmp(right),
|
||||
(Value::Function(_), _) => Ordering::Greater,
|
||||
(Value::TypeDefinition(left), Value::TypeDefinition(right)) => left.cmp(right),
|
||||
(Value::TypeDefinition(_), _) => Ordering::Greater,
|
||||
(Value::Structure(left), Value::Structure(right)) => left.cmp(right),
|
||||
(Value::Structure(_), _) => Ordering::Greater,
|
||||
(Value::Range(left), Value::Range(right)) => {
|
||||
let left_len = left.end - left.start;
|
||||
let right_len = right.end - right.start;
|
||||
@ -517,7 +514,7 @@ impl Serialize for Value {
|
||||
Value::Option(inner) => inner.serialize(serializer),
|
||||
Value::Map(inner) => inner.serialize(serializer),
|
||||
Value::Function(inner) => inner.serialize(serializer),
|
||||
Value::TypeDefinition(inner) => inner.serialize(serializer),
|
||||
Value::Structure(inner) => inner.serialize(serializer),
|
||||
Value::Range(range) => range.serialize(serializer),
|
||||
}
|
||||
}
|
||||
@ -540,7 +537,7 @@ impl Display for Value {
|
||||
Value::List(list) => write!(f, "{list}"),
|
||||
Value::Map(map) => write!(f, "{map}"),
|
||||
Value::Function(function) => write!(f, "{function}"),
|
||||
Value::TypeDefinition(structure) => write!(f, "{structure}"),
|
||||
Value::Structure(structure) => write!(f, "{structure}"),
|
||||
Value::Range(range) => write!(f, "{}..{}", range.start, range.end),
|
||||
}
|
||||
}
|
||||
|
@ -1,14 +1,19 @@
|
||||
use std::{
|
||||
collections::BTreeMap,
|
||||
fmt::{self, Display, Formatter},
|
||||
marker::PhantomData,
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde::{
|
||||
de::{MapAccess, Visitor},
|
||||
ser::SerializeMap,
|
||||
Deserialize, Deserializer, Serialize, Serializer,
|
||||
};
|
||||
|
||||
use crate::{Type, Value};
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
|
||||
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
|
||||
pub struct Structure(Arc<BTreeMap<String, (Option<Value>, Type)>>);
|
||||
|
||||
impl Structure {
|
||||
@ -35,3 +40,64 @@ impl Display for Structure {
|
||||
write!(f, "}}")
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for Structure {
|
||||
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let mut map = serializer.serialize_map(Some(self.0.len()))?;
|
||||
|
||||
for (key, (value, _type)) in self.0.iter() {
|
||||
map.serialize_entry(key, value)?;
|
||||
}
|
||||
|
||||
map.end()
|
||||
}
|
||||
}
|
||||
|
||||
struct StructureVisitor {
|
||||
marker: PhantomData<fn() -> Structure>,
|
||||
}
|
||||
|
||||
impl StructureVisitor {
|
||||
fn new() -> Self {
|
||||
StructureVisitor {
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Visitor<'de> for StructureVisitor {
|
||||
type Value = Structure;
|
||||
|
||||
fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
|
||||
formatter.write_str("key-value pairs")
|
||||
}
|
||||
|
||||
fn visit_map<M>(self, mut access: M) -> std::result::Result<Structure, M::Error>
|
||||
where
|
||||
M: MapAccess<'de>,
|
||||
{
|
||||
let mut b_tree = BTreeMap::new();
|
||||
|
||||
{
|
||||
while let Some((key, value)) = access.next_entry::<String, Value>()? {
|
||||
let r#type = value.r#type();
|
||||
|
||||
b_tree.insert(key, (Some(value), r#type));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Structure::new(b_tree))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for Structure {
|
||||
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_any(StructureVisitor::new())
|
||||
}
|
||||
}
|
||||
|
@ -1,18 +0,0 @@
|
||||
use std::fmt::{self, Display, Formatter};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::Structure;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
|
||||
pub enum TypeDefintion {
|
||||
Structure(Structure),
|
||||
}
|
||||
|
||||
impl Display for TypeDefintion {
|
||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||
match self {
|
||||
TypeDefintion::Structure(structure) => write!(f, "{structure}"),
|
||||
}
|
||||
}
|
||||
}
|
@ -10,9 +10,7 @@ fn simple_structure() {
|
||||
|
||||
btree_map.insert("x".to_string(), (Some(Value::Integer(0)), Type::Integer));
|
||||
|
||||
let expected = Ok(Value::TypeDefinition(TypeDefintion::Structure(
|
||||
Structure::new(btree_map),
|
||||
)));
|
||||
let expected = Ok(Value::Structure(Structure::new(btree_map)));
|
||||
|
||||
assert_eq!(expected, result);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user