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
use super::{Entry, Leaf};
use crate::Metadata;
use alloc::collections::btree_map::Entry::*;
use alloc::collections::BTreeMap;

pub(super) struct DuplicateName;
pub(super) struct FoundLeaf;

/// Node in a directory tree.
#[derive(Debug)]
pub(super) struct DirBuilder {
    /// Immediate files, symlinks or directories in this directory
    pub nodes: BTreeMap<String, Entry>,
    /// Metadata for this directory
    metadata: Metadata,
    /// Id of the parent; None for the root node
    pub parent_id: Option<u64>,
    /// Internal id, used for propagating Cids back from children during post order visit.
    pub id: u64,
}

impl DirBuilder {
    pub fn new(parent_id: u64, id: u64) -> Self {
        assert_ne!(parent_id, id);
        DirBuilder {
            nodes: Default::default(),
            metadata: Default::default(),
            parent_id: Some(parent_id),
            id,
        }
    }

    pub fn root(id: u64) -> Self {
        DirBuilder {
            nodes: Default::default(),
            metadata: Default::default(),
            parent_id: None,
            id,
        }
    }

    pub fn put_leaf(&mut self, key: String, leaf: Leaf) -> Result<(), DuplicateName> {
        match self.nodes.entry(key) {
            Occupied(_) => Err(DuplicateName),
            Vacant(ve) => {
                ve.insert(Entry::Leaf(leaf));
                Ok(())
            }
        }
    }

    pub fn add_or_get_node(
        &mut self,
        key: String,
        id: &mut Option<u64>,
    ) -> Result<&mut DirBuilder, FoundLeaf> {
        match self.nodes.entry(key) {
            Occupied(oe) => oe.into_mut().as_dir_builder().map_err(|_| FoundLeaf),
            Vacant(ve) => {
                let id = id.take().unwrap();
                let entry = ve.insert(Entry::Directory(Self::new(self.id, id)));
                Ok(entry.as_dir_builder().expect("just inserted"))
            }
        }
    }

    pub fn len(&self) -> usize {
        self.nodes.len()
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn set_metadata(&mut self, metadata: Metadata) {
        self.metadata = metadata;
    }
}