arui_core/tree/
visible.rs1use crate::tree::node::TreeNode;
4use crate::tree::root::ProjectTree;
5use std::fmt::{Display, Formatter};
6
7pub trait ProjectTreeVisible {
14 fn show(&self);
15 fn print_tree(&self);
16 fn print_node(node: &TreeNode, depth: usize);
17}
18
19impl ProjectTreeVisible for ProjectTree {
22 fn show(&self) {
37 println!("Project Tree:");
38 println!("ID: {}", self.id);
39 println!("Valid: {}", self.is_valid());
40 println!("Name: {}", self.name);
41 println!("Path: {}", self.path);
42 }
43
44 fn print_tree(&self) {
46 if let Some(ref root) = self.root {
47 Self::print_node(root, 0);
48 } else {
49 println!("Tree is empty");
50 }
51 }
52
53 fn print_node(node: &TreeNode, depth: usize) {
55 let indent = " ".repeat(depth);
57
58 let node_type = if node.is_dir { "DIR" } else { "FILE" };
60 println!("{}- {} [{}]", indent, node.path, node_type);
61
62 if let Some(ref children) = node.children {
64 for child in children {
65 Self::print_node(child, depth + 1);
66 }
67 }
68 }
69}
70
71impl Display for ProjectTree {
72 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
74 write!(
75 f,
76 "ProjectTree {{\n\tID: {},\n\tValid: {},\n\tName: {},\n\tPath: {}\n}}",
77 self.id,
78 self.is_valid(),
79 self.name,
80 self.path
81 )
82 }
83}
84
85#[cfg(test)]
86mod tests {
87 use crate::tree::root::ProjectTree;
88 use crate::tree::visible::ProjectTreeVisible;
89
90 #[test]
91 fn test_show_project() {
92 let name = "test".to_string();
93 let path = ".".to_string();
94 let tree = ProjectTree::new(name, path, None);
95 tree.show();
96 print!("{:}", tree);
97 }
98
99 #[test]
100 fn test_show_project_tree() {
101 let name = "test".to_string();
102 let path = "./src".to_string();
103 let mut tree = ProjectTree::new(name, path, None);
104 tree.build().expect("panic");
105 tree.print_tree();
107 }
108}