mirror of
https://github.com/bootandy/dust.git
synced 2026-06-08 11:29:05 +03:00
use accessor and creator methods for new types in lib
This commit is contained in:
+45
-11
@@ -2,34 +2,68 @@ use std::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Node {
|
||||
pub dir: DirEnt,
|
||||
pub children: Vec<Node>,
|
||||
entry: DirEnt,
|
||||
children: Vec<Node>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct DirEnt {
|
||||
pub name: String,
|
||||
pub size: u64,
|
||||
name: String,
|
||||
size: u64,
|
||||
}
|
||||
|
||||
impl Node {
|
||||
pub fn new(entry: DirEnt, children: Vec<Node>) -> Self {
|
||||
Node {
|
||||
entry: entry,
|
||||
children: children,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn children(&self) -> &Vec<Node> {
|
||||
&self.children
|
||||
}
|
||||
|
||||
pub fn entry(&self) -> &DirEnt {
|
||||
&self.entry
|
||||
}
|
||||
}
|
||||
|
||||
impl DirEnt {
|
||||
pub fn new(name: &str, size: u64) -> Self {
|
||||
DirEnt {
|
||||
name: String::from(name),
|
||||
size: size,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn name(&self) -> &String {
|
||||
&self.name
|
||||
}
|
||||
|
||||
pub fn size(&self) -> u64 {
|
||||
self.size
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for Node {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
if self.dir.size > other.dir.size {
|
||||
if self.entry.size > other.entry.size {
|
||||
Ordering::Less
|
||||
} else if self.dir.size < other.dir.size {
|
||||
} else if self.entry.size < other.entry.size {
|
||||
Ordering::Greater
|
||||
} else {
|
||||
let my_slashes = self.dir.name.matches('/').count();
|
||||
let other_slashes = other.dir.name.matches('/').count();
|
||||
let my_slashes = self.entry.name.matches('/').count();
|
||||
let other_slashes = other.entry.name.matches('/').count();
|
||||
|
||||
if my_slashes > other_slashes {
|
||||
Ordering::Greater
|
||||
} else if my_slashes < other_slashes {
|
||||
Ordering::Less
|
||||
} else {
|
||||
if self.dir.name < other.dir.name {
|
||||
if self.entry.name < other.entry.name {
|
||||
Ordering::Less
|
||||
} else if self.dir.name > other.dir.name {
|
||||
} else if self.entry.name > other.entry.name {
|
||||
Ordering::Greater
|
||||
} else {
|
||||
Ordering::Equal
|
||||
@@ -45,7 +79,7 @@ impl PartialOrd for Node {
|
||||
}
|
||||
impl PartialEq for Node {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
(&self.dir.name, self.dir.size) == (&other.dir.name, other.dir.size)
|
||||
(&self.entry.name, self.entry.size) == (&other.entry.name, other.entry.size)
|
||||
}
|
||||
}
|
||||
impl Eq for Node {}
|
||||
|
||||
Reference in New Issue
Block a user