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 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

View file

@ -16,6 +16,23 @@ pub enum MetaError {
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)]
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<String>,
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(),
})