able_file_system/src/main.rs

74 lines
1.5 KiB
Rust

use {
id_tree::{
InsertBehavior::{AsRoot, UnderNode},
Node, NodeId, Tree, TreeBuilder,
},
spin::Mutex,
};
#[derive(Debug)]
pub struct File {
name: String,
size: u64,
}
impl File {
fn new(name: String) -> Self {
Self {
size: *&name.len() as u64,
name,
}
}
}
lazy_static::lazy_static! {
pub static ref FILE_TREE: Mutex<Tree<File>> = {
let tree: Tree<File> = TreeBuilder::new().build();
return Mutex::new(tree);
};
}
fn main() {
generate_tree();
let tree = FILE_TREE.lock();
let root_id = tree.root_node_id();
for x in tree.traverse_pre_order(&root_id.unwrap()).unwrap() {
println!("Parent: {:?}", x.data());
for y in x.children() {
println!(" Children: {:?}", tree.get(y).unwrap().data());
}
}
}
fn generate_tree() {
let root_id: NodeId = FILE_TREE
.lock()
.insert(Node::new(File::new("Hello".to_string())), AsRoot)
.unwrap();
let child_node = FILE_TREE
.lock()
.insert(
Node::new(File::new("World!".to_string())),
UnderNode(&root_id),
)
.unwrap();
FILE_TREE
.lock()
.insert(
Node::new(File::new("bonk!".to_string())),
UnderNode(&root_id),
)
.unwrap();
FILE_TREE
.lock()
.insert(
Node::new(File::new("Mouse".to_string())),
UnderNode(&child_node),
)
.unwrap();
}