cyfs_lib/router_handler/http/
request.rs1use super::super::*;
2use cyfs_base::*;
3
4use serde_json::{Map, Value};
5
6#[derive(Debug)]
7pub struct RouterAddHandlerParam {
8 pub filter: Option<String>,
9 pub req_path: Option<String>,
10 pub index: i32,
11
12 pub default_action: RouterHandlerAction,
13
14 pub routine: Option<String>,
15}
16
17pub struct RouterRemoveHandlerParam {
18 pub id: String,
19}
20
21impl JsonCodec<RouterAddHandlerParam> for RouterAddHandlerParam {
22 fn encode_json(&self) -> Map<String, Value> {
23 let mut obj = Map::new();
24
25 JsonCodecHelper::encode_option_string_field(&mut obj, "filter", self.filter.as_ref());
26 JsonCodecHelper::encode_option_string_field(&mut obj, "req_path", self.req_path.as_ref());
27
28 obj.insert("index".to_string(), Value::String(self.index.to_string()));
29 obj.insert(
30 "default_action".to_string(),
31 Value::Object(self.default_action.encode_json()),
32 );
33
34 if self.routine.is_some() {
35 obj.insert(
36 "routine".to_string(),
37 Value::String(self.routine.as_ref().unwrap().clone()),
38 );
39 }
40
41 obj
42 }
43
44 fn decode_json(req_obj: &Map<String, Value>) -> BuckyResult<Self> {
45 let mut filter: Option<String> = None;
46 let mut req_path: Option<String> = None;
47 let mut default_action: Option<RouterHandlerAction> = None;
48 let mut routine: Option<String> = None;
49 let mut index: Option<i32> = None;
50
51 for (k, v) in req_obj {
52 match k.as_str() {
53 "filter" => {
54 filter = Some(JsonCodecHelper::decode_from_string(&v)?);
55 }
56 "req_path" => {
57 req_path = Some(JsonCodecHelper::decode_from_string(&v)?);
58 }
59
60 "index" => {
61 index = Some(JsonCodecHelper::decode_to_int(v)?);
62 }
63 "default_action" => {
64 if v.is_string() {
73 default_action = Some(JsonCodecHelper::decode_from_string(v)?);
74 } else if v.is_object() {
75 default_action =
76 Some(RouterHandlerAction::decode_json(v.as_object().unwrap())?);
77 } else {
78 let msg = format!("invalid default_action field format: {:?}", v);
79 warn!("{}", msg);
80 return Err(BuckyError::new(BuckyErrorCode::InvalidParam, msg));
81 }
82 }
83
84 "routine" => {
85 if !v.is_string() {
86 let msg = format!("invalid routine field: {:?}", v);
87 warn!("{}", msg);
88
89 return Err(BuckyError::new(BuckyErrorCode::InvalidParam, msg));
90 }
91
92 routine = Some(v.as_str().unwrap().to_owned());
93 }
94
95 u @ _ => {
96 warn!("unknown router handler field: {}", u);
97 }
98 }
99 }
100
101 if index.is_none() || default_action.is_none() {
102 let msg = format!("router handler request field missing: index/default_action");
103 warn!("{}", msg);
104
105 return Err(BuckyError::new(BuckyErrorCode::InvalidParam, msg));
106 }
107
108 let req = Self {
109 filter,
110 req_path,
111 index: index.unwrap(),
112 default_action: default_action.unwrap(),
113 routine,
114 };
115
116 Ok(req)
117 }
118}
119
120impl JsonCodec<RouterRemoveHandlerParam> for RouterRemoveHandlerParam {
121 fn encode_json(&self) -> Map<String, Value> {
122 let mut obj = Map::new();
123
124 obj.insert("id".to_string(), Value::String(self.id.clone()));
125
126 obj
127 }
128
129 fn decode_json(req_obj: &Map<String, Value>) -> BuckyResult<Self> {
130 let mut id: Option<String> = None;
131
132 for (k, v) in req_obj {
133 match k.as_str() {
134 "id" => {
135 if !v.is_string() {
136 let msg = format!("invalid handler id field: {:?}", v);
137 warn!("{}", msg);
138
139 return Err(BuckyError::new(BuckyErrorCode::InvalidParam, msg));
140 }
141
142 id = Some(v.as_str().unwrap().to_owned())
143 }
144
145 u @ _ => {
146 warn!("unknown router handler field: {}", u);
147 }
148 }
149 }
150
151 if id.is_none() {
152 let msg = format!("routine handler request missing: id");
153 warn!("{}", msg);
154
155 return Err(BuckyError::new(BuckyErrorCode::InvalidParam, msg));
156 }
157
158 let req = Self { id: id.unwrap() };
159
160 Ok(req)
161 }
162}
163
164