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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
use crate::*;

#[derive(Debug, Clone)]
pub enum AnyNamedObject {
    Standard(StandardObject),
    Core(TypelessCoreObject),
    DECApp(TypelessDECAppObject),
}

#[macro_export]
macro_rules! match_any_obj {
    ($on:ident, $o:ident, $body:tt, $chunk_id:ident, $chunk_body:tt) => {
        match $on {
            AnyNamedObject::Standard(o) => match o {
                StandardObject::Device($o) => $body,
                StandardObject::People($o) => $body,
                StandardObject::SimpleGroup($o) => $body,
                StandardObject::Org($o) => $body,
                StandardObject::AppGroup($o) => $body,
                StandardObject::UnionAccount($o) => $body,
                StandardObject::ChunkId($chunk_id) => $chunk_body,
                StandardObject::File($o) => $body,
                StandardObject::Dir($o) => $body,
                StandardObject::Diff($o) => $body,
                StandardObject::ProofOfService($o) => $body,
                StandardObject::Tx($o) => $body,
                StandardObject::Action($o) => $body,
                StandardObject::ObjectMap($o) => $body,
                StandardObject::Contract($o) => $body,
            },
            AnyNamedObject::Core($o) => $body,
            AnyNamedObject::DECApp($o) => $body,
        }
    };
}

impl AnyNamedObject {
    pub fn object_id(&self) -> ObjectId {
        self.calculate_id()
    }
    pub fn calculate_id(&self) -> ObjectId {
        match_any_obj!(self, o, { o.desc().calculate_id() }, chunk_id, {
            chunk_id.object_id()
        })
    }

    pub fn try_clone(&self) -> BuckyResult<Self> {
        let len = self.raw_measure(&None).map_err(|e| {
            log::error!("AnyNamedObject::try_clone/raw_measure error:{}", e);
            e
        })?;

        let mut buf = Vec::with_capacity(len);
        unsafe {
            buf.set_len(len);
        }

        self.raw_encode(&mut buf[..], &None).map_err(|e| {
            log::error!("AnyNamedObject::try_clone/raw_encode error:{}", e);
            e
        })?;

        let (ret, _) = Self::raw_decode(&buf[..]).map_err(|e| {
            log::error!("AnyNamedObject::try_clone/raw_decode error:{}", e);
            e
        })?;

        Ok(ret)
    }

    pub fn obj_type(&self) -> u16 {
        match_any_obj!(self, o, { o.desc().obj_type() }, _chunk_id, {
            ObjectTypeCode::Chunk.to_u16()
        })
    }

    pub fn obj_type_code(&self) -> ObjectTypeCode {
        match_any_obj!(self, o, { o.desc().obj_type_code() }, _chunk_id, {
            ObjectTypeCode::Chunk
        })
    }

    pub fn dec_id(&self) -> &Option<ObjectId> {
        match_any_obj!(self, o, { o.desc().dec_id() }, _chunk_id, { &None })
    }

    pub fn owner(&self) -> &Option<ObjectId> {
        match self {
            AnyNamedObject::Standard(s) => s.owner(),
            AnyNamedObject::Core(c) => c.desc().owner(),
            AnyNamedObject::DECApp(d) => d.desc().owner(),
        }
    }

    pub fn public_key(&self) -> Option<PublicKeyRef> {
        match self {
            AnyNamedObject::Standard(s) => s.public_key(),
            AnyNamedObject::Core(o) => o.desc().public_key(),
            AnyNamedObject::DECApp(o) => o.desc().public_key(),
        }
    }

    pub fn author(&self) -> &Option<ObjectId> {
        match self {
            AnyNamedObject::Standard(s) => s.author(),
            AnyNamedObject::Core(c) => c.desc().author(),
            AnyNamedObject::DECApp(d) => d.desc().author(),
        }
    }

    pub fn prev(&self) -> &Option<ObjectId> {
        match self {
            AnyNamedObject::Standard(s) => s.prev(),
            AnyNamedObject::Core(c) => c.desc().prev(),
            AnyNamedObject::DECApp(d) => d.desc().prev(),
        }
    }

    pub fn ood_list(&self) -> BuckyResult<&Vec<DeviceId>> {
        match self {
            AnyNamedObject::Standard(s) => s.ood_list(),
            AnyNamedObject::Core(_c) => Err(BuckyError::new(
                BuckyErrorCode::NotSupport,
                "ood_list not support in typeless Core object",
            )),
            AnyNamedObject::DECApp(_d) => Err(BuckyError::new(
                BuckyErrorCode::NotSupport,
                "ood_list not support in typeless DECApp object",
            )),
        }
    }

    pub fn ood_work_mode(&self) -> BuckyResult<OODWorkMode> {
        match self {
            AnyNamedObject::Standard(s) => s.ood_work_mode(),
            AnyNamedObject::Core(_c) => Err(BuckyError::new(
                BuckyErrorCode::NotSupport,
                "ood_work_mode not support in typeless Core object",
            )),
            AnyNamedObject::DECApp(_d) => Err(BuckyError::new(
                BuckyErrorCode::NotSupport,
                "ood_work_mode not support in typeless DECApp object",
            )),
        }
    }

    pub fn signs(&self) -> Option<&ObjectSigns> {
        match_any_obj!(self, o, { Some(o.signs()) }, chunk_id, {
            error!("chunk has no signs: {}", chunk_id);

            None
        })
    }

    pub fn signs_mut(&mut self) -> Option<&mut ObjectSigns> {
        match_any_obj!(self, o, { Some(o.signs_mut()) }, chunk_id, {
            error!("chunk has no signs: {}", chunk_id);

            None
        })
    }

    pub fn desc_hash(&self) -> BuckyResult<HashValue> {
        match_any_obj!(self, o, { o.desc().raw_hash_value() }, chunk_id, {
            let msg = format!("chunk has no desc: {}", chunk_id);
            error!("{}", msg);
            Err(BuckyError::new(BuckyErrorCode::NotSupport, msg))
        })
    }

    pub fn has_body(&self) -> BuckyResult<bool> {
        match_any_obj!(self, o, { Ok(o.body().is_some()) }, _chunk_id, {
            let msg = format!("chunk has no body: {}", self.calculate_id());
            error!("{}", msg);
            Err(BuckyError::new(BuckyErrorCode::NotSupport, msg))
        })
    }

    pub fn body_hash(&self) -> BuckyResult<Option<HashValue>> {
        match_any_obj!(
            self,
            o,
            {
                if o.body().is_some() {
                    let hash_value = o.body().as_ref().unwrap().raw_hash_value()?;
                    Ok(Some(hash_value))
                } else {
                    Ok(None)
                }
            },
            _chunk_id,
            {
                let msg = format!("chunk has no body: {}", self.calculate_id());
                error!("{}", msg);
                Err(BuckyError::new(BuckyErrorCode::NotSupport, msg))
            }
        )
    }

    pub fn ref_objs(&self) -> Option<&Vec<ObjectLink>> {
        match_any_obj!(self, o, { o.desc().ref_objs().as_ref() }, chunk_id, {
            error!("chunk has no ref_objs: {}", chunk_id);

            None
        })
    }

    pub fn is_standard(&self) -> bool {
        match self {
            AnyNamedObject::Standard(_) => true,
            _ => false,
        }
    }

    pub fn is_core(&self) -> bool {
        match self {
            AnyNamedObject::Core(_) => true,
            _ => false,
        }
    }

    pub fn is_dec(&self) -> bool {
        match self {
            AnyNamedObject::DECApp(_) => true,
            _ => false,
        }
    }

    // 设置对象body的修改时间
    pub fn set_body_update_time(&mut self, time: u64) {
        match_any_obj!(
            self,
            o,
            {
                match o.body_mut().as_mut() {
                    Some(body) => body.set_update_time(time),
                    None => {}
                }
            },
            _chunk_id,
            {}
        )
    }

    pub fn create_time(&self) -> u64 {
        match_any_obj!(self, o, { o.desc().create_time() }, _chunk_id, { 0 })
    }

    pub fn expired_time(&self) -> Option<u64> {
        match_any_obj!(self, o, { o.desc().expired_time() }, _chunk_id, { None })
    }

    pub fn update_time(&self) -> Option<u64> {
        match_any_obj!(
            self,
            o,
            {
                match o.body().as_ref() {
                    Some(body) => Some(body.update_time()),
                    None => None,
                }
            },
            _chunk_id,
            { None }
        )
    }

    // 获取对象body的修改时间(不包括签名)
    pub fn get_update_time(&self) -> u64 {
        match_any_obj!(
            self,
            o,
            {
                match o.body().as_ref() {
                    Some(body) => body.update_time(),
                    None => 0_u64,
                }
            },
            _chunk_id,
            { 0 }
        )
    }

    // 获取body+signs的最新修改时间
    pub fn get_full_update_time(&self) -> u64 {
        let update_time = self.get_update_time();

        // 如果签名时间比较新,那么取签名时间
        let latest_sign_time = match self.signs() {
            Some(v) => v.latest_sign_time(),
            None => 0,
        };

        std::cmp::max(update_time, latest_sign_time)
    }
}


impl RawEncode for AnyNamedObject {
    fn raw_measure(&self, purpose: &Option<RawEncodePurpose>) -> BuckyResult<usize> {
        match self {
            AnyNamedObject::Standard(obj) => obj.raw_measure(purpose),
            AnyNamedObject::Core(obj) => obj.raw_measure(purpose),
            AnyNamedObject::DECApp(obj) => obj.raw_measure(purpose),
        }
    }

    fn raw_encode<'a>(
        &self,
        buf: &'a mut [u8],
        purpose: &Option<RawEncodePurpose>,
    ) -> BuckyResult<&'a mut [u8]> {
        match self {
            AnyNamedObject::Standard(obj) => obj.raw_encode(buf, purpose),
            AnyNamedObject::Core(obj) => obj.raw_encode(buf, purpose),
            AnyNamedObject::DECApp(obj) => obj.raw_encode(buf, purpose),
        }
    }
}

impl<'de> RawDecode<'de> for AnyNamedObject {
    fn raw_decode(buf: &'de [u8]) -> Result<(Self, &'de [u8]), BuckyError> {
        let (obj_type_info, _new_buffer) = NamedObjectContext::raw_decode(buf).map_err(|e| {
            log::error!("AnyNamedObject::raw_decode/obj_type_info error:{}", e);
            e
        })?;

        match obj_type_info.obj_type_code() {
            ObjectTypeCode::Device => {
                let (device, buf) = Device::raw_decode(buf).map_err(|e| {
                    log::error!("AnyNamedObject::raw_decode/device error:{}", e);
                    e
                })?;
                return Ok((
                    AnyNamedObject::Standard(StandardObject::Device(device)),
                    buf,
                ));
            }
            ObjectTypeCode::People => {
                let (people, buf) = People::raw_decode(buf).map_err(|e| {
                    log::error!("AnyNamedObject::raw_decode/people error:{}", e);
                    e
                })?;
                return Ok((
                    AnyNamedObject::Standard(StandardObject::People(people)),
                    buf,
                ));
            }
            ObjectTypeCode::Org => {
                let (org, buf) = Org::raw_decode(buf).map_err(|e| {
                    log::error!("AnyNamedObject::raw_decode/org error:{}", e);
                    e
                })?;
                return Ok((AnyNamedObject::Standard(StandardObject::Org(org)), buf));
            }
            ObjectTypeCode::AppGroup => {
                let (app_group, buf) = AppGroup::raw_decode(buf).map_err(|e| {
                    log::error!("AnyNamedObject::raw_decode/app_group error:{}", e);
                    e
                })?;
                return Ok((
                    AnyNamedObject::Standard(StandardObject::AppGroup(app_group)),
                    buf,
                ));
            }
            ObjectTypeCode::SimpleGroup => {
                let (simple_group, buf) = SimpleGroup::raw_decode(buf).map_err(|e| {
                    log::error!("AnyNamedObject::raw_decode/simple_group error:{}", e);
                    e
                })?;
                return Ok((
                    AnyNamedObject::Standard(StandardObject::SimpleGroup(simple_group)),
                    buf,
                ));
            }
            ObjectTypeCode::UnionAccount => {
                let (ua, buf) = UnionAccount::raw_decode(buf).map_err(|e| {
                    log::error!("AnyNamedObject::raw_decode/ua error:{}", e);
                    e
                })?;
                return Ok((
                    AnyNamedObject::Standard(StandardObject::UnionAccount(ua)),
                    buf,
                ));
            }
            ObjectTypeCode::Chunk => {
                unreachable!();
            }
            ObjectTypeCode::File => {
                let (file, buf) = File::raw_decode(buf).map_err(|e| {
                    log::error!("AnyNamedObject::raw_decode/file error:{}", e);
                    e
                })?;
                return Ok((AnyNamedObject::Standard(StandardObject::File(file)), buf));
            }
            ObjectTypeCode::Dir => {
                let (dir, buf) = Dir::raw_decode(buf).map_err(|e| {
                    log::error!("AnyNamedObject::raw_decode/dir error:{}", e);
                    e
                })?;
                return Ok((AnyNamedObject::Standard(StandardObject::Dir(dir)), buf));
            }
            ObjectTypeCode::Diff => {
                let (diff, buf) = Diff::raw_decode(buf).map_err(|e| {
                    log::error!("AnyNamedObject::raw_decode/diff error:{}", e);
                    e
                })?;
                return Ok((AnyNamedObject::Standard(StandardObject::Diff(diff)), buf));
            }
            ObjectTypeCode::ProofOfService => {
                let (prof, buf) = ProofOfService::raw_decode(buf).map_err(|e| {
                    log::error!("AnyNamedObject::raw_decode/prof error:{}", e);
                    e
                })?;
                return Ok((
                    AnyNamedObject::Standard(StandardObject::ProofOfService(prof)),
                    buf,
                ));
            }
            ObjectTypeCode::Tx => {
                let (tx, buf) = Tx::raw_decode(buf).map_err(|e| {
                    log::error!("AnyNamedObject::raw_decode/tx error:{}", e);
                    e
                })?;
                return Ok((AnyNamedObject::Standard(StandardObject::Tx(tx)), buf));
            }
            ObjectTypeCode::Action => {
                let (action, buf) = Action::raw_decode(buf).map_err(|e| {
                    log::error!("AnyNamedObject::raw_decode/action error:{}", e);
                    e
                })?;
                return Ok((
                    AnyNamedObject::Standard(StandardObject::Action(action)),
                    buf,
                ));
            }
            ObjectTypeCode::ObjectMap => {
                let (relation, buf) = ObjectMap::raw_decode(buf).map_err(|e| {
                    log::error!("AnyNamedObject::raw_decode/relation error:{}", e);
                    e
                })?;
                return Ok((
                    AnyNamedObject::Standard(StandardObject::ObjectMap(relation)),
                    buf,
                ));
            }
            ObjectTypeCode::Contract => {
                let (contract, buf) = Contract::raw_decode(buf).map_err(|e| {
                    log::error!("AnyNamedObject::raw_decode/contract error:{}", e);
                    e
                })?;
                return Ok((
                    AnyNamedObject::Standard(StandardObject::Contract(contract)),
                    buf,
                ));
            }
            ObjectTypeCode::Custom => {
                return if obj_type_info.is_decapp_object() {
                    // println!("is dec app object");

                    let (dec_obj, buf) = TypelessDECAppObject::raw_decode(buf)?;
                    Ok((AnyNamedObject::DECApp(dec_obj), buf))
                } else {
                    // println!("is core object");

                    let (core_obj, buf) = TypelessCoreObject::raw_decode(buf)?;
                    Ok((AnyNamedObject::Core(core_obj), buf))
                };
            }
        }
    }
}

// 用 base16 hex实现serde
use serde::{
    de::{self, Visitor},
    Deserialize, Deserializer, Serialize, Serializer,
};

impl Serialize for AnyNamedObject {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let len = self.raw_measure(&None).unwrap();
        let mut buf = vec![0u8; len];
        self.raw_encode(buf.as_mut_slice(), &None).unwrap();
        serializer.serialize_str(&hex::encode(buf))
    }
}

impl<'de> Deserialize<'de> for AnyNamedObject {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct RawObjectIdVisitor;
        impl<'de> Visitor<'de> for RawObjectIdVisitor {
            type Value = AnyNamedObject;
            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                write!(formatter, "{}", "an ObjectId")
            }
            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                let raw = hex::decode(v)
                    .map_err(|err| E::custom(err.to_string()))
                    .map_err(|e| {
                        log::error!("AnyNamedObject::Deserialize error:{}", e);
                        e
                    })?;
                AnyNamedObject::raw_decode(raw.as_slice())
                    .map_err(|err| E::custom(err.to_string()))
                    .map(|(obj, _)| obj)
            }
        }
        deserializer.deserialize_str(RawObjectIdVisitor)
    }
}

use std::sync::Arc;
impl Into<AnyNamedObject> for Arc<AnyNamedObject> {
    fn into(self) -> AnyNamedObject {
        match Arc::try_unwrap(self) {
            Ok(v) => v,
            Err(v) => v.as_ref().clone(),
        }
    }
}

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

    #[test]
    fn test_any() {
        let mut sn_list = Vec::new();
        let mut endpoints = Vec::new();
        let unique_id = UniqueId::default();
        let name = "test_device";
        let owner = ObjectId::from_str("5aSixgLtjoYcAFH9isc6KCqDgKfTJ8jpgASAoiRz5NLk").unwrap();

        let ep = Endpoint::from_str("W4udp120.24.6.201:8060").unwrap();
        for _ in 0..10 {
            endpoints.push(ep.clone());
        }
        let device_id2 =
            DeviceId::from_str("5aSixgPXvhR4puWzFCHqvUXrjFWjxbq4y3thJVgZg6ty").unwrap();
        for _ in 0..10 {
            sn_list.push(device_id2.clone());
        }
        let desc_content = DeviceDescContent::new(unique_id.clone());

        let body_content =
            DeviceBodyContent::new(endpoints, sn_list, Vec::new(), Some(name.to_owned()), None);
        let secret1 = PrivateKey::generate_rsa(1024).unwrap();
        let public_key = secret1.public();

        let device = DeviceBuilder::new(desc_content, body_content)
            .public_key(public_key.clone())
            //.area(area.clone().unwrap())
            .owner(owner.clone())
            .build();

        let device_id = device.desc().device_id().object_id().to_owned();

        let buf = device.to_vec().unwrap();
        let (obj, _buf) = AnyNamedObject::raw_decode(&buf).unwrap();
        println!("{:?}", obj.owner().unwrap());
        assert_eq!(obj.owner().to_owned().unwrap(), owner);
        assert_eq!(obj.calculate_id(), device_id);
        let pk = obj.public_key().unwrap();
        if let PublicKeyRef::Single(key) = pk {
            assert_eq!(*key, public_key);
        } else {
            unreachable!();
        }

        let buf2 = obj.to_vec().unwrap();
        assert_eq!(buf.len(), buf2.len());
        assert_eq!(buf, buf2);
    }
}



macro_rules! any_for_standard_target {
    ($as_name:ident, $into_name:ident, $target:ident) => {
        impl AnyNamedObject {
            pub fn $as_name(&self) -> &$target {
                match self {
                    AnyNamedObject::Standard(s) => {
                        match s {
                            StandardObject::$target(f) => f,
                            _ => unreachable!(),
                        }
                    }
                    _ => unreachable!(),
                }
            }
            pub fn $into_name(self) -> $target {
                match self {
                    AnyNamedObject::Standard(s) => {
                        match s {
                            StandardObject::$target(f) => f,
                            _ => unreachable!(),
                        }
                    }
                    _ => unreachable!(),
                }
            }
        }
    }
}

any_for_standard_target!(as_file, into_file, File);
any_for_standard_target!(as_dir, into_dir, Dir);
any_for_standard_target!(as_people, into_people, People);
any_for_standard_target!(as_device, into_device, Device);
any_for_standard_target!(as_simple_group, into_simple_group, SimpleGroup);
any_for_standard_target!(as_object_map, into_object_map, ObjectMap);