Skip to main content

prolly/prolly/
tree.rs

1//! Tree structure for Prolly Trees
2
3use serde::{Deserialize, Serialize};
4
5use super::cid::Cid;
6use super::config::Config;
7
8/// A Prolly Tree handle
9#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
10pub struct Tree {
11    /// Root node CID (None if empty)
12    pub root: Option<Cid>,
13    /// Tree configuration
14    pub config: Config,
15}
16
17impl Tree {
18    /// Create a new empty tree with the given configuration
19    pub fn new(config: Config) -> Self {
20        Self { root: None, config }
21    }
22
23    /// Check if the tree is empty
24    pub fn is_empty(&self) -> bool {
25        self.root.is_none()
26    }
27}
28
29impl Default for Tree {
30    fn default() -> Self {
31        Self::new(Config::default())
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38
39    #[test]
40    fn test_tree_new() {
41        let tree = Tree::new(Config::default());
42        assert!(tree.is_empty());
43        assert!(tree.root.is_none());
44    }
45
46    #[test]
47    fn test_tree_default() {
48        let tree = Tree::default();
49        assert!(tree.is_empty());
50    }
51
52    #[test]
53    fn test_tree_with_root() {
54        let cid = Cid::from_bytes(b"test");
55        let tree = Tree {
56            root: Some(cid.clone()),
57            config: Config::default(),
58        };
59        assert!(!tree.is_empty());
60        assert_eq!(tree.root, Some(cid));
61    }
62}