still not working
This commit is contained in:
@@ -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<FileSytemTypes<'a>>,
|
||||
objects: Vec<FileSystemTypes<'a>>,
|
||||
//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<String>) -> 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<String>, size:usize) {
|
||||
self.objects.push(FileSystemTypes::MyFile(MyFile{name:name.into(), size}));
|
||||
}
|
||||
fn mkdir(&'a mut self, name: impl Into<String>) {
|
||||
self.objects.push(FileSystemTypes::MyDir(MyDir::new(name, Some(self))));
|
||||
}
|
||||
|
||||
|
||||
fn new(name: impl Into<String>, 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<'a>>),
|
||||
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::<Vec<_>>()[..] {
|
||||
["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<FileSytemTypes<'_>> = 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::<usize>().unwrap(),
|
||||
})),
|
||||
["dir", name] => cursor.mkdir(name),
|
||||
[size, name] => cursor.touch(name, size.parse::<usize>().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::<Vec<_>>()[..] {
|
||||
["$", "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),
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user