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
use crate::*;

#[derive(Clone)]
pub enum NDNId {
    DirId(DirId),
    FileId(FileId),
    DiffId(DiffId),
}

#[derive(Clone)]
pub enum NDNObject {
    Dir(Dir),
    File(File),
    Diff(Diff),
}

impl NDNObject {
    pub fn dir(&self) -> BuckyResult<&Dir> {
        match self {
            NDNObject::Dir(d) => Ok(d),
            _ => Err(BuckyError::new(
                BuckyErrorCode::InvalidParam,
                "type is not Dir",
            )),
        }
    }

    pub fn file(&self) -> BuckyResult<&File> {
        match self {
            NDNObject::File(d) => Ok(d),
            _ => Err(BuckyError::new(
                BuckyErrorCode::ParseError,
                "type is not File",
            )),
        }
    }

    pub fn diff(&self) -> BuckyResult<&Diff> {
        match self {
            NDNObject::Diff(d) => Ok(d),
            _ => Err(BuckyError::new(
                BuckyErrorCode::ParseError,
                "type is not Diff",
            )),
        }
    }

    pub fn object_id(&self) -> ObjectId {
        match self {
            NDNObject::Dir(d) => d.desc().dir_id().object_id().clone(),
            NDNObject::File(d) => d.desc().file_id().object_id().clone(),
            NDNObject::Diff(d) => d.desc().diff_id().object_id().clone(),
        }
    }
}