minor improvements
This commit is contained in:
parent
4e64b9786d
commit
bcefb1b68f
18 changed files with 111 additions and 124 deletions
|
|
@ -1,12 +1,10 @@
|
|||
use super::template::{DayTrait, ResultType};
|
||||
use crate::common::{file::split_lines, name::Name};
|
||||
use std::{
|
||||
cell::{Cell, RefCell},
|
||||
num::ParseIntError,
|
||||
rc::{Rc, Weak},
|
||||
};
|
||||
|
||||
use crate::common::file::split_lines;
|
||||
|
||||
use super::template::{DayTrait, ResultType};
|
||||
use thiserror::Error;
|
||||
|
||||
const DAY_NUMBER: usize = 7;
|
||||
|
|
@ -75,7 +73,7 @@ enum DirectoryError {
|
|||
#[derive(Debug)]
|
||||
struct Node {
|
||||
parent: Option<Weak<RefCell<Node>>>,
|
||||
name: String,
|
||||
name: Name,
|
||||
sub_dirs: Vec<Rc<RefCell<Node>>>,
|
||||
file_size: i64,
|
||||
size: Cell<Option<i64>>,
|
||||
|
|
@ -85,7 +83,7 @@ impl Node {
|
|||
fn root() -> Node {
|
||||
Node {
|
||||
parent: None,
|
||||
name: "/".to_owned(),
|
||||
name: "/".into(),
|
||||
sub_dirs: vec![],
|
||||
file_size: 0,
|
||||
size: Cell::new(None),
|
||||
|
|
@ -96,7 +94,7 @@ impl Node {
|
|||
self.sub_dirs.push(subdir);
|
||||
}
|
||||
|
||||
fn add_file(&mut self, _name: &str, size: i64) {
|
||||
fn add_file<N: Into<Name>>(&mut self, _name: N, size: i64) {
|
||||
self.file_size += size;
|
||||
}
|
||||
|
||||
|
|
@ -141,10 +139,10 @@ impl Directory {
|
|||
self.node.borrow().file_size
|
||||
}
|
||||
|
||||
pub fn add_subdir(&mut self, name: &str) {
|
||||
pub fn add_subdir<N: Into<Name>>(&mut self, name: N) {
|
||||
let subdir = Rc::new(RefCell::new(Node {
|
||||
parent: Some(Rc::downgrade(&self.node)),
|
||||
name: name.to_owned(),
|
||||
name: name.into(),
|
||||
sub_dirs: vec![],
|
||||
file_size: 0,
|
||||
size: Cell::new(None),
|
||||
|
|
@ -152,7 +150,7 @@ impl Directory {
|
|||
self.node.borrow_mut().add_subdir(subdir);
|
||||
}
|
||||
|
||||
pub fn add_file(&mut self, name: &str, size: i64) {
|
||||
pub fn add_file<N: Into<Name>>(&mut self, name: N, size: i64) {
|
||||
self.node.borrow_mut().add_file(name, size)
|
||||
}
|
||||
|
||||
|
|
@ -165,8 +163,9 @@ impl Directory {
|
|||
.map(|node| Directory { node })
|
||||
}
|
||||
|
||||
pub fn get_subdir(&self, name: &str) -> Option<Directory> {
|
||||
pub fn get_subdir<N: Into<Name>>(&self, name: N) -> Option<Directory> {
|
||||
let node = self.node.borrow();
|
||||
let name = name.into();
|
||||
let sub_node = node.sub_dirs.iter().find(|node| node.borrow().name == name);
|
||||
sub_node.map(|node| Directory::create(node.clone()))
|
||||
}
|
||||
|
|
@ -181,14 +180,14 @@ impl Directory {
|
|||
"/" => root.clone(),
|
||||
".." => {
|
||||
let Some(next) = current.parent() else {
|
||||
return Err(DirectoryError::NoParentDirectory);
|
||||
};
|
||||
return Err(DirectoryError::NoParentDirectory);
|
||||
};
|
||||
next
|
||||
}
|
||||
_ => {
|
||||
let Some(next) = current.get_subdir(dir) else {
|
||||
return Err(DirectoryError::NoSuchDirectory(dir.to_owned()))
|
||||
};
|
||||
return Err(DirectoryError::NoSuchDirectory(dir.to_owned()));
|
||||
};
|
||||
next
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue