cyfs_lib/traversal/object/
common.rs

1use super::def::*;
2use cyfs_base::*;
3
4pub struct CommonObjectTraverser {
5    current: NormalObject,
6    cb: ObjectTraverserCallBackRef,
7}
8
9impl CommonObjectTraverser {
10    pub fn new(current: NormalObject, cb: ObjectTraverserCallBackRef) -> Self {
11        Self { current, cb }
12    }
13
14    pub fn finish(self) -> NormalObject {
15        self.current
16    }
17
18    pub async fn tranverse(&self) -> BuckyResult<()> {
19        let object = self.current.object.object.as_ref().unwrap();
20        if let Some(id) = object.owner() {
21            self.append_item(id).await?;
22        }
23
24        if let Some(id) = object.prev() {
25            self.append_item(id).await?;
26        }
27
28        if let Some(id) = object.author() {
29            self.append_item(id).await?;
30        }
31
32        if let Some(dec_id) = object.dec_id() {
33            self.append_item(dec_id).await?;
34        }
35
36        if let Some(ref_list) = object.ref_objs() {
37            for link in ref_list {
38                self.append_link(link).await?;
39            }
40        }
41
42        if let Some(signs) = object.signs() {
43            if let Some(signs) = signs.body_signs() {
44                for sign in signs.iter() {
45                    self.append_sign(sign).await?;
46                }
47            }
48            if let Some(signs) = signs.desc_signs() {
49                for sign in signs.iter() {
50                    self.append_sign(sign).await?;
51                }
52            }
53        }
54
55        Ok(())
56    }
57
58    async fn append_sign(&self, sign: &Signature) -> BuckyResult<()> {
59        match sign.sign_source() {
60            SignatureSource::Object(link) => self.append_link(link).await,
61            _ => Ok(()),
62        }
63    }
64
65    async fn append_link(&self, link: &ObjectLink) -> BuckyResult<()> {
66        self.append_item(&link.obj_id).await?;
67
68        if let Some(ref owner) = link.obj_owner {
69            self.append_item(&owner).await?;
70        }
71
72        Ok(())
73    }
74
75    async fn append_item(&self, id: &ObjectId) -> BuckyResult<()> {
76        match id.obj_type_code() {
77            ObjectTypeCode::Chunk => {
78                let item = TraverseChunkItem {
79                    chunk_id: id.as_chunk_id().to_owned(),
80                };
81                self.cb.on_chunk(item).await
82            }
83            _ => {
84                if id.is_data() {
85                    return Ok(());
86                }
87
88                let obj = self.current.derive_normal(id.to_owned(), None, true);
89                let item = TraverseObjectItem::Normal(obj);
90                self.cb.on_object(item).await
91            }
92        }
93    }
94}