use crate::tree::node::TreeNode;
use crate::tree::root::ProjectTree;
use std::fmt::{Display, Formatter};
pub trait ProjectTreeVisible {
fn show(&self);
fn print_tree(&self);
fn print_node(node: &TreeNode, depth: usize);
}
impl ProjectTreeVisible for ProjectTree {
fn show(&self) {
println!("Project Tree:");
println!("ID: {}", self.id);
println!("Valid: {}", self.is_valid());
println!("Name: {}", self.name);
println!("Path: {}", self.path);
}
fn print_tree(&self) {
if let Some(ref root) = self.root {
Self::print_node(root, 0);
} else {
println!("Tree is empty");
}
}
fn print_node(node: &TreeNode, depth: usize) {
let indent = " ".repeat(depth);
let node_type = if node.is_dir { "DIR" } else { "FILE" };
println!("{}- {} [{}]", indent, node.path, node_type);
if let Some(ref children) = node.children {
for child in children {
Self::print_node(child, depth + 1);
}
}
}
}
impl Display for ProjectTree {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"ProjectTree {{\n\tID: {},\n\tValid: {},\n\tName: {},\n\tPath: {}\n}}",
self.id,
self.is_valid(),
self.name,
self.path
)
}
}
#[cfg(test)]
mod tests {
use crate::tree::root::ProjectTree;
use crate::tree::visible::ProjectTreeVisible;
#[test]
fn test_show_project() {
let name = "test".to_string();
let path = ".".to_string();
let tree = ProjectTree::new(name, path, None);
tree.show();
print!("{:}", tree);
}
#[test]
fn test_show_project_tree() {
let name = "test".to_string();
let path = "./src".to_string();
let mut tree = ProjectTree::new(name, path, None);
tree.build().expect("panic");
tree.print_tree();
}
}