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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use super::super::*;
use cyfs_base::*;
use serde_json::{Map, Value};
#[derive(Debug)]
pub struct RouterAddHandlerParam {
pub filter: Option<String>,
pub req_path: Option<String>,
pub index: i32,
pub default_action: RouterHandlerAction,
pub routine: Option<String>,
}
pub struct RouterRemoveHandlerParam {
pub id: String,
}
impl JsonCodec<RouterAddHandlerParam> for RouterAddHandlerParam {
fn encode_json(&self) -> Map<String, Value> {
let mut obj = Map::new();
JsonCodecHelper::encode_option_string_field(&mut obj, "filter", self.filter.as_ref());
JsonCodecHelper::encode_option_string_field(&mut obj, "req_path", self.req_path.as_ref());
obj.insert("index".to_string(), Value::String(self.index.to_string()));
obj.insert(
"default_action".to_string(),
Value::Object(self.default_action.encode_json()),
);
if self.routine.is_some() {
obj.insert(
"routine".to_string(),
Value::String(self.routine.as_ref().unwrap().clone()),
);
}
obj
}
fn decode_json(req_obj: &Map<String, Value>) -> BuckyResult<Self> {
let mut filter: Option<String> = None;
let mut req_path: Option<String> = None;
let mut default_action: Option<RouterHandlerAction> = None;
let mut routine: Option<String> = None;
let mut index: Option<i32> = None;
for (k, v) in req_obj {
match k.as_str() {
"filter" => {
filter = Some(JsonCodecHelper::decode_from_string(&v)?);
}
"req_path" => {
req_path = Some(JsonCodecHelper::decode_from_string(&v)?);
}
"index" => {
index = Some(JsonCodecHelper::decode_to_int(v)?);
}
"default_action" => {
if v.is_string() {
default_action = Some(JsonCodecHelper::decode_from_string(v)?);
} else if v.is_object() {
default_action =
Some(RouterHandlerAction::decode_json(v.as_object().unwrap())?);
} else {
let msg = format!("invalid default_action field format: {:?}", v);
warn!("{}", msg);
return Err(BuckyError::new(BuckyErrorCode::InvalidParam, msg));
}
}
"routine" => {
if !v.is_string() {
let msg = format!("invalid routine field: {:?}", v);
warn!("{}", msg);
return Err(BuckyError::new(BuckyErrorCode::InvalidParam, msg));
}
routine = Some(v.as_str().unwrap().to_owned());
}
u @ _ => {
warn!("unknown router handler field: {}", u);
}
}
}
if index.is_none() || default_action.is_none() {
let msg = format!("router handler request field missing: index/default_action");
warn!("{}", msg);
return Err(BuckyError::new(BuckyErrorCode::InvalidParam, msg));
}
let req = Self {
filter,
req_path,
index: index.unwrap(),
default_action: default_action.unwrap(),
routine,
};
Ok(req)
}
}
impl JsonCodec<RouterRemoveHandlerParam> for RouterRemoveHandlerParam {
fn encode_json(&self) -> Map<String, Value> {
let mut obj = Map::new();
obj.insert("id".to_string(), Value::String(self.id.clone()));
obj
}
fn decode_json(req_obj: &Map<String, Value>) -> BuckyResult<Self> {
let mut id: Option<String> = None;
for (k, v) in req_obj {
match k.as_str() {
"id" => {
if !v.is_string() {
let msg = format!("invalid handler id field: {:?}", v);
warn!("{}", msg);
return Err(BuckyError::new(BuckyErrorCode::InvalidParam, msg));
}
id = Some(v.as_str().unwrap().to_owned())
}
u @ _ => {
warn!("unknown router handler field: {}", u);
}
}
}
if id.is_none() {
let msg = format!("routine handler request missing: id");
warn!("{}", msg);
return Err(BuckyError::new(BuckyErrorCode::InvalidParam, msg));
}
let req = Self { id: id.unwrap() };
Ok(req)
}
}