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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
use crate::*;
use cyfs_base::*;

use std::fmt;
use std::sync::Arc;

#[derive(Clone, Debug)]
pub struct NONInputRequestCommon {
    // 请求路径,可为空
    pub req_path: Option<String>,

    // the request source info in bundle
    pub source: RequestSourceInfo,

    // api级别
    pub level: NONAPILevel,

    // 用以处理默认行为
    pub target: Option<ObjectId>,

    pub flags: u32,
}

impl fmt::Display for NONInputRequestCommon {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "req_path: {:?}", self.req_path)?;

        write!(f, ", {}", self.source)?;
        write!(f, ", level: {}", self.level.to_string())?;

        if let Some(target) = &self.target {
            write!(f, ", target: {}", target.to_string())?;
        }

        write!(f, ", flags: {}", self.flags)?;

        Ok(())
    }
}

/*
/object_id
/dir_id|object_map/inner_path
*/
#[derive(Clone, Debug)]
pub struct NONGetObjectInputRequest {
    pub common: NONInputRequestCommon,

    pub object_id: ObjectId,

    // object_id在dir情况下适用
    pub inner_path: Option<String>,
}

impl fmt::Display for NONGetObjectInputRequest {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "common: {}", self.common)?;
        write!(f, ", object_id: {}", self.object_id)?;

        write!(f, ", inner_path: {:?}", self.inner_path)
    }
}

#[derive(Debug)]
pub struct NONGetObjectInputResponse {
    pub object_update_time: Option<u64>,
    pub object_expires_time: Option<u64>,

    pub object: NONObjectInfo,

    // 对file有效
    pub attr: Option<Attributes>,
}

impl NONGetObjectInputResponse {
    pub fn new(
        object_id: ObjectId,
        object_raw: Vec<u8>,
        object: Option<Arc<AnyNamedObject>>,
    ) -> Self {
        let object = NONObjectInfo::new(object_id, object_raw, object);
        Self::new_with_object(object)
    }

    pub fn new_with_object(object: NONObjectInfo) -> Self {
        Self {
            object,
            object_expires_time: None,
            object_update_time: None,
            attr: None,
        }
    }

    pub fn init_times(&mut self) -> BuckyResult<()> {
        let t = self.object.get_update_time()?;
        if t > 0 {
            self.object_update_time = Some(t);
        }

        let t = self.object.get_expired_time()?;
        self.object_expires_time = t;
        Ok(())
    }
}

impl fmt::Display for NONGetObjectInputResponse {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, ", object: {}", self.object)?;
        write!(f, ", object_update_time: {:?}", self.object_update_time)?;
        write!(f, ", object_expires_time: {:?}", self.object_expires_time)?;

        if let Some(attr) = &self.attr {
            write!(f, ", attr: {:?}", attr)?;
        }

        Ok(())
    }
}

#[derive(Debug, Clone)]
pub struct NONPutObjectInputRequest {
    pub common: NONInputRequestCommon,

    pub object: NONObjectInfo,
    pub access: Option<AccessString>,
}

impl fmt::Display for NONPutObjectInputRequest {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "common: {}", self.common)?;
        write!(f, ", object: {}", self.object)?;
        if let Some(access) = &self.access {
            write!(f, ", access: {}", access.to_string())?;
        }

        Ok(())
    }
}

#[derive(Debug)]
pub struct NONPutObjectInputResponse {
    pub result: NONPutObjectResult,
    pub object_update_time: Option<u64>,
    pub object_expires_time: Option<u64>,
}

impl Default for NONPutObjectInputResponse {
    fn default() -> Self {
        Self::new(NONPutObjectResult::Accept)
    }
}

impl NONPutObjectInputResponse {
    pub fn new(result: NONPutObjectResult) -> Self {
        Self {
            result,
            object_update_time: None,
            object_expires_time: None,
        }
    }
}

impl fmt::Display for NONPutObjectInputResponse {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "result: {:?}", self.result)?;
        write!(f, ", object_update_time: {:?}", self.object_update_time)?;
        write!(f, ", object_expires_time: {:?}", self.object_expires_time)
    }
}

// post_object请求
#[derive(Debug, Clone)]
pub struct NONPostObjectInputRequest {
    pub common: NONInputRequestCommon,

    pub object: NONObjectInfo,
}

impl fmt::Display for NONPostObjectInputRequest {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "common: {}", self.common)?;
        write!(f, ", object: {}", self.object)
    }
}

#[derive(Debug)]
pub struct NONPostObjectInputResponse {
    pub object: Option<NONObjectInfo>,
}

impl fmt::Display for NONPostObjectInputResponse {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.object {
            Some(object) => {
                write!(f, "object: {}", object,)
            }
            None => {
                write!(f, "none",)
            }
        }
    }
}

// select object
#[derive(Debug, Clone)]
pub struct NONSelectObjectInputRequest {
    pub common: NONInputRequestCommon,

    pub filter: SelectFilter,
    pub opt: Option<SelectOption>,
}

impl fmt::Display for NONSelectObjectInputRequest {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "common: {}", self.common)?;
        write!(f, ", filter: {}", self.filter)?;
        write!(f, ", opt: {:?}", self.opt)
    }
}

#[derive(Debug)]
pub struct NONSelectObjectInputResponse {
    pub objects: Vec<SelectResponseObjectInfo>,
}

impl fmt::Display for NONSelectObjectInputResponse {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "select count: {}, list=[", self.objects.len())?;

        for item in &self.objects {
            write!(f, "{{ {} }}, ", item)?;
        }

        write!(f, "]")?;

        Ok(())
    }
}

// delete object
#[derive(Debug, Clone)]
pub struct NONDeleteObjectInputRequest {
    pub common: NONInputRequestCommon,

    pub object_id: ObjectId,

    pub inner_path: Option<String>,
}

impl fmt::Display for NONDeleteObjectInputRequest {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "common: {}", self.common)?;
        write!(f, ", object_id: {}", self.object_id)?;

        if let Some(inner_path) = &self.inner_path {
            write!(f, ", inner_path: {}", inner_path)?;
        }

        Ok(())
    }
}

#[derive(Debug)]
pub struct NONDeleteObjectInputResponse {
    pub object: Option<NONObjectInfo>,
}

impl fmt::Display for NONDeleteObjectInputResponse {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(item) = &self.object {
            write!(f, "object: {}", item)?;
        }
        Ok(())
    }
}