From fa58b3c01110ec92d3f1738fdafc87358d34d06d Mon Sep 17 00:00:00 2001 From: Peltoche Date: Sun, 25 Nov 2018 16:12:13 +0100 Subject: [PATCH] Add the node_type field into the Meta struct --- src/formatter.rs | 8 ++++---- src/meta.rs | 33 +++++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/formatter.rs b/src/formatter.rs index 2b71294..3023580 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -1,6 +1,6 @@ use color::{Colors, Elem}; use icon; -use meta::Meta; +use meta::{Meta, Type}; use std::os::unix::fs::PermissionsExt; use std::time::{Duration, SystemTime, UNIX_EPOCH}; use time::Timespec; @@ -38,9 +38,9 @@ impl Formatter { pub fn format_symlink(&self, meta: &Meta) -> String { let mut content = String::new(); - let color = Colors[&Elem::Link]; - if let Some(ref link) = meta.symlink_target { - content += &color.paint(String::from(" ⇒ ") + &color.paint(link).to_string()); + if let Type::Symlink(ref target) = meta.node_type { + let color = Colors[&Elem::Link]; + content += &color.paint(String::from(" ⇒ ") + &color.paint(target).to_string()); } content diff --git a/src/meta.rs b/src/meta.rs index 7041471..c5238c0 100644 --- a/src/meta.rs +++ b/src/meta.rs @@ -16,6 +16,23 @@ pub enum MetaError { UnreadableMetadatas { path: String, err: io::Error }, } +#[derive(Debug)] +pub enum Type { + Symlink(String), + File, + Directory, +} + +impl From for Type { + fn from(meta: Metadata) -> Self { + if meta.is_dir() { + Type::Directory + } else { + Type::File + } + } +} + #[derive(Debug)] pub struct Meta { pub path: PathBuf, @@ -23,7 +40,7 @@ pub struct Meta { pub metadata: Metadata, pub group: String, pub user: String, - pub symlink_target: Option, + pub node_type: Type, pub size_value: String, pub size_unit: String, } @@ -48,8 +65,8 @@ impl Meta { }; // Check if the path is a symlink or not and retrieve the corresponding - // metadatas. - let (meta, symlink_target) = match read_link(path) { + // metadatas, and type. + let (meta, node_type) = match read_link(path) { Ok(res) => { // This path is a symlink. // @@ -58,17 +75,17 @@ impl Meta { .symlink_metadata() .expect("failed to retrieve symlink metadata"); - let symlink = res + let target = res .to_str() .expect("failed to convert symlink to str") .to_string(); - (meta, Some(symlink)) + (meta, Type::Symlink(target)) } _ => { // This path is a file. // - // Retireve the metadate and return no link target. + // Retireve the metadata and return the node_type. let meta = match path.metadata() { Ok(meta) => meta, Err(err) => { @@ -79,7 +96,7 @@ impl Meta { } }; - (meta, None) + (meta, Type::from(meta)) } }; @@ -106,7 +123,7 @@ impl Meta { name: String::from(name), user, group, - symlink_target, + node_type: node_type, size_value: size_parts[0].to_string(), size_unit: size_parts[1].to_string(), })