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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
mod errors;
pub mod namespace;
pub use errors::Error;
use crate::action::Action;
use crate::actions::setter::namespace::Namespace;
use crate::actions::setter::Error as SetterError;
use crate::errors::Error as CrateErr;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use std::borrow::Cow;
/// This type represents an [Action](../action/trait.Action.html) which sets data to the
/// destination JSON Value.
#[derive(Debug, Serialize, Deserialize)]
pub struct Setter {
namespace: Vec<Namespace>,
child: Box<dyn Action>,
}
impl Setter {
pub fn new(namespace: Vec<Namespace>, child: Box<dyn Action>) -> Self {
Self { namespace, child }
}
}
#[typetag::serde]
impl Action for Setter {
fn apply<'a>(
&self,
source: &'a Value,
destination: &mut Value,
) -> Result<Option<Cow<'a, Value>>, CrateErr> {
if let Some(field) = self.child.apply(source, destination)? {
let field = field.into_owned();
let mut current = destination;
for ns in &self.namespace {
match ns {
Namespace::Object { id } => {
match current {
Value::Object(o) => {
current = o.entry(id.clone()).or_insert(Value::Null);
}
Value::Null => {
let mut o = Map::new();
o.insert(id.clone(), Value::Null);
*current = Value::Object(o);
current = current.as_object_mut().unwrap().get_mut(id).unwrap();
}
_ => {
return Err(SetterError::InvalidDestinationType(format!(
"Attempting to set an Object by id to an {:?}",
current
))
.into())
}
};
}
Namespace::Array { index } => {
let index = *index;
match current {
Value::Array(arr) => {
if index >= arr.len() {
arr.resize_with(index + 1, Value::default);
arr[index] = Value::Null;
}
current = &mut arr[index];
}
Value::Null => {
*current = Value::Array(vec![Value::Null; index + 1]);
current = &mut current.as_array_mut().unwrap()[index];
}
_ => {
return Err(SetterError::InvalidDestinationType(format!(
"Attempting to set an Array by index to an {:?}",
current
))
.into())
}
};
}
Namespace::AppendArray => {
match current {
Value::Array(arr) => {
arr.push(Value::Null);
current = arr.last_mut().unwrap();
}
Value::Null => {
let arr = vec![Value::Null];
*current = Value::Array(arr);
current = current.as_array_mut().unwrap().last_mut().unwrap();
}
_ => {
return Err(SetterError::InvalidDestinationType(format!(
"Attempting to append an {:?} to an Array",
current
))
.into())
}
};
}
Namespace::MergeObject => {
return match field {
Value::Object(mut o) => match current {
Value::Object(existing) => {
existing.append(&mut o);
Ok(None)
}
Value::Null => {
let mut new = Map::new();
new.append(&mut o);
*current = Value::Object(new);
Ok(None)
}
_ => Err(SetterError::InvalidDestinationType(format!(
"Attempting to merge an Object with and {:?}",
current
))
.into()),
},
_ => Err(SetterError::InvalidDestinationType(format!(
"Attempting to merge {:?} with an Object",
field
))
.into()),
};
}
Namespace::MergeArray => {
return match field {
Value::Array(arr) => match current {
Value::Array(existing) => {
if arr.len() > existing.len() {
*existing = arr;
return Ok(None);
}
for (i, v) in arr.into_iter().enumerate() {
existing[i] = v.clone();
}
Ok(None)
}
Value::Null => {
*current = Value::Array(arr);
Ok(None)
}
_ => Err(SetterError::InvalidDestinationType(format!(
"Attempting to merge an Array with and {:?}",
current
))
.into()),
},
_ => Err(SetterError::InvalidDestinationType(format!(
"Attempting to merge {:?} with an Array",
field
))
.into()),
};
}
Namespace::CombineArray => {
return match field {
Value::Array(mut arr) => match current {
Value::Array(existing) => {
existing.append(&mut arr);
Ok(None)
}
Value::Null => {
*current = Value::Array(arr);
Ok(None)
}
_ => Err(SetterError::InvalidDestinationType(format!(
"Attempting to combine an Array with and {:?}",
current
))
.into()),
},
_ => Err(SetterError::InvalidDestinationType(format!(
"Attempting to merge {:?} with an Array",
field
))
.into()),
};
}
};
}
*current = field;
}
Ok(None)
}
}