<commit_before>use file::{File, GREY};
use self::FileType::*;

use std::old_io as io;

use ansi_term::Style;
use ansi_term::Style::Plain;
use ansi_term::Colour::{Red, Green, Yellow, Blue, Cyan, Fixed};

#[derive(PartialEq, Debug, Copy)]
pub enum FileType {
    Normal, Directory, Executable, Immediate, Compiled, Symlink, Special,
    Image, Video, Music, Lossless, Compressed, Document, Temp, Crypto,
}

static IMAGE_TYPES: &'static [&'static str] = &[
    "png", "jpeg", "jpg", "gif", "bmp", "tiff", "tif",
    "ppm", "pgm", "pbm", "pnm", "webp", "raw", "arw",
    "svg", "stl", "eps", "dvi", "ps", "cbr",
    "cbz", "xpm", "ico" ];

static VIDEO_TYPES: &'static [&'static str] = &[
    "avi", "flv", "m2v", "mkv", "mov", "mp4", "mpeg",
    "mpg", "ogm", "ogv", "vob", "wmv" ];

static MUSIC_TYPES: &'static [&'static str] = &[
    "aac", "m4a", "mp3", "ogg", "wma" ];

static MUSIC_LOSSLESS: &'static [&'static str] = &[
    "alac", "ape", "flac", "wav" ];

static COMPRESSED_TYPES: &'static [&'static str] = &[
    "zip", "tar", "Z", "gz", "bz2", "a", "ar", "7z",
    "iso", "dmg", "tc", "rar", "par" ];

static DOCUMENT_TYPES: &'static [&'static str] = &[
    "djvu", "doc", "docx", "dvi", "eml", "eps", "fotd",
    "odp", "odt", "pdf", "ppt", "pptx", "rtf",
    "xls", "xlsx" ];

static TEMP_TYPES: &'static [&'static str] = &[
    "tmp", "swp", "swo", "swn", "bak" ];

static CRYPTO_TYPES: &'static [&'static str] = &[
    "asc", "gpg", "sig", "signature", "pgp" ];

static COMPILED_TYPES: &'static [&'static str] = &[
    "class", "elc", "hi", "o", "pyc" ];

static BUILD_TYPES: &'static [&'static str] = &[
    "Makefile", "Cargo.toml", "SConstruct", "CMakeLists.txt",
    "build.gradle", "Rakefile", "Gruntfile.js",
    "Gruntfile.coffee" ];

impl FileType {

    /// Get the `ansi_term::Style` that a file of this type should use.
    pub fn style(&self) -> Style {
        match *self {
            Normal     => Plain,
            Directory  => Blue.bold(),
            Symlink    => Cyan.normal(),
            Special    => Yellow.normal(),
            Executable => Green.bold(),
            Image      => Fixed(133).normal(),
            Video      => Fixed(135).normal(),
            Music      => Fixed(92).normal(),
            Lossless   => Fixed(93).normal(),
            Crypto     => Fixed(109).normal(),
            Document   => Fixed(105).normal(),
            Compressed => Red.normal(),
            Temp       => GREY.normal(),
            Immediate  => Yellow.bold().underline(),
            Compiled   => Fixed(137).normal(),
        }
    }
}

pub trait HasType {
    /// For a given file, find out what type it has.
    fn get_type(&self) -> FileType;
}

impl<'a> HasType for File<'a> {
    fn get_type(&self) -> FileType {
        let name = self.name.as_slice();
        if self.stat.kind == io::FileType::Directory {
            return Directory;
        }
        else if self.stat.kind == io::FileType::Symlink {
            return Symlink;
        }
        else if self.stat.kind == io::FileType::BlockSpecial || self.stat.kind == io::FileType::NamedPipe || self.stat.kind == io::FileType::Unknown {
            return Special;
        }
        else if self.stat.perm.contains(io::USER_EXECUTE) {
            return Executable;
        }
        else if name.starts_with("README") || BUILD_TYPES.iter().any(|&s| s == name) {
            return Immediate;
        }
        else if let Some(ref ext) = self.ext {
            if IMAGE_TYPES.iter().any(|&s| s == *ext) {
                return Image;
            }
            else if VIDEO_TYPES.iter().any(|&s| s == *ext) {
                return Video;
            }
            else if MUSIC_TYPES.iter().any(|&s| s == *ext) {
                return Music;
            }
            else if MUSIC_LOSSLESS.iter().any(|&s| s == *ext) {
                return Lossless;
            }
            else if CRYPTO_TYPES.iter().any(|&s| s == *ext) {
                return Crypto;
            }
            else if DOCUMENT_TYPES.iter().any(|&s| s == *ext) {
                return Document;
            }
            else if COMPRESSED_TYPES.iter().any(|&s| s == *ext) {
                return Compressed;
            }
            else if self.is_tmpfile() || TEMP_TYPES.iter().any(|&s| s == *ext) {
                return Temp;
            }

            let source_files = self.get_source_files();
            if source_files.is_empty() {
                return Normal;
            }
            else if source_files.iter().any(|path| self.dir.map(|d| d.contains(path)).unwrap_or(false)) {
                return Temp;
            }
            else {
                if COMPILED_TYPES.iter().any(|&s| s == *ext) {
                    return Compiled;
                }
                else {
                    return Normal;
                }
            }
        }

        return Normal;  // no filetype
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use file::test::{dummy_stat, new_file};<commit_msg>Code cleanups in filetype detection<commit_after>use file::{File, GREY};
use self::FileType::*;

use std::old_io as io;

use ansi_term::Style;
use ansi_term::Style::Plain;
use ansi_term::Colour::{Red, Green, Yellow, Blue, Cyan, Fixed};

#[derive(PartialEq, Debug, Copy)]
pub enum FileType {
    Normal, Directory, Executable, Immediate, Compiled, Symlink, Special,
    Image, Video, Music, Lossless, Compressed, Document, Temp, Crypto,
}

static IMAGE_TYPES: &'static [&'static str] = &[
    "png", "jpeg", "jpg", "gif", "bmp", "tiff", "tif",
    "ppm", "pgm", "pbm", "pnm", "webp", "raw", "arw",
    "svg", "stl", "eps", "dvi", "ps", "cbr",
    "cbz", "xpm", "ico" ];

static VIDEO_TYPES: &'static [&'static str] = &[
    "avi", "flv", "m2v", "mkv", "mov", "mp4", "mpeg",
    "mpg", "ogm", "ogv", "vob", "wmv" ];

static MUSIC_TYPES: &'static [&'static str] = &[
    "aac", "m4a", "mp3", "ogg", "wma" ];

static MUSIC_LOSSLESS: &'static [&'static str] = &[
    "alac", "ape", "flac", "wav" ];

static COMPRESSED_TYPES: &'static [&'static str] = &[
    "zip", "tar", "Z", "gz", "bz2", "a", "ar", "7z",
    "iso", "dmg", "tc", "rar", "par" ];

static DOCUMENT_TYPES: &'static [&'static str] = &[
    "djvu", "doc", "docx", "dvi", "eml", "eps", "fotd",
    "odp", "odt", "pdf", "ppt", "pptx", "rtf",
    "xls", "xlsx" ];

static TEMP_TYPES: &'static [&'static str] = &[
    "tmp", "swp", "swo", "swn", "bak" ];

static CRYPTO_TYPES: &'static [&'static str] = &[
    "asc", "gpg", "sig", "signature", "pgp" ];

static COMPILED_TYPES: &'static [&'static str] = &[
    "class", "elc", "hi", "o", "pyc" ];

static BUILD_TYPES: &'static [&'static str] = &[
    "Makefile", "Cargo.toml", "SConstruct", "CMakeLists.txt",
    "build.gradle", "Rakefile", "Gruntfile.js",
    "Gruntfile.coffee" ];

impl FileType {

    /// Get the `ansi_term::Style` that a file of this type should use.
    pub fn style(&self) -> Style {
        match *self {
            Normal     => Plain,
            Directory  => Blue.bold(),
            Symlink    => Cyan.normal(),
            Special    => Yellow.normal(),
            Executable => Green.bold(),
            Image      => Fixed(133).normal(),
            Video      => Fixed(135).normal(),
            Music      => Fixed(92).normal(),
            Lossless   => Fixed(93).normal(),
            Crypto     => Fixed(109).normal(),
            Document   => Fixed(105).normal(),
            Compressed => Red.normal(),
            Temp       => GREY.normal(),
            Immediate  => Yellow.bold().underline(),
            Compiled   => Fixed(137).normal(),
        }
    }
}

pub trait HasType {
    /// For a given file, find out what type it has.
    fn get_type(&self) -> FileType;
}

impl<'a> HasType for File<'a> {
    fn get_type(&self) -> FileType {
        let name = self.name.as_slice();
        if self.stat.kind == io::FileType::Directory {
            return Directory;
        }
        else if self.stat.kind == io::FileType::Symlink {
            return Symlink;
        }
        else if self.stat.kind == io::FileType::BlockSpecial || self.stat.kind == io::FileType::NamedPipe || self.stat.kind == io::FileType::Unknown {
            return Special;
        }
        else if self.stat.perm.contains(io::USER_EXECUTE) {
            return Executable;
        }
        else if name.starts_with("README") || BUILD_TYPES.iter().any(|&s| s == name) {
            return Immediate;
        }
        else if let Some(ref ext) = self.ext {
            if IMAGE_TYPES.iter().any(|&s| s == *ext) {
                return Image;
            }
            else if VIDEO_TYPES.iter().any(|&s| s == *ext) {
                return Video;
            }
            else if MUSIC_TYPES.iter().any(|&s| s == *ext) {
                return Music;
            }
            else if MUSIC_LOSSLESS.iter().any(|&s| s == *ext) {
                return Lossless;
            }
            else if CRYPTO_TYPES.iter().any(|&s| s == *ext) {
                return Crypto;
            }
            else if DOCUMENT_TYPES.iter().any(|&s| s == *ext) {
                return Document;
            }
            else if COMPRESSED_TYPES.iter().any(|&s| s == *ext) {
                return Compressed;
            }
            else if self.is_tmpfile() || TEMP_TYPES.iter().any(|&s| s == *ext) {
                return Temp;
            }

            let source_files = self.get_source_files();
            if source_files.is_empty() {
                return Normal;
            }
            else if source_files.iter().any(|path| self.dir.map(|d| d.contains(path)).unwrap_or(false)) {
                return Temp;
            }
            else if COMPILED_TYPES.iter().any(|&s| s == *ext) {
                return Compiled;
            }
        }

        return Normal;  // no filetype
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use file::test::{dummy_stat, new_file};
{'fraction_non_alphanumeric': 0.18303751512706737, 'fraction_numerical': 0.005042355788624445, 'mean_word_length': 2.0161192214111923, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4177456', 'n_tokens_mistral': 3478, 'n_tokens_neox': 3402, 'n_words': 786}