I think I actually have a good foundation for a tree here.
This commit is contained in:
		
							parent
							
								
									44c6da936e
								
							
						
					
					
						commit
						607a48eb3a
					
				
					 1 changed files with 78 additions and 13 deletions
				
			
		| 
						 | 
					@ -1,22 +1,87 @@
 | 
				
			||||||
#![allow(unused)]
 | 
					#![allow(unused)]
 | 
				
			||||||
use std::{rc::{Weak, Rc}, cell::RefCell};
 | 
					use std::{
 | 
				
			||||||
 | 
					    cell::RefCell,
 | 
				
			||||||
 | 
					    ops::Deref,
 | 
				
			||||||
 | 
					    rc::{Rc, Weak},
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// #[derive(Debug)]
 | 
				
			||||||
 | 
					// pub struct NodeRef(Rc<RefCell<Node>>);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug)]
 | 
					#[derive(Debug)]
 | 
				
			||||||
pub struct Dir {
 | 
					pub struct FileRef(Rc<RefCell<File>>);
 | 
				
			||||||
    parent: Option<Weak<RefCell<Dir>>>,
 | 
					
 | 
				
			||||||
    name: String,
 | 
					#[derive(Debug)]
 | 
				
			||||||
    children: Vec<Rc<RefCell<FileOrDir>>>
 | 
					pub struct DirRef(Rc<RefCell<Dir>>);
 | 
				
			||||||
 | 
					#[derive(Debug)]
 | 
				
			||||||
 | 
					pub struct WeakDirRef(Weak<RefCell<Dir>>);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[derive(Debug)]
 | 
				
			||||||
 | 
					pub enum Node {
 | 
				
			||||||
 | 
					    Dir(DirRef),
 | 
				
			||||||
 | 
					    File(FileRef),
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug)]
 | 
					#[derive(Debug)]
 | 
				
			||||||
pub struct File {
 | 
					pub struct File {
 | 
				
			||||||
    dir: Weak<RefCell<Dir>>,
 | 
					    parent: WeakDirRef,
 | 
				
			||||||
    size: usize,
 | 
					    pub name: String,
 | 
				
			||||||
    name: String
 | 
					    pub size: usize,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug)]
 | 
					#[derive(Debug, Default)]
 | 
				
			||||||
pub enum FileOrDir{
 | 
					pub struct Dir {
 | 
				
			||||||
    File(File),
 | 
					    parent: Option<WeakDirRef>,
 | 
				
			||||||
    Dir(Dir),
 | 
					    pub name: String,
 | 
				
			||||||
 | 
					    children: Vec<Node>,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl DirRef {
 | 
				
			||||||
 | 
					    //impling it on the ref makes the recursion a bit easier.
 | 
				
			||||||
 | 
					    fn total_size(&self) -> usize {
 | 
				
			||||||
 | 
					        self.0
 | 
				
			||||||
 | 
					            .borrow()
 | 
				
			||||||
 | 
					            .deref()
 | 
				
			||||||
 | 
					            .children
 | 
				
			||||||
 | 
					            .iter()
 | 
				
			||||||
 | 
					            .map(|f| match f {
 | 
				
			||||||
 | 
					                Node::Dir(dir) => dir.total_size(),
 | 
				
			||||||
 | 
					                Node::File(file) => file.0.borrow().size,
 | 
				
			||||||
 | 
					            })
 | 
				
			||||||
 | 
					            .sum()
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    fn new(name: String) -> Self {
 | 
				
			||||||
 | 
					        DirRef(Rc::new(RefCell::new(Dir {
 | 
				
			||||||
 | 
					            parent: None,
 | 
				
			||||||
 | 
					            name,
 | 
				
			||||||
 | 
					            children: Vec::new(),
 | 
				
			||||||
 | 
					        })))
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    //needs to be impled on the ref because of the need to get a weak backreference to self.
 | 
				
			||||||
 | 
					    fn add(&self, node: Node) {
 | 
				
			||||||
 | 
					        match node {
 | 
				
			||||||
 | 
					            Node::Dir(ref dir) => {
 | 
				
			||||||
 | 
					                dir.0.borrow_mut().parent = Some(WeakDirRef(Rc::downgrade(&self.0)))
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            Node::File(ref file) => file.0.borrow_mut().parent = WeakDirRef(Rc::downgrade(&self.0)),
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        self.0.borrow_mut().children.push(node);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl File {
 | 
				
			||||||
 | 
					    fn get_parent(&self)-> &WeakDirRef {
 | 
				
			||||||
 | 
					        &self.parent
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl Dir {
 | 
				
			||||||
 | 
					    fn get_children(&self) -> &[Node] {
 | 
				
			||||||
 | 
					        &self.children
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn get_parent(&self) -> &Option<WeakDirRef> {
 | 
				
			||||||
 | 
					        &self.parent
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue