Browse Source

Create info module with business logic routines

Currently these routines number two: file type checking based on a file's name, and source file checking, also based on the file's name.
Benjamin Sago 9 years ago
parent
commit
fae0f3874e
6 changed files with 62 additions and 37 deletions
  1. 0 35
      src/fs/file.rs
  2. 1 2
      src/fs/mod.rs
  3. 11 0
      src/info/filetype.rs
  4. 7 0
      src/info/mod.rs
  5. 42 0
      src/info/sources.rs
  6. 1 0
      src/main.rs

+ 0 - 35
src/fs/file.rs

@@ -310,41 +310,6 @@ impl<'dir> File<'dir> {
         }
     }
 
-    /// For this file, return a vector of alternate file paths that, if any of
-    /// them exist, mean that *this* file should be coloured as `Compiled`.
-    ///
-    /// The point of this is to highlight compiled files such as `foo.o` when
-    /// their source file `foo.c` exists in the same directory. It's too
-    /// dangerous to highlight *all* compiled, so the paths in this vector
-    /// are checked for existence first: for example, `foo.js` is perfectly
-    /// valid without `foo.coffee`.
-    pub fn get_source_files(&self) -> Vec<PathBuf> {
-        if let Some(ref ext) = self.ext {
-            match &ext[..] {
-                "class" => vec![self.path.with_extension("java")],  // Java
-                "css"   => vec![self.path.with_extension("sass"),   self.path.with_extension("less")],  // SASS, Less
-                "elc"   => vec![self.path.with_extension("el")],    // Emacs Lisp
-                "hi"    => vec![self.path.with_extension("hs")],    // Haskell
-                "js"    => vec![self.path.with_extension("coffee"), self.path.with_extension("ts")],  // CoffeeScript, TypeScript
-                "o"     => vec![self.path.with_extension("c"),      self.path.with_extension("cpp")], // C, C++
-                "pyc"   => vec![self.path.with_extension("py")],    // Python
-
-                "aux" => vec![self.path.with_extension("tex")],  // TeX: auxiliary file
-                "bbl" => vec![self.path.with_extension("tex")],  // BibTeX bibliography file
-                "blg" => vec![self.path.with_extension("tex")],  // BibTeX log file
-                "lof" => vec![self.path.with_extension("tex")],  // TeX list of figures
-                "log" => vec![self.path.with_extension("tex")],  // TeX log file
-                "lot" => vec![self.path.with_extension("tex")],  // TeX list of tables
-                "toc" => vec![self.path.with_extension("tex")],  // TeX table of contents
-
-                _ => vec![],  // No source files if none of the above
-            }
-        }
-        else {
-            vec![]  // No source files if there's no extension, either!
-        }
-    }
-
     /// Whether this file's extension is any of the strings that get passed in.
     ///
     /// This will always return `false` if the file has no extension.

+ 1 - 2
src/fs/mod.rs

@@ -2,8 +2,7 @@ mod dir;
 pub use self::dir::Dir;
 
 mod file;
-pub mod filetype;
 pub use self::file::File;
 
 pub mod feature;
-pub mod fields;
+pub mod fields;

+ 11 - 0
src/fs/filetype.rs → src/info/filetype.rs

@@ -1,7 +1,17 @@
+//! Tests for various types of file (video, image, compressed, etc).
+//!
+//! Currently this is dependent on the file’s name and extension, because
+//! those are the only metadata that we have access to without reading the
+//! file’s contents.
+
 use fs::File;
 
 
 impl<'_> File<'_> {
+
+    /// An “immediate” file is something that can be run or activated somehow
+    /// in order to kick off the build of a project. It’s usually only present
+    /// in directories full of source code.
     pub fn is_immediate(&self) -> bool {
         self.name.starts_with("README") || self.name_is_one_of( &[
             "Makefile", "Cargo.toml", "SConstruct", "CMakeLists.txt",
@@ -32,6 +42,7 @@ impl<'_> File<'_> {
         ])
     }
 
+    // Lossless music, rather than any other kind of data...
     pub fn is_lossless(&self) -> bool {
         self.extension_is_one_of( &[
             "alac", "ape", "flac", "wav",

+ 7 - 0
src/info/mod.rs

@@ -0,0 +1,7 @@
+//! The "info" module contains routines that aren't about probing the
+//! filesystem nor displaying output to the user, but are internal "business
+//! logic” routines that are performed on a file’s already-read metadata.
+//! (This counts the file name as metadata.)
+
+mod filetype;
+mod sources;

+ 42 - 0
src/info/sources.rs

@@ -0,0 +1,42 @@
+use std::path::PathBuf;
+
+use fs::File;
+
+
+impl<'_> File<'_> {
+
+    /// For this file, return a vector of alternate file paths that, if any of
+    /// them exist, mean that *this* file should be coloured as “compiled”.
+    ///
+    /// The point of this is to highlight compiled files such as `foo.o` when
+    /// their source file `foo.c` exists in the same directory. It's too
+    /// dangerous to highlight *all* compiled, so the paths in this vector
+    /// are checked for existence first: for example, `foo.js` is perfectly
+    /// valid without `foo.coffee`.
+    pub fn get_source_files(&self) -> Vec<PathBuf> {
+        if let Some(ref ext) = self.ext {
+            match &ext[..] {
+                "class" => vec![self.path.with_extension("java")],  // Java
+                "css"   => vec![self.path.with_extension("sass"),   self.path.with_extension("less")],  // SASS, Less
+                "elc"   => vec![self.path.with_extension("el")],    // Emacs Lisp
+                "hi"    => vec![self.path.with_extension("hs")],    // Haskell
+                "js"    => vec![self.path.with_extension("coffee"), self.path.with_extension("ts")],  // CoffeeScript, TypeScript
+                "o"     => vec![self.path.with_extension("c"),      self.path.with_extension("cpp")], // C, C++
+                "pyc"   => vec![self.path.with_extension("py")],    // Python
+
+                "aux" => vec![self.path.with_extension("tex")],  // TeX: auxiliary file
+                "bbl" => vec![self.path.with_extension("tex")],  // BibTeX bibliography file
+                "blg" => vec![self.path.with_extension("tex")],  // BibTeX log file
+                "lof" => vec![self.path.with_extension("tex")],  // TeX list of figures
+                "log" => vec![self.path.with_extension("tex")],  // TeX log file
+                "lot" => vec![self.path.with_extension("tex")],  // TeX list of tables
+                "toc" => vec![self.path.with_extension("tex")],  // TeX table of contents
+
+                _ => vec![],  // No source files if none of the above
+            }
+        }
+        else {
+            vec![]  // No source files if there's no extension, either!
+        }
+    }
+}

+ 1 - 0
src/main.rs

@@ -27,6 +27,7 @@ use fs::{Dir, File};
 use options::{Options, View};
 
 mod fs;
+mod info;
 mod options;
 mod output;
 mod term;