-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreflection.rs
156 lines (132 loc) · 4.03 KB
/
reflection.rs
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use failure::Fail;
use std::collections::HashMap;
#[derive(Debug, Fail)]
pub enum ReflectionError {
#[fail(
display = "Primitive value type mismatch, expected '{:?}', got '{:?}'",
expected, actual
)]
ValueMismatch {
expected: PrimitiveValueKind,
actual: PrimitiveValueKind,
},
#[fail(display = "Field '{}' does not exist", name)]
InvalidField { name: String },
}
// Primitive values
#[derive(Debug, Clone, Copy)]
pub enum PrimitiveValueKind {
String,
Bool,
}
pub enum PrimitiveValue {
String(String),
Bool(bool),
}
impl PrimitiveValue {
fn kind(&self) -> PrimitiveValueKind {
match self {
PrimitiveValue::String(_) => PrimitiveValueKind::String,
PrimitiveValue::Bool(_) => PrimitiveValueKind::Bool,
}
}
}
// Reflection API
pub enum FieldMutReflection<'a> {
Object(&'a mut dyn Object),
List(&'a mut dyn List),
Primitive(&'a mut dyn Primitive),
Any(&'a mut serde_json::Value),
}
pub trait Object {
fn create(&mut self, field_name: &str) -> Result<FieldMutReflection, ReflectionError>;
}
pub trait List {
fn push(&mut self) -> FieldMutReflection;
}
pub trait Primitive {
fn kind(&self) -> PrimitiveValueKind;
fn set(&mut self, value: PrimitiveValue) -> Result<(), ReflectionError>;
}
// Blanket implementations
impl<T> Object for HashMap<String, T>
where
T: Object + Default,
{
fn create(&mut self, field_name: &str) -> Result<FieldMutReflection, ReflectionError> {
let child = self.entry(field_name.to_owned()).or_insert_with(T::default);
Ok(FieldMutReflection::Object(child))
}
}
impl<T> Object for HashMap<String, Vec<T>>
where
Vec<T>: List,
{
fn create(&mut self, field_name: &str) -> Result<FieldMutReflection, ReflectionError> {
let list = self.entry(field_name.to_owned()).or_insert_with(Vec::new);
Ok(FieldMutReflection::List(list))
}
}
impl<T> List for Vec<T>
where
T: Object + Default,
{
fn push(&mut self) -> FieldMutReflection {
self.push(T::default());
FieldMutReflection::Object(self.last_mut().unwrap())
}
}
// Any (serde_json::Value) support
// serde_json::Value
impl Object for HashMap<String, serde_json::Value> {
fn create(&mut self, field_name: &str) -> Result<FieldMutReflection, ReflectionError> {
let child = self
.entry(field_name.to_owned())
.or_insert(serde_json::Value::Null);
Ok(FieldMutReflection::Any(child))
}
}
impl List for Vec<serde_json::Value> {
fn push(&mut self) -> FieldMutReflection {
self.push(serde_json::Value::Null);
FieldMutReflection::Any(self.last_mut().unwrap())
}
}
// Primitives support
macro_rules! primitive {
($typ:ty => $kind:ident) => {
impl Primitive for $typ {
fn kind(&self) -> PrimitiveValueKind {
PrimitiveValueKind::$kind
}
fn set(&mut self, value: PrimitiveValue) -> Result<(), ReflectionError> {
match value {
PrimitiveValue::$kind(s) => {
*self = s;
Ok(())
}
v => Err(ReflectionError::ValueMismatch {
expected: PrimitiveValueKind::$kind,
actual: v.kind(),
}),
}
}
}
impl Object for HashMap<String, $typ> {
fn create(&mut self, field_name: &str) -> Result<FieldMutReflection, ReflectionError> {
let primitive = self
.entry(field_name.to_owned())
.or_insert_with(Default::default);
Ok(FieldMutReflection::Primitive(primitive))
}
}
impl List for Vec<$typ> {
fn push(&mut self) -> FieldMutReflection {
self.push(<$typ>::default());
FieldMutReflection::Primitive(self.last_mut().unwrap())
}
}
};
}
primitive!(String => String);
primitive!(bool => Bool);