cyfs_lib/non/
output_request.rs

1use super::def::*;
2use crate::*;
3use cyfs_base::*;
4
5use std::fmt;
6
7#[derive(Clone)]
8pub struct NONOutputRequestCommon {
9    // 请求路径,可为空
10    pub req_path: Option<String>,
11
12    // 来源Device,默认为空表示当前协议栈的device-id,在zone内转发请求时候会使用此字段
13    pub source: Option<DeviceId>,
14
15    // 来源DEC,如果为None,默认为system-dec
16    pub dec_id: Option<ObjectId>,
17
18    // api级别
19    pub level: NONAPILevel,
20
21    // 用以处理默认行为
22    pub target: Option<ObjectId>,
23
24    pub flags: u32,
25}
26
27impl NONOutputRequestCommon {
28    pub fn new(level: NONAPILevel) -> Self {
29        Self {
30            req_path: None,
31            source: None,
32            dec_id: None,
33            level,
34            target: None,
35            flags: 0,
36        }
37    }
38}
39
40impl fmt::Display for NONOutputRequestCommon {
41    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42        write!(f, "req_path: {:?}", self.req_path)?;
43        write!(f, ", level: {:?}", self.level)?;
44
45        if let Some(source) = &self.source {
46            write!(f, ", source: {}", source)?;
47        }
48
49        if let Some(dec_id) = &self.dec_id {
50            write!(f, ", dec_id: {}", dec_id)?;
51        }
52
53        if let Some(target) = &self.target {
54            write!(f, ", target: {}", target.to_string())?;
55        }
56
57        write!(f, ", flags: {}", self.flags)?;
58
59        Ok(())
60    }
61}
62
63// put requests
64#[derive(Clone)]
65pub struct NONPutObjectOutputRequest {
66    pub common: NONOutputRequestCommon,
67
68    pub object: NONObjectInfo,
69    pub access: Option<AccessString>,
70}
71
72impl NONPutObjectOutputRequest {
73    pub fn new(level: NONAPILevel, object_id: ObjectId, object_raw: Vec<u8>) -> Self {
74        Self {
75            common: NONOutputRequestCommon::new(level),
76            object: NONObjectInfo::new(object_id, object_raw, None),
77            access: None,
78        }
79    }
80
81    pub fn new_noc(object_id: ObjectId, object_raw: Vec<u8>) -> Self {
82        Self::new(NONAPILevel::NOC, object_id, object_raw)
83    }
84
85    pub fn new_non(target: Option<DeviceId>, object_id: ObjectId, object_raw: Vec<u8>) -> Self {
86        let mut ret = Self::new(NONAPILevel::NON, object_id, object_raw);
87        ret.common.target = target.map(|v| v.into());
88
89        ret
90    }
91
92    pub fn new_router(target: Option<ObjectId>, object_id: ObjectId, object_raw: Vec<u8>) -> Self {
93        let mut ret = Self::new(NONAPILevel::Router, object_id, object_raw);
94        ret.common.target = target;
95
96        ret
97    }
98}
99
100impl fmt::Display for NONPutObjectOutputRequest {
101    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
102        write!(f, "common: {}", self.common)?;
103        write!(f, ", object: {}", self.object)?;
104        if let Some(access) = &self.access {
105            write!(f, ", access: {}", access.to_string())?;
106        }
107
108        Ok(())
109    }
110}
111
112#[derive(Clone)]
113pub struct NONUpdateObjectMetaOutputRequest {
114    pub common: NONOutputRequestCommon,
115
116    pub object_id: ObjectId,
117    pub access: Option<AccessString>,
118}
119
120impl NONUpdateObjectMetaOutputRequest {
121    pub fn new(level: NONAPILevel, object_id: ObjectId, access: Option<AccessString>) -> Self {
122        Self {
123            common: NONOutputRequestCommon::new(level),
124            object_id,
125            access,
126        }
127    }
128
129    pub fn new_noc(object_id: ObjectId, access: Option<AccessString>) -> Self {
130        Self::new(NONAPILevel::NOC, object_id, access)
131    }
132
133    pub fn new_non(target: Option<DeviceId>, object_id: ObjectId, access: Option<AccessString>) -> Self {
134        let mut ret = Self::new(NONAPILevel::NON, object_id, access);
135        ret.common.target = target.map(|v| v.into());
136
137        ret
138    }
139
140    pub fn new_router(target: Option<ObjectId>, object_id: ObjectId, access: Option<AccessString>) -> Self {
141        let mut ret = Self::new(NONAPILevel::Router, object_id, access);
142        ret.common.target = target;
143
144        ret
145    }
146}
147
148impl fmt::Display for NONUpdateObjectMetaOutputRequest {
149    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
150        write!(f, "common: {}", self.common)?;
151        write!(f, ", object: {}", self.object_id)?;
152        if let Some(access) = &self.access {
153            write!(f, ", access: {}", access.to_string())?;
154        }
155
156        Ok(())
157    }
158}
159
160#[derive(Clone)]
161pub struct NONPutObjectOutputResponse {
162    pub result: NONPutObjectResult,
163    pub object_update_time: Option<u64>,
164    pub object_expires_time: Option<u64>,
165}
166
167impl fmt::Display for NONPutObjectOutputResponse {
168    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
169        write!(f, "result: {:?}", self.result)?;
170        write!(f, ", object_update_time: {:?}", self.object_update_time)?;
171        write!(f, ", object_expires_time: {:?}", self.object_expires_time)
172    }
173}
174
175// get requests
176#[derive(Clone)]
177pub struct NONGetObjectOutputRequest {
178    pub common: NONOutputRequestCommon,
179
180    pub object_id: ObjectId,
181
182    // inner_path在dir情况下适用
183    pub inner_path: Option<String>,
184}
185
186impl NONGetObjectOutputRequest {
187    pub fn new(level: NONAPILevel, object_id: ObjectId, inner_path: Option<String>) -> Self {
188        Self {
189            common: NONOutputRequestCommon::new(level),
190            object_id,
191            inner_path,
192        }
193    }
194
195    pub fn new_noc(object_id: ObjectId, inner_path: Option<String>) -> Self {
196        Self::new(NONAPILevel::NOC, object_id, inner_path)
197    }
198
199    pub fn new_non(
200        target: Option<DeviceId>,
201        object_id: ObjectId,
202        inner_path: Option<String>,
203    ) -> Self {
204        let mut ret = Self::new(NONAPILevel::NON, object_id, inner_path);
205        ret.common.target = target.map(|v| v.into());
206
207        ret
208    }
209
210    pub fn new_router(
211        target: Option<ObjectId>,
212        object_id: ObjectId,
213        inner_path: Option<String>,
214    ) -> Self {
215        let mut ret = Self::new(NONAPILevel::Router, object_id, inner_path);
216        ret.common.target = target;
217
218        ret
219    }
220
221    pub fn object_debug_info(&self) -> String {
222        if let Some(req_path) = &self.common.req_path {
223            if let Some(inner_path) = &self.inner_path {
224                if inner_path.starts_with('/') {
225                    format!("{}:{}{}", req_path, self.object_id, inner_path)
226                } else {
227                    format!("{}:{}/{}", req_path, self.object_id, inner_path)
228                }
229            } else {
230                format!("{}:{}", req_path, self.object_id)
231            }
232        } else {
233            if let Some(inner_path) = &self.inner_path {
234                if inner_path.starts_with('/') {
235                    format!("{}{}", self.object_id, inner_path)
236                } else {
237                    format!("{}/{}", self.object_id, inner_path)
238                }
239            } else {
240                self.object_id.to_string()
241            }
242        }
243    }
244}
245
246
247impl fmt::Display for NONGetObjectOutputRequest {
248    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
249        write!(f, "common: {}", self.common)?;
250        write!(f, ", object_id: {}", self.object_id)?;
251
252        write!(f, ", inner_path: {:?}", self.inner_path)
253    }
254}
255
256#[derive(Clone)]
257pub struct NONGetObjectOutputResponse {
258    pub object_update_time: Option<u64>,
259    pub object_expires_time: Option<u64>,
260
261    pub object: NONObjectInfo,
262
263    // 对file有效
264    pub attr: Option<Attributes>,
265}
266
267impl fmt::Display for NONGetObjectOutputResponse {
268    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
269        write!(f, "object: {}", self.object)?;
270        write!(f, ", object_update_time: {:?}", self.object_update_time)?;
271        write!(f, ", object_expires_time: {:?}", self.object_expires_time)?;
272
273        if let Some(attr) = &self.attr {
274            write!(f, ", attr: {:?}", attr)?;
275        }
276
277        Ok(())
278    }
279}
280
281// POST请求
282#[derive(Clone)]
283pub struct NONPostObjectOutputRequest {
284    pub common: NONOutputRequestCommon,
285
286    pub object: NONObjectInfo,
287}
288
289impl NONPostObjectOutputRequest {
290    pub fn new(level: NONAPILevel, object_id: ObjectId, object_raw: Vec<u8>) -> Self {
291        Self {
292            common: NONOutputRequestCommon::new(level),
293            object: NONObjectInfo::new(object_id, object_raw, None),
294        }
295    }
296
297    pub fn new_noc(object_id: ObjectId, object_raw: Vec<u8>) -> Self {
298        Self::new(NONAPILevel::NOC, object_id, object_raw)
299    }
300
301    pub fn new_non(target: Option<DeviceId>, object_id: ObjectId, object_raw: Vec<u8>) -> Self {
302        let mut ret = Self::new(NONAPILevel::NON, object_id, object_raw);
303        ret.common.target = target.map(|v| v.into());
304
305        ret
306    }
307
308    pub fn new_router(target: Option<ObjectId>, object_id: ObjectId, object_raw: Vec<u8>) -> Self {
309        let mut ret = Self::new(NONAPILevel::Router, object_id, object_raw);
310        ret.common.target = target;
311
312        ret
313    }
314}
315
316impl fmt::Display for NONPostObjectOutputRequest {
317    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
318        write!(f, "common: {}", self.common)?;
319        write!(f, ", object: {}", self.object)
320    }
321}
322
323#[derive(Clone)]
324pub struct NONPostObjectOutputResponse {
325    pub object: Option<NONObjectInfo>,
326}
327
328impl fmt::Display for NONPostObjectOutputResponse {
329    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
330        match &self.object {
331            Some(object) => {
332                write!(f, "object: {}", object,)
333            }
334            None => {
335                write!(f, "none",)
336            }
337        }
338    }
339}
340
341
342// select
343#[derive(Clone)]
344pub struct NONSelectObjectOutputRequest {
345    pub common: NONOutputRequestCommon,
346
347    pub filter: SelectFilter,
348    pub opt: Option<SelectOption>,
349}
350
351impl NONSelectObjectOutputRequest {
352    pub fn new(level: NONAPILevel, filter: SelectFilter, opt: Option<SelectOption>) -> Self {
353        Self {
354            common: NONOutputRequestCommon::new(level),
355            filter,
356            opt,
357        }
358    }
359
360    pub fn new_noc(filter: SelectFilter, opt: Option<SelectOption>) -> Self {
361        Self::new(NONAPILevel::NOC, filter, opt)
362    }
363
364    pub fn new_non(
365        target: Option<DeviceId>,
366        filter: SelectFilter,
367        opt: Option<SelectOption>,
368    ) -> Self {
369        let mut ret = Self::new(NONAPILevel::NON, filter, opt);
370        ret.common.target = target.map(|v| v.into());
371
372        ret
373    }
374
375    pub fn new_router(
376        target: Option<ObjectId>,
377        filter: SelectFilter,
378        opt: Option<SelectOption>,
379    ) -> Self {
380        let mut ret = Self::new(NONAPILevel::Router, filter, opt);
381        ret.common.target = target;
382
383        ret
384    }
385}
386
387impl fmt::Display for NONSelectObjectOutputRequest {
388    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
389        write!(f, "common: {}", self.common)?;
390        write!(f, ", filter: {}", self.filter)?;
391        write!(f, ", opt: {:?}", self.opt)
392    }
393}
394
395
396#[derive(Clone)]
397pub struct NONSelectObjectOutputResponse {
398    pub objects: Vec<SelectResponseObjectInfo>,
399}
400
401impl fmt::Display for NONSelectObjectOutputResponse {
402    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
403        write!(f, "select count: {}, list=[", self.objects.len())?;
404
405        for item in &self.objects {
406            write!(f, "{{ {} }}, ", item)?;
407        }
408
409        write!(f, "]")?;
410
411        Ok(())
412    }
413}
414
415#[derive(Clone)]
416pub struct NONDeleteObjectOutputRequest {
417    pub common: NONOutputRequestCommon,
418
419    pub object_id: ObjectId,
420
421    pub inner_path: Option<String>,
422}
423
424impl NONDeleteObjectOutputRequest {
425    pub fn new(level: NONAPILevel, object_id: ObjectId, inner_path: Option<String>) -> Self {
426        Self {
427            common: NONOutputRequestCommon::new(level),
428            object_id,
429            inner_path,
430        }
431    }
432
433    pub fn new_noc(object_id: ObjectId, inner_path: Option<String>) -> Self {
434        Self::new(NONAPILevel::NOC, object_id, inner_path)
435    }
436
437    pub fn new_non(
438        target: Option<DeviceId>,
439        object_id: ObjectId,
440        inner_path: Option<String>,
441    ) -> Self {
442        let mut ret = Self::new(NONAPILevel::NON, object_id, inner_path);
443        ret.common.target = target.map(|v| v.into());
444
445        ret
446    }
447
448    pub fn new_router(
449        target: Option<ObjectId>,
450        object_id: ObjectId,
451        inner_path: Option<String>,
452    ) -> Self {
453        let mut ret = Self::new(NONAPILevel::Router, object_id, inner_path);
454        ret.common.target = target;
455
456        ret
457    }
458}
459
460impl fmt::Display for NONDeleteObjectOutputRequest {
461    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
462        write!(f, "common: {}", self.common)?;
463        write!(f, ", object_id: {}", self.object_id)?;
464
465        if let Some(inner_path) = &self.inner_path {
466            write!(f, ", inner_path: {}", inner_path)?;
467        }
468
469        Ok(())
470    }
471}
472
473
474#[derive(Clone)]
475pub struct NONDeleteObjectOutputResponse {
476    pub object: Option<NONObjectInfo>,
477}
478
479impl fmt::Display for NONDeleteObjectOutputResponse {
480    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
481        write!(f, "object: {:?}", self.object)?;
482    
483        Ok(())
484    }
485}