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::{
|
use std::fmt::{self, Display, Formatter};
|
||||||
fmt::{self, Display, Formatter},
|
|
||||||
sync::Arc,
|
|
||||||
};
|
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
@ -167,7 +164,7 @@ impl AbstractTree for FunctionNode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn run(&self, _source: &str, _context: &Map) -> Result<Value> {
|
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)
|
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 serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
AbstractTree, BuiltInValue, Error, Expression, Format, Function, FunctionNode, Identifier,
|
AbstractTree, BuiltInValue, Error, Expression, Format, Function, FunctionNode, Identifier,
|
||||||
List, Map, Result, Statement, Structure, SyntaxNode, Type, TypeDefintion, TypeSpecification,
|
List, Map, Result, Statement, Structure, SyntaxNode, Type, TypeSpecification, Value,
|
||||||
Value,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
|
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
|
||||||
@ -34,7 +33,7 @@ impl AbstractTree for ValueNode {
|
|||||||
"function" => {
|
"function" => {
|
||||||
let function_node = FunctionNode::from_syntax(child, source, context)?;
|
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()),
|
"integer" => ValueNode::Integer(source[child.byte_range()].to_string()),
|
||||||
"string" => {
|
"string" => {
|
||||||
@ -255,7 +254,7 @@ impl AbstractTree for ValueNode {
|
|||||||
value_map.insert(key.to_string(), (value_option, r#type.clone()));
|
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()),
|
ValueNode::Range(range) => Value::Range(range.clone()),
|
||||||
};
|
};
|
||||||
|
@ -1,7 +1,4 @@
|
|||||||
use std::{
|
use std::fmt::{self, Display, Formatter};
|
||||||
fmt::{self, Display, Formatter},
|
|
||||||
sync::Arc,
|
|
||||||
};
|
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
@ -9,10 +6,10 @@ use crate::{
|
|||||||
built_in_functions::Callable, BuiltInFunction, Format, FunctionNode, Map, Result, Type, Value,
|
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 {
|
pub enum Function {
|
||||||
BuiltIn(BuiltInFunction),
|
BuiltIn(BuiltInFunction),
|
||||||
ContextDefined(Arc<FunctionNode>),
|
ContextDefined(FunctionNode),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Function {
|
impl Function {
|
||||||
|
@ -18,15 +18,12 @@ use std::{
|
|||||||
ops::{Add, AddAssign, Div, Mul, Range, Rem, Sub, SubAssign},
|
ops::{Add, AddAssign, Div, Mul, Range, Rem, Sub, SubAssign},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use self::{
|
pub use self::{function::Function, list::List, map::Map, structure::Structure};
|
||||||
function::Function, list::List, map::Map, structure::Structure, type_definition::TypeDefintion,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub mod function;
|
pub mod function;
|
||||||
pub mod list;
|
pub mod list;
|
||||||
pub mod map;
|
pub mod map;
|
||||||
pub mod structure;
|
pub mod structure;
|
||||||
pub mod type_definition;
|
|
||||||
|
|
||||||
/// Dust value representation.
|
/// Dust value representation.
|
||||||
///
|
///
|
||||||
@ -44,7 +41,7 @@ pub enum Value {
|
|||||||
Boolean(bool),
|
Boolean(bool),
|
||||||
Range(Range<i64>),
|
Range(Range<i64>),
|
||||||
Option(Option<Box<Value>>),
|
Option(Option<Box<Value>>),
|
||||||
TypeDefinition(TypeDefintion),
|
Structure(Structure),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Value {
|
impl Default for Value {
|
||||||
@ -109,8 +106,8 @@ impl Value {
|
|||||||
Type::None
|
Type::None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Value::TypeDefinition(_) => todo!(),
|
|
||||||
Value::Range(_) => todo!(),
|
Value::Range(_) => todo!(),
|
||||||
|
Value::Structure(_) => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -441,7 +438,7 @@ impl PartialEq for Value {
|
|||||||
(Value::Map(left), Value::Map(right)) => left == right,
|
(Value::Map(left), Value::Map(right)) => left == right,
|
||||||
(Value::Function(left), Value::Function(right)) => left == right,
|
(Value::Function(left), Value::Function(right)) => left == right,
|
||||||
(Value::Option(left), Value::Option(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,
|
(Value::Range(left), Value::Range(right)) => left == right,
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
@ -479,8 +476,8 @@ impl Ord for Value {
|
|||||||
(Value::Map(_), _) => Ordering::Greater,
|
(Value::Map(_), _) => Ordering::Greater,
|
||||||
(Value::Function(left), Value::Function(right)) => left.cmp(right),
|
(Value::Function(left), Value::Function(right)) => left.cmp(right),
|
||||||
(Value::Function(_), _) => Ordering::Greater,
|
(Value::Function(_), _) => Ordering::Greater,
|
||||||
(Value::TypeDefinition(left), Value::TypeDefinition(right)) => left.cmp(right),
|
(Value::Structure(left), Value::Structure(right)) => left.cmp(right),
|
||||||
(Value::TypeDefinition(_), _) => Ordering::Greater,
|
(Value::Structure(_), _) => Ordering::Greater,
|
||||||
(Value::Range(left), Value::Range(right)) => {
|
(Value::Range(left), Value::Range(right)) => {
|
||||||
let left_len = left.end - left.start;
|
let left_len = left.end - left.start;
|
||||||
let right_len = right.end - right.start;
|
let right_len = right.end - right.start;
|
||||||
@ -517,7 +514,7 @@ impl Serialize for Value {
|
|||||||
Value::Option(inner) => inner.serialize(serializer),
|
Value::Option(inner) => inner.serialize(serializer),
|
||||||
Value::Map(inner) => inner.serialize(serializer),
|
Value::Map(inner) => inner.serialize(serializer),
|
||||||
Value::Function(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),
|
Value::Range(range) => range.serialize(serializer),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -540,7 +537,7 @@ impl Display for Value {
|
|||||||
Value::List(list) => write!(f, "{list}"),
|
Value::List(list) => write!(f, "{list}"),
|
||||||
Value::Map(map) => write!(f, "{map}"),
|
Value::Map(map) => write!(f, "{map}"),
|
||||||
Value::Function(function) => write!(f, "{function}"),
|
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),
|
Value::Range(range) => write!(f, "{}..{}", range.start, range.end),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,19 @@
|
|||||||
use std::{
|
use std::{
|
||||||
collections::BTreeMap,
|
collections::BTreeMap,
|
||||||
fmt::{self, Display, Formatter},
|
fmt::{self, Display, Formatter},
|
||||||
|
marker::PhantomData,
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
};
|
};
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{
|
||||||
|
de::{MapAccess, Visitor},
|
||||||
|
ser::SerializeMap,
|
||||||
|
Deserialize, Deserializer, Serialize, Serializer,
|
||||||
|
};
|
||||||
|
|
||||||
use crate::{Type, Value};
|
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)>>);
|
pub struct Structure(Arc<BTreeMap<String, (Option<Value>, Type)>>);
|
||||||
|
|
||||||
impl Structure {
|
impl Structure {
|
||||||
@ -35,3 +40,64 @@ impl Display for Structure {
|
|||||||
write!(f, "}}")
|
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));
|
btree_map.insert("x".to_string(), (Some(Value::Integer(0)), Type::Integer));
|
||||||
|
|
||||||
let expected = Ok(Value::TypeDefinition(TypeDefintion::Structure(
|
let expected = Ok(Value::Structure(Structure::new(btree_map)));
|
||||||
Structure::new(btree_map),
|
|
||||||
)));
|
|
||||||
|
|
||||||
assert_eq!(expected, result);
|
assert_eq!(expected, result);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user