Просмотр исходного кода

refactor: Move get_file_type to FileType enum

Robert Minsk 2 лет назад
Родитель
Сommit
e5cf097c53
2 измененных файлов с 15 добавлено и 16 удалено
  1. 10 10
      src/info/filetype.rs
  2. 5 6
      src/theme/mod.rs

+ 10 - 10
src/info/filetype.rs

@@ -14,7 +14,7 @@ use crate::fs::File;
 use crate::theme::FileColours;
 
 #[derive(Debug, Clone)]
-enum FileType {
+pub enum FileType {
     Image,
     Video,
     Music,
@@ -232,17 +232,14 @@ const EXTENSION_TYPES: Map<&'static str, FileType> = phf_map! {
     "zwc"        => FileType::Compiled,
 };
 
-#[derive(Debug)]
-pub struct FileTypeClassifier;
-
-impl FileTypeClassifier {
+impl FileType {
     /// Lookup the file type based on the file's name, by the file name
     /// lowercase extension, or if the file could be compiled from related
     /// source code.
     fn get_file_type(file: &File<'_>) -> Option<FileType> {
         // Case-insensitive readme is checked first for backwards compatibility.
         if file.name.to_lowercase().starts_with("readme") {
-            return Some(FileType::Immediate)
+            return Some(Self::Immediate)
         }
         if let Some(file_type) = FILENAME_TYPES.get(&file.name) {
             return Some(file_type.clone())
@@ -251,23 +248,26 @@ impl FileTypeClassifier {
             return Some(file_type.clone())
         }
         if file.name.ends_with('~') || (file.name.starts_with('#') && file.name.ends_with('#')) {
-            return Some(FileType::Temp)
+            return Some(Self::Temp)
         }
         if let Some(dir) = file.parent_dir {
             if file.get_source_files().iter().any(|path| dir.contains(path)) {
-                return Some(FileType::Compiled)
+                return Some(Self::Compiled)
             }
         }
         None
     }
 }
 
-impl FileColours for FileTypeClassifier {
+#[derive(Debug)]
+pub struct FileTypeColor;
+
+impl FileColours for FileTypeColor {
     /// Map from the file type to the display style/color for the file.
     fn colour_file(&self, file: &File<'_>) -> Option<Style> {
         use ansi_term::Colour::*;
 
-        match FileTypeClassifier::get_file_type(file) {
+        match FileType::get_file_type(file) {
             Some(FileType::Compiled)   => Some(Yellow.normal()),
             Some(FileType::Compressed) => Some(Red.normal()),
             Some(FileType::Crypto)     => Some(Green.bold()),

+ 5 - 6
src/theme/mod.rs

@@ -6,7 +6,6 @@ use crate::output::render;
 
 mod ui_styles;
 pub use self::ui_styles::UiStyles;
-pub use self::ui_styles::Size as SizeColours;
 
 mod lsc;
 pub use self::lsc::LSColors;
@@ -66,7 +65,7 @@ impl Options {
 
     #[allow(trivial_casts)]   // the `as Box<_>` stuff below warns about this for some reason
     pub fn to_theme(&self, isatty: bool) -> Theme {
-        use crate::info::filetype::FileTypeClassifier;
+        use crate::info::filetype::FileTypeColor;
 
         if self.use_colours == UseColours::Never || (self.use_colours == UseColours::Automatic && ! isatty) {
             let ui = UiStyles::plain();
@@ -80,10 +79,10 @@ impl Options {
 
         // Use between 0 and 2 file name highlighters
         let exts = match (exts.is_non_empty(), use_default_filetypes) {
-            (false, false)  => Box::new(NoFileColours)              as Box<_>,
-            (false,  true)  => Box::new(FileTypeClassifier)         as Box<_>,
-            ( true, false)  => Box::new(exts)                       as Box<_>,
-            ( true,  true)  => Box::new((exts, FileTypeClassifier)) as Box<_>,
+            (false, false)  => Box::new(NoFileColours)         as Box<_>,
+            (false,  true)  => Box::new(FileTypeColor)         as Box<_>,
+            ( true, false)  => Box::new(exts)                  as Box<_>,
+            ( true,  true)  => Box::new((exts, FileTypeColor)) as Box<_>,
         };
 
         Theme { ui, exts }