workspacer_tree/
workspace_tree_node.rs

1// ---------------- [ File: workspacer-tree/src/workspace_tree_node.rs ]
2crate::ix!();
3
4#[derive(Debug)]
5pub struct WorkspaceTreeNode {
6    crate_name:   String,
7    crate_version: Option<SemverVersion>,
8    crate_path:   PathBuf,
9    children:     Vec<WorkspaceTreeNode>,
10}
11
12impl WorkspaceTreeNode {
13    /// Internal constructor for an individual node.
14    pub fn new(
15        crate_name: String,
16        crate_version: Option<SemverVersion>,
17        crate_path: PathBuf,
18    ) -> Self {
19        Self {
20            crate_name,
21            crate_version,
22            crate_path,
23            children: Vec::new(),
24        }
25    }
26
27    /// Recursively push child nodes
28    pub fn add_child(&mut self, child: WorkspaceTreeNode) {
29        self.children.push(child);
30    }
31
32    /// Recursively renders `self` into the output string with indentation.
33    /// `level` controls how many spaces to indent. 
34    pub fn render_recursive(
35        &self,
36        level: usize,
37        show_version: bool,
38        show_path: bool,
39        out: &mut String,
40    ) {
41        // Indent with 2 spaces * level
42        let indent = "  ".repeat(level);
43
44        // e.g. "my_crate (v1.2.3)  [/some/path]"
45        let mut line = format!("{}{}", indent, self.crate_name);
46
47        if show_version {
48            if let Some(ref v) = self.crate_version {
49                line.push_str(&format!(" (v{})", v));
50            }
51        }
52
53        if show_path {
54            line.push_str(&format!("  [{}]", self.crate_path.display()));
55        }
56
57        out.push_str(&line);
58        out.push('\n');
59
60        // Recurse on children
61        for child in &self.children {
62            child.render_recursive(level + 1, show_version, show_path, out);
63        }
64    }
65}