2023-10-31 23:53:43 -05:00
|
|
|
use std::{
|
|
|
|
cell::RefCell,
|
2023-11-11 22:07:57 -06:00
|
|
|
collections::{hash_map, HashMap},
|
2023-10-31 23:53:43 -05:00
|
|
|
ops::Deref,
|
|
|
|
rc::{Rc, Weak},
|
|
|
|
};
|
|
|
|
|
2023-11-11 22:07:57 -06:00
|
|
|
pub type WeakDirRef = Weak<RefCell<Dir>>;
|
|
|
|
|
|
|
|
pub trait FileLike {
|
|
|
|
fn get_parent(&self) -> Option<DirRef>;
|
|
|
|
fn get_name(&self) -> String;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2023-10-31 23:53:43 -05:00
|
|
|
pub struct DirRef(Rc<RefCell<Dir>>);
|
|
|
|
|
2023-11-11 22:07:57 -06:00
|
|
|
impl Deref for DirRef {
|
|
|
|
type Target = Rc<RefCell<Dir>>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
2022-12-07 01:26:48 -06:00
|
|
|
}
|
|
|
|
|
2023-11-11 22:07:57 -06:00
|
|
|
impl DirRef {
|
|
|
|
pub fn add_child_node(&mut self, mut node: Node) -> NodeRef {
|
|
|
|
node.change_parent(self);
|
|
|
|
let node_ref = NodeRef(Rc::new(RefCell::new(node)));
|
|
|
|
Rc::clone(self)
|
|
|
|
.borrow_mut()
|
|
|
|
.children
|
|
|
|
.push(NodeRef(Rc::clone(&node_ref)));
|
|
|
|
node_ref
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_child_dir(&mut self, mut dir: Dir) -> DirRef {
|
|
|
|
dir.parent = Some(Rc::downgrade(self));
|
|
|
|
let dir_ref = DirRef(Rc::new(RefCell::new(dir)));
|
|
|
|
Rc::clone(self).borrow_mut().children.push(
|
|
|
|
Node::Dir(())
|
|
|
|
);
|
|
|
|
todo!()
|
|
|
|
}
|
2023-10-31 23:53:43 -05:00
|
|
|
}
|
|
|
|
|
2023-11-11 22:07:57 -06:00
|
|
|
#[derive(Debug)]
|
2023-10-31 23:53:43 -05:00
|
|
|
pub struct Dir {
|
2023-11-11 22:07:57 -06:00
|
|
|
name: String,
|
2023-10-31 23:53:43 -05:00
|
|
|
parent: Option<WeakDirRef>,
|
2023-11-11 22:07:57 -06:00
|
|
|
children: Vec<NodeRef>,
|
2023-10-31 23:53:43 -05:00
|
|
|
}
|
|
|
|
|
2023-11-11 22:07:57 -06:00
|
|
|
impl Dir {
|
|
|
|
pub fn get_total_size(&self) -> usize {
|
|
|
|
self.children
|
2023-10-31 23:53:43 -05:00
|
|
|
.iter()
|
2023-11-11 22:07:57 -06:00
|
|
|
.map(|n| -> usize {
|
|
|
|
match Rc::clone(n).borrow().deref() {
|
|
|
|
Node::Dir(d) => d.get_total_size(),
|
|
|
|
Node::File(f) => f.size,
|
|
|
|
}
|
2023-10-31 23:53:43 -05:00
|
|
|
})
|
|
|
|
.sum()
|
|
|
|
}
|
2023-11-01 18:58:39 -05:00
|
|
|
|
2023-11-11 22:07:57 -06:00
|
|
|
pub fn get_children(&self) -> impl Iterator<Item = NodeRef> + '_ {
|
|
|
|
self.children.iter().map(|n| NodeRef(Rc::clone(n)))
|
2023-10-31 23:53:43 -05:00
|
|
|
}
|
2023-11-11 22:07:57 -06:00
|
|
|
}
|
2023-10-31 23:53:43 -05:00
|
|
|
|
2023-11-11 22:07:57 -06:00
|
|
|
impl FileLike for Dir {
|
|
|
|
fn get_parent(&self) -> Option<DirRef> {
|
|
|
|
Some(DirRef(self.parent.as_ref()?.upgrade()?))
|
2023-11-01 18:58:39 -05:00
|
|
|
}
|
2023-11-11 22:07:57 -06:00
|
|
|
fn get_name(&self) -> String {
|
|
|
|
self.name.clone()
|
2023-10-31 23:53:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-11 22:07:57 -06:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct NodeRef(Rc<RefCell<Node>>);
|
2023-10-31 23:53:43 -05:00
|
|
|
|
2023-11-11 22:07:57 -06:00
|
|
|
impl Deref for NodeRef {
|
|
|
|
type Target = Rc<RefCell<Node>>;
|
2023-10-31 23:53:43 -05:00
|
|
|
|
2023-11-11 22:07:57 -06:00
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
2023-10-31 23:53:43 -05:00
|
|
|
}
|
2022-12-07 01:26:48 -06:00
|
|
|
}
|
2023-11-01 18:58:39 -05:00
|
|
|
|
2023-11-11 22:07:57 -06:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Node {
|
|
|
|
Dir(Dir),
|
|
|
|
File(File),
|
|
|
|
}
|
|
|
|
|
2023-11-01 18:58:39 -05:00
|
|
|
impl Node {
|
2023-11-11 22:07:57 -06:00
|
|
|
fn change_parent(&mut self, new_parent: &DirRef) {
|
2023-11-01 18:58:39 -05:00
|
|
|
match self {
|
2023-11-11 22:07:57 -06:00
|
|
|
Node::Dir(d) => d.parent = Some(Rc::downgrade(new_parent)),
|
|
|
|
Node::File(f) => f.parent = Rc::downgrade(new_parent),
|
2023-11-01 18:58:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-11 22:07:57 -06:00
|
|
|
impl FileLike for Node {
|
|
|
|
fn get_parent(&self) -> Option<DirRef> {
|
|
|
|
match self {
|
|
|
|
Node::Dir(d) => d.get_parent(),
|
|
|
|
Node::File(f) => f.get_parent(),
|
|
|
|
}
|
2023-11-01 18:58:39 -05:00
|
|
|
}
|
2023-11-11 22:07:57 -06:00
|
|
|
fn get_name(&self) -> String {
|
2023-11-01 18:58:39 -05:00
|
|
|
match self {
|
2023-11-11 22:07:57 -06:00
|
|
|
Node::Dir(d) => d.get_name(),
|
|
|
|
Node::File(f) => f.get_name(),
|
2023-11-01 18:58:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-11 22:07:57 -06:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct File {
|
|
|
|
name: String,
|
|
|
|
parent: WeakDirRef,
|
|
|
|
pub size: usize,
|
2023-11-01 18:58:39 -05:00
|
|
|
}
|
|
|
|
|
2023-11-11 22:07:57 -06:00
|
|
|
impl FileLike for File {
|
|
|
|
fn get_parent(&self) -> Option<DirRef> {
|
|
|
|
Some(DirRef(self.parent.upgrade()?))
|
|
|
|
}
|
|
|
|
fn get_name(&self) -> String {
|
|
|
|
self.name.clone()
|
2023-11-01 18:58:39 -05:00
|
|
|
}
|
|
|
|
}
|