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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
use cyfs_base::*;

use std::str::FromStr;

use crate::NONAPILevel;

#[derive(Debug, Hash, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum NDNAction {
    PutData,
    GetData,

    DeleteData,
    PutSharedData,
    GetSharedData,

    QueryFile,
}

impl ToString for NDNAction {
    fn to_string(&self) -> String {
        (match *self {
            Self::PutData => "put-data",
            Self::GetData => "get-data",
            Self::DeleteData => "delete-data",
            Self::PutSharedData => "put-shared-data",
            Self::GetSharedData => "get-shared-data",
            Self::QueryFile => "query-file",
        })
        .to_owned()
    }
}

impl FromStr for NDNAction {
    type Err = BuckyError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        let ret = match value {
            "put-data" => Self::PutData,
            "get-data" => Self::GetData,
            "delete-data" => Self::DeleteData,
            "put-shared-data" => Self::PutSharedData,
            "get-shared-data" => Self::GetSharedData,
            "query-file" => Self::QueryFile,
            v @ _ => {
                let msg = format!("unknown ndn action: {}", v);
                error!("{}", msg);

                return Err(BuckyError::new(BuckyErrorCode::InvalidData, msg));
            }
        };

        Ok(ret)
    }
}

// non操作的缺省行为,默认为NON
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum NDNAPILevel {
    NDC = 0,
    NDN = 1,
    Router = 2,
}

impl Into<NONAPILevel> for NDNAPILevel {
    fn into(self) -> NONAPILevel {
        match self {
            Self::NDC => NONAPILevel::NOC,
            Self::NDN => NONAPILevel::NON,
            Self::Router => NONAPILevel::Router,
        }
    }
}

impl Default for NDNAPILevel {
    fn default() -> Self {
        Self::Router
    }
}

impl ToString for NDNAPILevel {
    fn to_string(&self) -> String {
        (match *self {
            Self::NDC => "ndc",
            Self::NDN => "ndn",
            Self::Router => "router",
        })
        .to_owned()
    }
}

impl FromStr for NDNAPILevel {
    type Err = BuckyError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        let ret = match value {
            "ndc" => Self::NDC,
            "ndn" => Self::NDN,
            "router" => Self::Router,
            v @ _ => {
                let msg = format!("unknown ndn api level: {}", v);
                error!("{}", msg);

                return Err(BuckyError::new(BuckyErrorCode::UnSupport, msg));
            }
        };

        Ok(ret)
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum NDNPutDataResult {
    Accept,
    AlreadyExists,
}

impl ToString for NDNPutDataResult {
    fn to_string(&self) -> String {
        (match *self {
            Self::Accept => "Accept",
            Self::AlreadyExists => "AlreadyExists",
        })
        .to_owned()
    }
}

impl FromStr for NDNPutDataResult {
    type Err = BuckyError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        let ret = match value {
            "Accept" => Self::Accept,
            "AlreadyExists" => Self::AlreadyExists,
            v @ _ => {
                let msg = format!("unknown NDNPutDataResult: {}", v);
                error!("{}", msg);

                return Err(BuckyError::new(BuckyErrorCode::InvalidData, msg));
            }
        };

        Ok(ret)
    }
}

// 对于chunk,那么需要配置所引用的file_id/dir_id,以及最近的一个dir_id
// dir_id(dir内部直接使用了chunk)
// dir_id/file_id
// file_id(file内部的chunk)

// target 可以是 chunk的owner,或者转发者, 或者是关联的 dsg contract

// chunk的关联对象,一般是[target:]file  或者 [target:]dir/inner_path
#[derive(Clone, Eq, PartialEq)]
pub struct NDNDataRefererObject {
    pub target: Option<ObjectId>,
    pub object_id: ObjectId,
    pub inner_path: Option<String>,
}

impl NDNDataRefererObject {
    pub fn is_inner_path_empty(&self) -> bool {
        match &self.inner_path {
            Some(v) => v.trim().is_empty(),
            None => true,
        }
    }

    pub fn to_string(&self) -> String {
        let last = if let Some(inner_path) = &self.inner_path {
            format!("{}/{}", self.object_id.to_string(), inner_path.trim_start_matches('/'))
        } else {
            self.object_id.to_string()
        };
        if let Some(target) = &self.target {
            format!("{}:{}", target.to_string(), last)
        } else {
            last
        }
    }
}

impl std::fmt::Display for NDNDataRefererObject {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.to_string())
    }
}

impl std::fmt::Debug for NDNDataRefererObject {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.to_string())
    }
}

impl FromStr for NDNDataRefererObject {
    type Err = BuckyError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        let parts: Vec<&str> = value.split("/").collect();
        if parts.is_empty() {
            let msg = format!(
                "invalid NDNDataRefererObject, object_id not found! {}",
                value
            );
            error!("{}", msg);
            return Err(BuckyError::new(BuckyErrorCode::InvalidFormat, msg));
        }

        let id_parts: Vec<&str> = parts[0].split(":").collect();
        let (target, object_id) = if id_parts.len() == 1 {
            ObjectId::from_str(id_parts[0])
                .map_err(|e| {
                    let msg = format!(
                        "invalid NDNDataRefererObject object_id format! {}, {}",
                        value, e
                    );
                    error!("{}", msg);
                    BuckyError::new(BuckyErrorCode::InvalidFormat, msg)
                })
                .map(|o| (None, o))
        } else if id_parts.len() == 2 {
            ObjectId::from_str(id_parts[0])
                .map_err(|e| {
                    let msg = format!(
                        "invalid NDNDataRefererObject target format! {}, {}",
                        value, e
                    );
                    error!("{}", msg);
                    BuckyError::new(BuckyErrorCode::InvalidFormat, msg)
                })
                .and_then(|t| {
                    ObjectId::from_str(id_parts[1])
                        .map_err(|e| {
                            let msg = format!(
                                "invalid NDNDataRefererObject object_id format! {}, {}",
                                value, e
                            );
                            error!("{}", msg);
                            BuckyError::new(BuckyErrorCode::InvalidFormat, msg)
                        })
                        .map(|o| (Some(t), o))
                })
        } else {
            let msg = format!(
                "invalid NDNDataRefererObject, object_id not found! {}",
                value
            );
            error!("{}", msg);
            Err(BuckyError::new(BuckyErrorCode::InvalidFormat, msg))
        }?;

        let inner_path = if parts.len() > 1 {
            let inner_path = parts[1..].join("/");
            let inner_path = if inner_path != "/" {
                format!("/{}", inner_path)
            } else {
                inner_path
            };
            Some(inner_path)
        } else {
            None
        };

        Ok(Self {
            target,
            object_id,
            inner_path,
        })
    }
}

#[cfg(test)]
mod test {
    use crate::*;
    use cyfs_base::*;
    use std::str::FromStr;

    fn test_codec_impl(inner_path: &str) {
        let target = DeviceId::from_str("5aSixgPJLRApy31v15U8mM7cVSndc8kjbECXSfP9o6Ef").unwrap();
        let object_id = ObjectId::from_str("7jMmeXZcUvyomvtKAnfa8BCdCXCzdp1S5xavMsC7BcYJ").unwrap();

        let referer_object = NDNDataRefererObject {
            target: None,
            object_id: object_id.clone(),
            inner_path: Some(inner_path.to_owned()),
        };

        let s = referer_object.to_string();
        println!("{}", s);

        let referer_object2 = NDNDataRefererObject::from_str(&s).unwrap();
        assert_eq!(referer_object, referer_object2);

        let referer_object = NDNDataRefererObject {
            target: Some(target.object_id().to_owned()),
            object_id: object_id.clone(),
            inner_path: Some(inner_path.to_owned()),
        };

        let s = referer_object.to_string();
        println!("{}", s);

        let referer_object2 = NDNDataRefererObject::from_str(&s).unwrap();
        assert_eq!(referer_object, referer_object2);

        let referer_object = NDNDataRefererObject {
            target: None,
            object_id: object_id.clone(),
            inner_path: None,
        };

        let s = referer_object.to_string();
        println!("{}", s);

        let referer_object2 = NDNDataRefererObject::from_str(&s).unwrap();
        assert_eq!(referer_object, referer_object2);
    }

    #[test]
    fn test_codec() {
        let inner_path = "/a/b/c/ddd";
        test_codec_impl(inner_path);

        let inner_path = "/";
        test_codec_impl(inner_path);

        let inner_path = "/abcd";
        test_codec_impl(inner_path);
    }
}