try number who knows for day07.
This commit is contained in:
		
							parent
							
								
									70a96b79a2
								
							
						
					
					
						commit
						824321c660
					
				
					 4 changed files with 55 additions and 105 deletions
				
			
		| 
						 | 
				
			
			@ -1,82 +1,20 @@
 | 
			
		|||
use std::{
 | 
			
		||||
    cell::RefCell,
 | 
			
		||||
    collections::{hash_map, HashMap},
 | 
			
		||||
    ops::Deref,
 | 
			
		||||
    rc::{Rc, Weak},
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
pub type WeakDirRef = Weak<RefCell<Dir>>;
 | 
			
		||||
use thiserror::Error;
 | 
			
		||||
 | 
			
		||||
pub trait FileLike {
 | 
			
		||||
    fn get_parent(&self) -> Option<DirRef>;
 | 
			
		||||
    fn get_name(&self) -> String;
 | 
			
		||||
#[derive(Error, Debug)]
 | 
			
		||||
pub enum FileTreeError {
 | 
			
		||||
    #[error("Directory operation on file")]
 | 
			
		||||
    IsFile,
 | 
			
		||||
    #[error("File operation on directory")]
 | 
			
		||||
    IsDir,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
pub struct DirRef(Rc<RefCell<Dir>>);
 | 
			
		||||
 | 
			
		||||
impl Deref for DirRef {
 | 
			
		||||
    type Target = Rc<RefCell<Dir>>;
 | 
			
		||||
 | 
			
		||||
    fn deref(&self) -> &Self::Target {
 | 
			
		||||
        &self.0
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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!()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
pub struct Dir {
 | 
			
		||||
    name: String,
 | 
			
		||||
    parent: Option<WeakDirRef>,
 | 
			
		||||
    children: Vec<NodeRef>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Dir {
 | 
			
		||||
    pub fn get_total_size(&self) -> usize {
 | 
			
		||||
        self.children
 | 
			
		||||
            .iter()
 | 
			
		||||
            .map(|n| -> usize {
 | 
			
		||||
                match Rc::clone(n).borrow().deref() {
 | 
			
		||||
                    Node::Dir(d) => d.get_total_size(),
 | 
			
		||||
                    Node::File(f) => f.size,
 | 
			
		||||
                }
 | 
			
		||||
            })
 | 
			
		||||
            .sum()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn get_children(&self) -> impl Iterator<Item = NodeRef> + '_ {
 | 
			
		||||
        self.children.iter().map(|n| NodeRef(Rc::clone(n)))
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl FileLike for Dir {
 | 
			
		||||
    fn get_parent(&self) -> Option<DirRef> {
 | 
			
		||||
        Some(DirRef(self.parent.as_ref()?.upgrade()?))
 | 
			
		||||
    }
 | 
			
		||||
    fn get_name(&self) -> String {
 | 
			
		||||
        self.name.clone()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
type WeakNodeRef = Weak<RefCell<Node>>;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
pub struct NodeRef(Rc<RefCell<Node>>);
 | 
			
		||||
| 
						 | 
				
			
			@ -89,48 +27,60 @@ impl Deref for NodeRef {
 | 
			
		|||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<Rc<RefCell<Node>>> for NodeRef {
 | 
			
		||||
    fn from(value: Rc<RefCell<Node>>) -> Self {
 | 
			
		||||
        NodeRef(value)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<NodeRef> for Rc<RefCell<Node>> {
 | 
			
		||||
    fn from(value: NodeRef) -> Self {
 | 
			
		||||
        value.0
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
pub enum Node {
 | 
			
		||||
    Dir(Dir),
 | 
			
		||||
    File(File),
 | 
			
		||||
pub struct Node {
 | 
			
		||||
    pub name: String,
 | 
			
		||||
    parent: Option<WeakNodeRef>,
 | 
			
		||||
    contents: Contents,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Node {
 | 
			
		||||
    fn change_parent(&mut self, new_parent: &DirRef) {
 | 
			
		||||
        match self {
 | 
			
		||||
            Node::Dir(d) => d.parent = Some(Rc::downgrade(new_parent)),
 | 
			
		||||
            Node::File(f) => f.parent = Rc::downgrade(new_parent),
 | 
			
		||||
    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,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl FileLike for Node {
 | 
			
		||||
    fn get_parent(&self) -> Option<DirRef> {
 | 
			
		||||
        match self {
 | 
			
		||||
            Node::Dir(d) => d.get_parent(),
 | 
			
		||||
            Node::File(f) => f.get_parent(),
 | 
			
		||||
    pub fn get_size(&self) -> Result<usize, FileTreeError> {
 | 
			
		||||
        match self.contents {
 | 
			
		||||
            Contents::Size(s) => Ok(s),
 | 
			
		||||
            Contents::Children(_) => Err(FileTreeError::IsDir),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    fn get_name(&self) -> String {
 | 
			
		||||
        match self {
 | 
			
		||||
            Node::Dir(d) => d.get_name(),
 | 
			
		||||
            Node::File(f) => f.get_name(),
 | 
			
		||||
    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),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    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)))),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
pub struct File {
 | 
			
		||||
    name: String,
 | 
			
		||||
    parent: WeakDirRef,
 | 
			
		||||
    pub size: usize,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl FileLike for File {
 | 
			
		||||
    fn get_parent(&self) -> Option<DirRef> {
 | 
			
		||||
        Some(DirRef(self.parent.upgrade()?))
 | 
			
		||||
    }
 | 
			
		||||
    fn get_name(&self) -> String {
 | 
			
		||||
        self.name.clone()
 | 
			
		||||
    }
 | 
			
		||||
pub enum Contents {
 | 
			
		||||
    Size(usize),
 | 
			
		||||
    Children(Vec<NodeRef>),
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -73,11 +73,11 @@ fn parse_ls(lines: &mut std::iter::Peekable<std::str::Lines<'_>>) -> Vec<LsEntry
 | 
			
		|||
    ret
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn parse(input: &str) -> Dir {
 | 
			
		||||
pub fn parse(input: &str) -> Node {
 | 
			
		||||
    unimplemented!()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn commands_to_tree(input: Vec<Command>) -> Dir {
 | 
			
		||||
pub fn commands_to_tree(input: Vec<Command>) -> Node {
 | 
			
		||||
    unimplemented!()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,4 +1,4 @@
 | 
			
		|||
#![allow(unused)]
 | 
			
		||||
#![allow(dead_code)]
 | 
			
		||||
mod part1;
 | 
			
		||||
mod part2;
 | 
			
		||||
mod parser;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,4 +1,4 @@
 | 
			
		|||
#![allow(unused)]
 | 
			
		||||
#![allow(dead_code)]
 | 
			
		||||
mod part1;
 | 
			
		||||
mod part2;
 | 
			
		||||
mod utilities;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue