cyfs_lib/traversal/object/
def.rs1use crate::*;
2use cyfs_base::*;
3
4use std::sync::Arc;
5
6#[derive(Debug, Clone, Eq, PartialEq)]
7pub enum NormalObjectPostion {
8 Middle,
9 Leaf,
10 Assoc,
11}
12
13#[derive(Debug)]
14pub struct NormalObject {
15 pub pos: NormalObjectPostion,
16 pub object: NONSlimObjectInfo,
17 pub path: String,
18 pub config_ref_depth: u32,
19 pub ref_depth: u32,
20}
21
22impl NormalObject {
23 pub fn dervie_path(path: &str, key: &str) -> String {
24 if path.ends_with("/") {
25 format!("{}{}", path, key)
26 } else {
27 format!("{}/{}", path, key)
28 }
29 }
30
31 pub fn derive_normal(
32 &self,
33 object_id: ObjectId,
34 key: Option<&str>,
35 is_ref_object: bool,
36 ) -> Self {
37 let pos = match object_id.obj_type_code() {
38 ObjectTypeCode::ObjectMap => NormalObjectPostion::Middle,
39 _ => match self.pos {
40 NormalObjectPostion::Middle => NormalObjectPostion::Leaf,
41 NormalObjectPostion::Leaf | NormalObjectPostion::Assoc => {
42 NormalObjectPostion::Assoc
43 }
44 },
45 };
46
47 let path = match key {
48 Some(key) => Self::dervie_path(&self.path, key),
49 None => self.path.clone(),
50 };
51
52 let ref_depth = if is_ref_object {
53 self.ref_depth + 1
54 } else {
55 self.ref_depth
56 };
57
58 Self {
59 pos,
60 object: NONSlimObjectInfo::new(object_id, None, None),
61 path,
62 config_ref_depth: self.config_ref_depth,
63 ref_depth,
64 }
65 }
66}
67
68#[derive(Debug)]
69pub struct SubObject {
70 pub object_id: ObjectId,
71}
72
73#[derive(Debug)]
74pub enum TraverseObjectItem {
75 Normal(NormalObject),
76 Sub(SubObject),
77}
78
79impl TraverseObjectItem {
80 pub fn object_id(&self) -> &ObjectId {
81 match self {
82 Self::Normal(item) => &item.object.object_id,
83 Self::Sub(item) => &item.object_id,
84 }
85 }
86}
87
88pub struct TraverseChunkItem {
89 pub chunk_id: ChunkId,
90}
91
92#[async_trait::async_trait]
93pub trait ObjectTraverserCallBack: Send + Sync {
94 async fn on_object(&self, item: TraverseObjectItem) -> BuckyResult<()>;
95 async fn on_chunk(&self, item: TraverseChunkItem) -> BuckyResult<()>;
96
97 async fn on_error(&self, id: &ObjectId, e: BuckyError) -> BuckyResult<()>;
98 async fn on_missing(&self, id: &ObjectId) -> BuckyResult<()>;
99}
100
101pub type ObjectTraverserCallBackRef = Arc<Box<dyn ObjectTraverserCallBack>>;