1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
pub use defaults::Defaults;
pub use enum_dispatch::enum_dispatch;
pub use serde::{de, Deserialize, Deserializer, Serialize};
pub use serde_json::Value;
pub use serde_with::skip_serializing_none;
use std::collections::BTreeMap;
pub use std::convert::AsRef;
pub use std::sync::Arc;
pub use strum::AsRefStr;
#[enum_dispatch]
pub trait NodeTrait {
fn type_name(&self) -> String;
fn id(&self) -> Option<String>;
}
macro_rules! impl_primitive {
($type:ident) => {
impl NodeTrait for $type {
fn type_name(&self) -> String {
stringify!($type).into()
}
fn id(&self) -> Option<String> {
None
}
}
};
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Primitive {
Null,
Boolean(Boolean),
Integer(Integer),
Number(Number),
String(String),
Object(Object),
Array(Array),
}
pub type Boolean = bool;
impl_primitive!(Boolean);
pub type Integer = i64;
impl_primitive!(Integer);
pub type Number = f64;
impl_primitive!(Number);
pub type Array = Vec<Primitive>;
impl_primitive!(Array);
pub type Object = BTreeMap<String, Primitive>;
impl_primitive!(Object);
#[macro_export]
macro_rules! impl_struct {
($type:ident) => {
impl NodeTrait for $type {
fn type_name(&self) -> String {
stringify!($type).into()
}
fn id(&self) -> Option<String> {
self.id.as_ref().map(|id| *id.clone())
}
}
};
}
#[macro_export]
macro_rules! impl_enum {
($type:ident) => {
impl NodeTrait for $type {
fn type_name(&self) -> String {
stringify!($type).into()
}
fn id(&self) -> Option<String> {
None
}
}
};
}