still not working
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
use std::rc::Rc;
|
||||||
use std::io::{prelude::*, BufReader};
|
use std::io::{prelude::*, BufReader};
|
||||||
|
|
||||||
struct MyFile {
|
struct MyFile {
|
||||||
@@ -8,12 +9,33 @@ struct MyFile {
|
|||||||
|
|
||||||
struct MyDir <'a>{
|
struct MyDir <'a>{
|
||||||
name: String,
|
name: String,
|
||||||
objects: Vec<FileSytemTypes<'a>>,
|
objects: Vec<FileSystemTypes<'a>>,
|
||||||
//TODO should this bee reference counted
|
//TODO should this bee reference counted
|
||||||
parent_dir: Option<&'a MyDir<'a>>,
|
parent_dir: Option<&'a MyDir<'a>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <'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 {
|
fn new(name: impl Into<String>, parent_dir: Option<&'a MyDir<'a>>) -> Self {
|
||||||
let name: String = name.into();
|
let name: String = name.into();
|
||||||
let parent_dir = match parent_dir {
|
let parent_dir = match parent_dir {
|
||||||
@@ -28,9 +50,9 @@ impl <'a> MyDir<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum FileSytemTypes <'a>{
|
enum FileSystemTypes <'a>{
|
||||||
MyFile(MyFile),
|
MyFile(MyFile),
|
||||||
MyDir(&'a Box<MyDir<'a>>),
|
MyDir(MyDir<'a>),
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> std::io::Result<()> {
|
fn main() -> std::io::Result<()> {
|
||||||
@@ -39,10 +61,10 @@ fn main() -> std::io::Result<()> {
|
|||||||
let reader = BufReader::new(file);
|
let reader = BufReader::new(file);
|
||||||
|
|
||||||
//create root file object on heap
|
//create root file object on heap
|
||||||
let root = Box::new(MyDir::new("/", None));
|
let root = MyDir::new("/", None);
|
||||||
let mut ls_mode = false;
|
let mut ls_mode = false;
|
||||||
//set a pointer to the currently on MyDir, in this case start at roo
|
//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
|
// loop through the input files lines
|
||||||
reader.lines().for_each(|line| {
|
reader.lines().for_each(|line| {
|
||||||
@@ -52,17 +74,8 @@ fn main() -> std::io::Result<()> {
|
|||||||
if ls_mode && line.as_bytes()[0] != '$' as u8 {
|
if ls_mode && line.as_bytes()[0] != '$' as u8 {
|
||||||
//do adding to cursor
|
//do adding to cursor
|
||||||
match line.split_whitespace().collect::<Vec<_>>()[..] {
|
match line.split_whitespace().collect::<Vec<_>>()[..] {
|
||||||
["dir", name] => { // sub directories start with dir
|
["dir", name] => cursor.mkdir(name),
|
||||||
// create the subdirectory on the heap
|
[size, name] => cursor.touch(name, size.parse::<usize>().unwrap()),
|
||||||
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(),
|
|
||||||
})),
|
|
||||||
_ => panic!("oops {}", line),
|
_ => panic!("oops {}", line),
|
||||||
};
|
};
|
||||||
// end the for_each
|
// end the for_each
|
||||||
@@ -72,26 +85,9 @@ fn main() -> std::io::Result<()> {
|
|||||||
//parse all other lines as commands
|
//parse all other lines as commands
|
||||||
match line.split_whitespace().collect::<Vec<_>>()[..] {
|
match line.split_whitespace().collect::<Vec<_>>()[..] {
|
||||||
["$", "ls"] => ls_mode = true,
|
["$", "ls"] => ls_mode = true,
|
||||||
["$", "cd", "/"] => cursor = root.as_mut(), //set current directory back to root
|
["$", "cd", "/"] => cursor = Box::from(&root).as_mut(), //set current directory back to root
|
||||||
["$", "cd", ".."] => {
|
["$", "cd", ".."] => cursor = Box::from(cursor.move_up().unwrap()).as_mut(),
|
||||||
// set current directory to the parent directory
|
["$", "cd", dir] => cursor = Box::from(cursor.move_down(dir).unwrap()).as_mut(),
|
||||||
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();
|
|
||||||
}
|
|
||||||
_ => panic!("unknown command {}", line),
|
_ => panic!("unknown command {}", line),
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user