Make file types a trait

This commit is contained in:
Ben S 2014-06-17 22:17:22 +01:00
parent 03ec414dfb
commit 75e8d829f3
2 changed files with 19 additions and 13 deletions

View File

@ -7,7 +7,7 @@ use format::{format_metric_bytes, format_IEC_bytes};
use unix::{get_user_name, get_group_name}; use unix::{get_user_name, get_group_name};
use sort::SortPart; use sort::SortPart;
use dir::Dir; use dir::Dir;
use filetype::FileType; use filetype::HasType;
// Instead of working with Rust's Paths, we have our own File object // Instead of working with Rust's Paths, we have our own File object
// that holds the Path and various cached information. Each file is // that holds the Path and various cached information. Each file is
@ -143,7 +143,7 @@ impl<'a> File<'a> {
} }
fn file_colour(&self) -> Style { fn file_colour(&self) -> Style {
FileType::from_file(self).style() self.get_type().style()
} }
fn permissions_string(&self) -> String { fn permissions_string(&self) -> String {

View File

@ -55,16 +55,22 @@ impl FileType {
Compiled => Fixed(137).normal(), Compiled => Fixed(137).normal(),
} }
} }
}
pub fn from_file(file: &File) -> FileType { pub trait HasType {
if file.stat.kind == io::TypeDirectory { fn get_type(&self) -> FileType;
}
impl<'a> HasType for File<'a> {
fn get_type(&self) -> FileType {
if self.stat.kind == io::TypeDirectory {
return Directory; return Directory;
} }
else if file.stat.perm.contains(io::UserExecute) { else if self.stat.perm.contains(io::UserExecute) {
return Executable; return Executable;
} }
else if file.ext.is_some() { else if self.ext.is_some() {
let ext = file.ext.unwrap(); let ext = self.ext.unwrap();
if IMAGE_TYPES.iter().any(|&s| s == ext) { if IMAGE_TYPES.iter().any(|&s| s == ext) {
return Image; return Image;
} }
@ -86,26 +92,26 @@ impl FileType {
else if COMPRESSED_TYPES.iter().any(|&s| s == ext) { else if COMPRESSED_TYPES.iter().any(|&s| s == ext) {
return Compressed; 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; return Temp;
} }
} }
if file.name.starts_with("README") { if self.name.starts_with("README") {
return Immediate; return Immediate;
} }
let source_files = file.get_source_files(); let source_files = self.get_source_files();
if source_files.len() == 0 { if source_files.len() == 0 {
let source_files_usual = file.get_source_files_usual(); let source_files_usual = self.get_source_files_usual();
if source_files_usual.iter().any(|path| file.dir.contains(path)) { if source_files_usual.iter().any(|path| self.dir.contains(path)) {
Temp Temp
} }
else { else {
Normal Normal
} }
} }
else if source_files.iter().any(|path| file.dir.contains(path)) { else if source_files.iter().any(|path| self.dir.contains(path)) {
Temp Temp
} }
else { else {