Add the node_type field into the Meta struct

This commit is contained in:
Peltoche 2018-11-25 16:12:13 +01:00
parent de3a3afeb7
commit fa58b3c011
No known key found for this signature in database
GPG key ID: CED68D0487156952
2 changed files with 29 additions and 12 deletions

View file

@ -1,6 +1,6 @@
use color::{Colors, Elem}; use color::{Colors, Elem};
use icon; use icon;
use meta::Meta; use meta::{Meta, Type};
use std::os::unix::fs::PermissionsExt; use std::os::unix::fs::PermissionsExt;
use std::time::{Duration, SystemTime, UNIX_EPOCH}; use std::time::{Duration, SystemTime, UNIX_EPOCH};
use time::Timespec; use time::Timespec;
@ -38,9 +38,9 @@ impl Formatter {
pub fn format_symlink(&self, meta: &Meta) -> String { pub fn format_symlink(&self, meta: &Meta) -> String {
let mut content = String::new(); let mut content = String::new();
let color = Colors[&Elem::Link]; if let Type::Symlink(ref target) = meta.node_type {
if let Some(ref link) = meta.symlink_target { let color = Colors[&Elem::Link];
content += &color.paint(String::from("") + &color.paint(link).to_string()); content += &color.paint(String::from("") + &color.paint(target).to_string());
} }
content content

View file

@ -16,6 +16,23 @@ pub enum MetaError {
UnreadableMetadatas { path: String, err: io::Error }, UnreadableMetadatas { path: String, err: io::Error },
} }
#[derive(Debug)]
pub enum Type {
Symlink(String),
File,
Directory,
}
impl From<Metadata> for Type {
fn from(meta: Metadata) -> Self {
if meta.is_dir() {
Type::Directory
} else {
Type::File
}
}
}
#[derive(Debug)] #[derive(Debug)]
pub struct Meta { pub struct Meta {
pub path: PathBuf, pub path: PathBuf,
@ -23,7 +40,7 @@ pub struct Meta {
pub metadata: Metadata, pub metadata: Metadata,
pub group: String, pub group: String,
pub user: String, pub user: String,
pub symlink_target: Option<String>, pub node_type: Type,
pub size_value: String, pub size_value: String,
pub size_unit: String, pub size_unit: String,
} }
@ -48,8 +65,8 @@ impl Meta {
}; };
// Check if the path is a symlink or not and retrieve the corresponding // Check if the path is a symlink or not and retrieve the corresponding
// metadatas. // metadatas, and type.
let (meta, symlink_target) = match read_link(path) { let (meta, node_type) = match read_link(path) {
Ok(res) => { Ok(res) => {
// This path is a symlink. // This path is a symlink.
// //
@ -58,17 +75,17 @@ impl Meta {
.symlink_metadata() .symlink_metadata()
.expect("failed to retrieve symlink metadata"); .expect("failed to retrieve symlink metadata");
let symlink = res let target = res
.to_str() .to_str()
.expect("failed to convert symlink to str") .expect("failed to convert symlink to str")
.to_string(); .to_string();
(meta, Some(symlink)) (meta, Type::Symlink(target))
} }
_ => { _ => {
// This path is a file. // 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() { let meta = match path.metadata() {
Ok(meta) => meta, Ok(meta) => meta,
Err(err) => { Err(err) => {
@ -79,7 +96,7 @@ impl Meta {
} }
}; };
(meta, None) (meta, Type::from(meta))
} }
}; };
@ -106,7 +123,7 @@ impl Meta {
name: String::from(name), name: String::from(name),
user, user,
group, group,
symlink_target, node_type: node_type,
size_value: size_parts[0].to_string(), size_value: size_parts[0].to_string(),
size_unit: size_parts[1].to_string(), size_unit: size_parts[1].to_string(),
}) })