2023-10-31 23:53:43 -05:00
|
|
|
use std::{
|
|
|
|
cell::RefCell,
|
|
|
|
ops::Deref,
|
|
|
|
rc::{Rc, Weak},
|
|
|
|
};
|
|
|
|
|
2023-11-12 18:27:48 -06:00
|
|
|
use thiserror::Error;
|
2023-11-11 22:07:57 -06:00
|
|
|
|
2023-11-12 18:27:48 -06:00
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum FileTreeError {
|
|
|
|
#[error("Directory operation on file")]
|
|
|
|
IsFile,
|
|
|
|
#[error("File operation on directory")]
|
|
|
|
IsDir,
|
2023-11-11 22:07:57 -06:00
|
|
|
}
|
|
|
|
|
2023-11-12 18:27:48 -06:00
|
|
|
type WeakNodeRef = Weak<RefCell<Node>>;
|
|
|
|
|
2023-11-11 22:07:57 -06:00
|
|
|
#[derive(Debug)]
|
2023-11-12 18:27:48 -06:00
|
|
|
pub struct NodeRef(Rc<RefCell<Node>>);
|
2023-10-31 23:53:43 -05:00
|
|
|
|
2023-11-12 18:27:48 -06:00
|
|
|
impl Deref for NodeRef {
|
|
|
|
type Target = Rc<RefCell<Node>>;
|
2023-11-11 22:07:57 -06:00
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
2022-12-07 01:26:48 -06:00
|
|
|
}
|
|
|
|
|
2023-11-12 18:27:48 -06:00
|
|
|
impl From<Rc<RefCell<Node>>> for NodeRef {
|
|
|
|
fn from(value: Rc<RefCell<Node>>) -> Self {
|
|
|
|
NodeRef(value)
|
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-12 18:27:48 -06:00
|
|
|
impl From<NodeRef> for Rc<RefCell<Node>> {
|
|
|
|
fn from(value: NodeRef) -> Self {
|
|
|
|
value.0
|
2023-10-31 23:53:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-11 22:07:57 -06:00
|
|
|
#[derive(Debug)]
|
2023-11-12 18:27:48 -06:00
|
|
|
pub struct Node {
|
|
|
|
pub name: String,
|
|
|
|
parent: Option<WeakNodeRef>,
|
|
|
|
contents: Contents,
|
2023-11-11 22:07:57 -06:00
|
|
|
}
|
|
|
|
|
2023-11-01 18:58:39 -05:00
|
|
|
impl Node {
|
2023-11-12 18:27:48 -06:00
|
|
|
fn set_parent(&mut self, newparent: &NodeRef) {
|
|
|
|
self.parent = Some(Rc::downgrade(newparent));
|
|
|
|
}
|
|
|
|
pub fn get_parent(&self) -> Option<NodeRef> {
|
|
|
|
match &self.parent{
|
|
|
|
Some(w) => Some(NodeRef(w.clone().upgrade()?)),
|
|
|
|
None => None,
|
2023-11-01 18:58:39 -05:00
|
|
|
}
|
|
|
|
}
|
2023-11-12 18:27:48 -06:00
|
|
|
pub fn get_size(&self) -> Result<usize, FileTreeError> {
|
|
|
|
match self.contents {
|
|
|
|
Contents::Size(s) => Ok(s),
|
|
|
|
Contents::Children(_) => Err(FileTreeError::IsDir),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn set_size(&mut self, size: usize) -> Result<(), FileTreeError> {
|
|
|
|
match self.contents {
|
|
|
|
Contents::Size(ref mut s) => {
|
|
|
|
*s = size;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
Contents::Children(_) => Err(FileTreeError::IsDir),
|
2023-11-11 22:07:57 -06:00
|
|
|
}
|
2023-11-01 18:58:39 -05:00
|
|
|
}
|
2023-11-12 18:27:48 -06:00
|
|
|
pub fn get_children(&self) -> Result<impl Iterator<Item = NodeRef> + '_, FileTreeError> {
|
|
|
|
match &self.contents {
|
|
|
|
Contents::Size(_) => Err(FileTreeError::IsFile),
|
|
|
|
Contents::Children(c) => Ok(c.iter().map(|n| NodeRef(Rc::clone(n)))),
|
2023-11-01 18:58:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-11 22:07:57 -06:00
|
|
|
#[derive(Debug)]
|
2023-11-12 18:27:48 -06:00
|
|
|
pub enum Contents {
|
|
|
|
Size(usize),
|
|
|
|
Children(Vec<NodeRef>),
|
2023-11-01 18:58:39 -05:00
|
|
|
}
|