Improved match statement to make it more readable and slightly faster

This commit is contained in:
Dreaded_X 2022-12-08 00:29:44 +01:00
parent dc44463638
commit d0bef9e654
Signed by: Dreaded_X
GPG Key ID: 76BDEC4E165D8AD9

View File

@ -67,42 +67,27 @@ mod implementation {
input
.lines()
.flat_map(|line| line.split_once(" "))
.for_each(|(typ, rest)| {
match typ {
"$" => {
// There are two cmds: ls, cd ${dir}, we only care about dir
if let Some((_, dir)) = rest.split_once(" ") {
match dir {
"/" => current = &mut root as *mut Node,
".." => {
unsafe {
.map(|line| line.rsplit_once(" ").unwrap())
.for_each(|split| {
match split {
("$ cd", "/") => current = &mut root as *mut Node,
("$ cd", "..") => unsafe {
let parent = (*current).parent;
if parent == std::ptr::null_mut() {
panic!("Node has no parent")
}
current = parent;
}
}
name => {
unsafe {
},
("$ cd", name) => unsafe {
current = (*current).get_directory(name).unwrap();
}
},
};
}
("$", "ls") => {},
("dir", name) => unsafe {
(*current).add_directory(name.to_owned())
},
"dir" => {
unsafe {
(*current).add_directory(rest.to_owned())
}
},
size => {
unsafe {
(size, _name) => unsafe {
(*current).add_file(size.parse().unwrap())
}
},
}
});