exa/src/info/filetype.rs

95 lines
3.0 KiB
Rust
Raw Normal View History

//! Tests for various types of file (video, image, compressed, etc).
//!
//! Currently this is dependent on the files name and extension, because
//! those are the only metadata that we have access to without reading the
//! files contents.
use fs::File;
2015-06-08 20:33:39 +00:00
#[derive(Debug)]
pub struct FileExtensions;
impl FileExtensions {
/// An “immediate” file is something that can be run or activated somehow
/// in order to kick off the build of a project. Its usually only present
/// in directories full of source code.
pub fn is_immediate(&self, file: &File) -> bool {
file.name.starts_with("README") || file.name_is_one_of( &[
2015-05-09 15:10:26 +00:00
"Makefile", "Cargo.toml", "SConstruct", "CMakeLists.txt",
"build.gradle", "Rakefile", "Gruntfile.js",
"Gruntfile.coffee",
])
}
2015-05-09 15:10:26 +00:00
pub fn is_image(&self, file: &File) -> bool {
file.extension_is_one_of( &[
2015-05-09 15:10:26 +00:00
"png", "jpeg", "jpg", "gif", "bmp", "tiff", "tif",
"ppm", "pgm", "pbm", "pnm", "webp", "raw", "arw",
"svg", "stl", "eps", "dvi", "ps", "cbr",
"cbz", "xpm", "ico", "cr2", "orf", "nef",
2015-05-09 15:10:26 +00:00
])
}
2015-05-09 15:10:26 +00:00
pub fn is_video(&self, file: &File) -> bool {
file.extension_is_one_of( &[
2015-05-09 15:10:26 +00:00
"avi", "flv", "m2v", "mkv", "mov", "mp4", "mpeg",
"mpg", "ogm", "ogv", "vob", "wmv", "webm", "m2ts",
"ts",
2015-05-09 15:10:26 +00:00
])
}
2015-05-09 15:10:26 +00:00
pub fn is_music(&self, file: &File) -> bool {
file.extension_is_one_of( &[
"aac", "m4a", "mp3", "ogg", "wma", "mka", "opus",
2015-05-09 15:10:26 +00:00
])
}
2015-05-09 15:10:26 +00:00
// Lossless music, rather than any other kind of data...
pub fn is_lossless(&self, file: &File) -> bool {
file.extension_is_one_of( &[
2015-05-09 15:10:26 +00:00
"alac", "ape", "flac", "wav",
])
}
2015-05-09 15:10:26 +00:00
pub fn is_crypto(&self, file: &File) -> bool {
file.extension_is_one_of( &[
"asc", "enc", "gpg", "pgp", "sig", "signature", "pfx", "p12",
2015-05-09 15:10:26 +00:00
])
}
2015-05-09 15:10:26 +00:00
pub fn is_document(&self, file: &File) -> bool {
file.extension_is_one_of( &[
2015-05-09 15:10:26 +00:00
"djvu", "doc", "docx", "dvi", "eml", "eps", "fotd",
"odp", "odt", "pdf", "ppt", "pptx", "rtf",
"xls", "xlsx",
])
}
2015-05-09 15:10:26 +00:00
pub fn is_compressed(&self, file: &File) -> bool {
file.extension_is_one_of( &[
2015-05-09 15:10:26 +00:00
"zip", "tar", "Z", "gz", "bz2", "a", "ar", "7z",
"iso", "dmg", "tc", "rar", "par", "tgz",
2015-05-09 15:10:26 +00:00
])
}
2015-05-09 15:10:26 +00:00
pub fn is_temp(&self, file: &File) -> bool {
file.name.ends_with('~')
|| (file.name.starts_with('#') && file.name.ends_with('#'))
|| file.extension_is_one_of( &[ "tmp", "swp", "swo", "swn", "bak" ])
}
2015-05-09 15:10:26 +00:00
pub fn is_compiled(&self, file: &File) -> bool {
if file.extension_is_one_of( &[ "class", "elc", "hi", "o", "pyc" ]) {
2015-05-09 15:10:26 +00:00
true
}
else if let Some(dir) = file.parent_dir {
file.get_source_files().iter().any(|path| dir.contains(path))
}
2015-05-09 15:10:26 +00:00
else {
false
}
}
}