cyfs_lib/non/
input_request_codec.rs

1use super::input_request::*;
2use cyfs_base::*;
3
4use serde_json::{Map, Value};
5
6impl JsonCodec<NONInputRequestCommon> for NONInputRequestCommon {
7    fn encode_json(&self) -> Map<String, Value> {
8        let mut obj = Map::new();
9
10        JsonCodecHelper::encode_option_string_field(&mut obj, "req_path", self.req_path.as_ref());
11        JsonCodecHelper::encode_field(&mut obj, "source", &self.source);
12        JsonCodecHelper::encode_string_field(&mut obj, "level", &self.level);
13        JsonCodecHelper::encode_option_string_field(&mut obj, "target", self.target.as_ref());
14        JsonCodecHelper::encode_number_field(&mut obj, "flags", self.flags);
15
16        obj
17    }
18
19    fn decode_json(obj: &Map<String, Value>) -> BuckyResult<NONInputRequestCommon> {
20        Ok(Self {
21            req_path: JsonCodecHelper::decode_option_string_field(obj, "req_path")?,
22            source: JsonCodecHelper::decode_field(obj, "source")?,
23            level: JsonCodecHelper::decode_string_field(obj, "level")?,
24            target: JsonCodecHelper::decode_option_string_field(obj, "target")?,
25            flags: JsonCodecHelper::decode_int_field(obj, "flags")?,
26        })
27    }
28}
29
30// get_object
31impl JsonCodec<NONGetObjectInputRequest> for NONGetObjectInputRequest {
32    fn encode_json(&self) -> Map<String, Value> {
33        let mut obj = Map::new();
34
35        JsonCodecHelper::encode_field(&mut obj, "common", &self.common);
36        JsonCodecHelper::encode_string_field(&mut obj, "object_id", &self.object_id);
37        JsonCodecHelper::encode_option_string_field(
38            &mut obj,
39            "inner_path",
40            self.inner_path.as_ref(),
41        );
42
43        obj
44    }
45
46    fn decode_json(obj: &Map<String, Value>) -> BuckyResult<NONGetObjectInputRequest> {
47        Ok(Self {
48            common: JsonCodecHelper::decode_field(obj, "common")?,
49            object_id: JsonCodecHelper::decode_string_field(obj, "object_id")?,
50            inner_path: JsonCodecHelper::decode_option_string_field(obj, "inner_path")?,
51        })
52    }
53}
54
55impl JsonCodec<NONGetObjectInputResponse> for NONGetObjectInputResponse {
56    fn encode_json(&self) -> Map<String, Value> {
57        let mut obj = Map::new();
58
59        JsonCodecHelper::encode_field(&mut obj, "object", &self.object);
60        JsonCodecHelper::encode_option_string_field(
61            &mut obj,
62            "object_expires_time",
63            self.object_expires_time.as_ref(),
64        );
65        JsonCodecHelper::encode_option_string_field(
66            &mut obj,
67            "object_update_time",
68            self.object_update_time.as_ref(),
69        );
70
71        JsonCodecHelper::encode_option_number_field(
72            &mut obj,
73            "attr",
74            self.attr.as_ref().map(|v| v.flags()),
75        );
76
77        obj
78    }
79
80    fn decode_json(obj: &Map<String, Value>) -> BuckyResult<NONGetObjectInputResponse> {
81        let attr = JsonCodecHelper::decode_option_int_field(obj, "attr")?;
82
83        Ok(Self {
84            object: JsonCodecHelper::decode_field(obj, "object")?,
85            object_expires_time: JsonCodecHelper::decode_option_string_field(
86                obj,
87                "object_expires_time",
88            )?,
89            object_update_time: JsonCodecHelper::decode_option_string_field(
90                obj,
91                "object_update_time",
92            )?,
93            attr: attr.map(|v| Attributes::new(v)),
94        })
95    }
96}
97
98// put_object
99impl JsonCodec<NONPutObjectInputRequest> for NONPutObjectInputRequest {
100    fn encode_json(&self) -> Map<String, Value> {
101        let mut obj = Map::new();
102
103        JsonCodecHelper::encode_field(&mut obj, "common", &self.common);
104        JsonCodecHelper::encode_field(&mut obj, "object", &self.object);
105        if let Some(access) = &self.access {
106            JsonCodecHelper::encode_number_field(&mut obj, "access", access.value());
107        }
108        obj
109    }
110
111    fn decode_json(obj: &Map<String, Value>) -> BuckyResult<NONPutObjectInputRequest> {
112        let access: Option<u32> = JsonCodecHelper::decode_option_int_field(obj, "access")?;
113        let access = access.map(|v| AccessString::new(v));
114
115        Ok(Self {
116            common: JsonCodecHelper::decode_field(obj, "common")?,
117            object: JsonCodecHelper::decode_field(obj, "object")?,
118            access,
119        })
120    }
121}
122
123impl JsonCodec<NONPutObjectInputResponse> for NONPutObjectInputResponse {
124    fn encode_json(&self) -> Map<String, Value> {
125        let mut obj = Map::new();
126
127        JsonCodecHelper::encode_string_field(&mut obj, "result", &self.result);
128        JsonCodecHelper::encode_option_string_field(
129            &mut obj,
130            "object_expires_time",
131            self.object_expires_time.as_ref(),
132        );
133        JsonCodecHelper::encode_option_string_field(
134            &mut obj,
135            "object_update_time",
136            self.object_update_time.as_ref(),
137        );
138
139        obj
140    }
141
142    fn decode_json(obj: &Map<String, Value>) -> BuckyResult<NONPutObjectInputResponse> {
143        Ok(Self {
144            result: JsonCodecHelper::decode_string_field(obj, "result")?,
145            object_expires_time: JsonCodecHelper::decode_option_string_field(
146                obj,
147                "object_expires_time",
148            )?,
149            object_update_time: JsonCodecHelper::decode_option_string_field(
150                obj,
151                "object_update_time",
152            )?,
153        })
154    }
155}
156
157// post_object
158impl JsonCodec<NONPostObjectInputRequest> for NONPostObjectInputRequest {
159    fn encode_json(&self) -> Map<String, Value> {
160        let mut obj = Map::new();
161
162        JsonCodecHelper::encode_field(&mut obj, "common", &self.common);
163        JsonCodecHelper::encode_field(&mut obj, "object", &self.object);
164        obj
165    }
166
167    fn decode_json(obj: &Map<String, Value>) -> BuckyResult<NONPostObjectInputRequest> {
168        Ok(Self {
169            common: JsonCodecHelper::decode_field(obj, "common")?,
170            object: JsonCodecHelper::decode_field(obj, "object")?,
171        })
172    }
173}
174
175impl JsonCodec<NONPostObjectInputResponse> for NONPostObjectInputResponse {
176    fn encode_json(&self) -> Map<String, Value> {
177        let mut obj = Map::new();
178
179        JsonCodecHelper::encode_option_field(&mut obj, "object", self.object.as_ref());
180
181        obj
182    }
183
184    fn decode_json(obj: &Map<String, Value>) -> BuckyResult<NONPostObjectInputResponse> {
185        Ok(Self {
186            object: JsonCodecHelper::decode_option_field(obj, "object")?,
187        })
188    }
189}
190
191// select_object
192impl JsonCodec<NONSelectObjectInputRequest> for NONSelectObjectInputRequest {
193    fn encode_json(&self) -> Map<String, Value> {
194        let mut obj = Map::new();
195
196        JsonCodecHelper::encode_field(&mut obj, "common", &self.common);
197
198        JsonCodecHelper::encode_field(&mut obj, "filter", &self.filter);
199        JsonCodecHelper::encode_option_field(&mut obj, "opt", self.opt.as_ref());
200
201        obj
202    }
203
204    fn decode_json(obj: &Map<String, Value>) -> BuckyResult<NONSelectObjectInputRequest> {
205        Ok(Self {
206            common: JsonCodecHelper::decode_field(obj, "common")?,
207            filter: JsonCodecHelper::decode_field(obj, "filter")?,
208            opt: JsonCodecHelper::decode_option_field(obj, "opt")?,
209        })
210    }
211}
212
213impl JsonCodec<NONSelectObjectInputResponse> for NONSelectObjectInputResponse {
214    fn encode_json(&self) -> Map<String, Value> {
215        let mut obj = Map::new();
216
217        JsonCodecHelper::encode_as_list(&mut obj, "objects", &self.objects);
218
219        obj
220    }
221
222    fn decode_json(obj: &Map<String, Value>) -> BuckyResult<NONSelectObjectInputResponse> {
223        Ok(Self {
224            objects: JsonCodecHelper::decode_array_field(obj, "objects")?,
225        })
226    }
227}
228
229// delete_object
230impl JsonCodec<NONDeleteObjectInputRequest> for NONDeleteObjectInputRequest {
231    fn encode_json(&self) -> Map<String, Value> {
232        let mut obj = Map::new();
233
234        JsonCodecHelper::encode_field(&mut obj, "common", &self.common);
235        JsonCodecHelper::encode_string_field(&mut obj, "object_id", &self.object_id);
236        JsonCodecHelper::encode_option_string_field(
237            &mut obj,
238            "inner_path",
239            self.inner_path.as_ref(),
240        );
241
242        obj
243    }
244
245    fn decode_json(obj: &Map<String, Value>) -> BuckyResult<NONDeleteObjectInputRequest> {
246        Ok(Self {
247            common: JsonCodecHelper::decode_field(obj, "common")?,
248            object_id: JsonCodecHelper::decode_string_field(obj, "object_id")?,
249            inner_path: JsonCodecHelper::decode_option_string_field(obj, "inner_path")?,
250        })
251    }
252}
253
254impl JsonCodec<NONDeleteObjectInputResponse> for NONDeleteObjectInputResponse {
255    fn encode_json(&self) -> Map<String, Value> {
256        let mut obj = Map::new();
257
258        if let Some(object) = &self.object {
259            JsonCodecHelper::encode_field(&mut obj, "object", object);
260        }
261
262        obj
263    }
264
265    fn decode_json(obj: &Map<String, Value>) -> BuckyResult<NONDeleteObjectInputResponse> {
266        Ok(Self {
267            object: JsonCodecHelper::decode_option_field(obj, "object")?,
268        })
269    }
270}