From 545f3422e8963e71c80c38c08eaffca61a6e82d8 Mon Sep 17 00:00:00 2001 From: Dylan Thies Date: Thu, 15 Dec 2022 22:00:26 -0500 Subject: [PATCH] still not working --- day7/src/main.rs | 68 +++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 36 deletions(-) diff --git a/day7/src/main.rs b/day7/src/main.rs index 552c0d6..371954c 100644 --- a/day7/src/main.rs +++ b/day7/src/main.rs @@ -1,4 +1,5 @@ use std::fs::File; +use std::rc::Rc; use std::io::{prelude::*, BufReader}; struct MyFile { @@ -8,12 +9,33 @@ struct MyFile { struct MyDir <'a>{ name: String, - objects: Vec>, + objects: Vec>, //TODO should this bee reference counted parent_dir: Option<&'a MyDir<'a>>, } impl <'a> MyDir<'a> { + fn move_up(&self) -> Option<&MyDir>{ + self.parent_dir + } + + fn move_down(&self, dir:impl Into) -> Option<&MyDir> { + let dir = dir.into(); + self.objects.iter() + .filter_map(|x| match x { + FileSystemTypes::MyDir(y) => Some(y), + _ => None, + }) + .find(|x| x.name == dir) + } + fn touch(&mut self, name: impl Into, size:usize) { + self.objects.push(FileSystemTypes::MyFile(MyFile{name:name.into(), size})); + } + fn mkdir(&'a mut self, name: impl Into) { + self.objects.push(FileSystemTypes::MyDir(MyDir::new(name, Some(self)))); + } + + fn new(name: impl Into, parent_dir: Option<&'a MyDir<'a>>) -> Self { let name: String = name.into(); let parent_dir = match parent_dir { @@ -28,9 +50,9 @@ impl <'a> MyDir<'a> { } } -enum FileSytemTypes <'a>{ +enum FileSystemTypes <'a>{ MyFile(MyFile), - MyDir(&'a Box>), + MyDir(MyDir<'a>), } fn main() -> std::io::Result<()> { @@ -39,10 +61,10 @@ fn main() -> std::io::Result<()> { let reader = BufReader::new(file); //create root file object on heap - let root = Box::new(MyDir::new("/", None)); + let root = MyDir::new("/", None); let mut ls_mode = false; //set a pointer to the currently on MyDir, in this case start at roo - let mut cursor = root.as_mut(); + let mut cursor = Box::from(&root).as_mut(); // loop through the input files lines reader.lines().for_each(|line| { @@ -52,17 +74,8 @@ fn main() -> std::io::Result<()> { if ls_mode && line.as_bytes()[0] != '$' as u8 { //do adding to cursor match line.split_whitespace().collect::>()[..] { - ["dir", name] => { // sub directories start with dir - // create the subdirectory on the heap - let new_directory = &Box::new(MyDir::new(name, Some(cursor))); - // attach the new directory to the current directory - let obj: &mut Vec> = cursor.objects.as_mut(); - obj.push(FileSytemTypes::MyDir(new_directory)); - } - [size, name] => cursor.objects.push(FileSytemTypes::MyFile(MyFile { - name: name.to_string(), - size: size.parse::().unwrap(), - })), + ["dir", name] => cursor.mkdir(name), + [size, name] => cursor.touch(name, size.parse::().unwrap()), _ => panic!("oops {}", line), }; // end the for_each @@ -72,26 +85,9 @@ fn main() -> std::io::Result<()> { //parse all other lines as commands match line.split_whitespace().collect::>()[..] { ["$", "ls"] => ls_mode = true, - ["$", "cd", "/"] => cursor = root.as_mut(), //set current directory back to root - ["$", "cd", ".."] => { - // set current directory to the parent directory - cursor = match cursor.parent_dir { - Some( x) => &mut x, - _ => panic!("issue {}", line), - } - } - ["$", "cd", dir] => { - //set the currect directory to the subdirectory named dir - cursor = cursor - .objects - .iter() - .filter_map(|x| match x { - FileSytemTypes::MyDir(y) => Some(y.as_mut()), - _ => None, - }) - .find(|x| x.name == dir) - .unwrap(); - } + ["$", "cd", "/"] => cursor = Box::from(&root).as_mut(), //set current directory back to root + ["$", "cd", ".."] => cursor = Box::from(cursor.move_up().unwrap()).as_mut(), + ["$", "cd", dir] => cursor = Box::from(cursor.move_down(dir).unwrap()).as_mut(), _ => panic!("unknown command {}", line), } });