Jelajahi Sumber

Make file types a trait

Ben S 11 tahun lalu
induk
melakukan
75e8d829f3
2 mengubah file dengan 19 tambahan dan 13 penghapusan
  1. 2 2
      file.rs
  2. 17 11
      filetype.rs

+ 2 - 2
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 {

+ 17 - 11
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 {