From 75e8d829f311cd0e67d5dd80a52522c946d99c7a Mon Sep 17 00:00:00 2001 From: Ben S Date: Tue, 17 Jun 2014 22:17:22 +0100 Subject: [PATCH] Make file types a trait --- file.rs | 4 ++-- filetype.rs | 28 +++++++++++++++++----------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/file.rs b/file.rs index 226bbf7..a56953d 100644 --- a/file.rs +++ b/file.rs @@ -7,7 +7,7 @@ use format::{format_metric_bytes, format_IEC_bytes}; use unix::{get_user_name, get_group_name}; use sort::SortPart; use dir::Dir; -use filetype::FileType; +use filetype::HasType; // Instead of working with Rust's Paths, we have our own File object // that holds the Path and various cached information. Each file is @@ -143,7 +143,7 @@ impl<'a> File<'a> { } fn file_colour(&self) -> Style { - FileType::from_file(self).style() + self.get_type().style() } fn permissions_string(&self) -> String { diff --git a/filetype.rs b/filetype.rs index ec4f85c..fc8629c 100644 --- a/filetype.rs +++ b/filetype.rs @@ -55,16 +55,22 @@ impl FileType { Compiled => Fixed(137).normal(), } } +} + +pub trait HasType { + fn get_type(&self) -> FileType; +} - pub fn from_file(file: &File) -> FileType { - if file.stat.kind == io::TypeDirectory { +impl<'a> HasType for File<'a> { + fn get_type(&self) -> FileType { + if self.stat.kind == io::TypeDirectory { return Directory; } - else if file.stat.perm.contains(io::UserExecute) { + else if self.stat.perm.contains(io::UserExecute) { return Executable; } - else if file.ext.is_some() { - let ext = file.ext.unwrap(); + else if self.ext.is_some() { + let ext = self.ext.unwrap(); if IMAGE_TYPES.iter().any(|&s| s == ext) { return Image; } @@ -86,26 +92,26 @@ impl FileType { else if COMPRESSED_TYPES.iter().any(|&s| s == ext) { return Compressed; } - else if file.is_tmpfile() || TEMP_TYPES.iter().any(|&s| s == ext) { + else if self.is_tmpfile() || TEMP_TYPES.iter().any(|&s| s == ext) { return Temp; } } - if file.name.starts_with("README") { + if self.name.starts_with("README") { return Immediate; } - let source_files = file.get_source_files(); + let source_files = self.get_source_files(); if source_files.len() == 0 { - let source_files_usual = file.get_source_files_usual(); - if source_files_usual.iter().any(|path| file.dir.contains(path)) { + let source_files_usual = self.get_source_files_usual(); + if source_files_usual.iter().any(|path| self.dir.contains(path)) { Temp } else { Normal } } - else if source_files.iter().any(|path| file.dir.contains(path)) { + else if source_files.iter().any(|path| self.dir.contains(path)) { Temp } else { -- GitLab