1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use std::collections::HashMap;

lazy_static! {
  pub static ref FILE_TYPES: HashMap<&'static str, &'static str> = map!(
    "html" => "html",
    "htm" => "html",
    "vue" => "html",
    "ejs" => "html",
    "html.erb" => "html",
    // yaml
    "yaml" => "yaml",
    "yml" => "yaml",
    // rust
    "rust" => "rust",
    "rs" => "rust",
    // sql
    "sql" => "sql",
    // ruby
    "ruby" => "ruby",
    "rb" => "ruby",
    "Gemfile" => "ruby",
    // crystal
    "crystal" => "ruby",
    "cr" => "ruby",
    // elixir
    "elixir" => "elixir",
    "ex" => "elixir",
    "exs" => "elixir",
    // javascript
    "js" => "javascript",
    "jsx" => "javascript",
    "javascript" => "javascript",
    "ts" => "javascript",
    "tsx" => "javascript",
    "typescript" => "javascript",
    "js.erb" => "javascript",
    // css
    "css" => "css",
    "scss" => "css",
    "sass" => "css",
    "less" => "css",
    // json
    "json" => "json",
    "json5" => "json",
    // go
    "go" => "go",
    // python
    "python" => "python",
    "py" => "python",
    // objective-c
    "objective_c" => "objective_c",
    "objective-c" => "objective_c",
    "m" => "objective_c",
    "h" => "objective_c",
    // strings for Cocoa
    "strings" => "strings",
    // csharp
    "csharp" => "csharp",
    "cs" => "csharp",
    // java
    "java" => "java",
    // scala
    "scala" => "scala",
    // swift
    "swift" => "swift",
    // kotlin
    "kotlin" => "kotlin",
    // php
    "php" => "php",
    // dart
    "dart" => "dart",
    // markdown
    "markdown" => "markdown",
    "md" => "markdown",
    // LaTeX
    "latex" => "latex",
    "tex" => "latex",
    // AsciiDoc
    "asciidoc" => "asciidoc",
    "adoc" => "asciidoc",
    // gettext
    "po" => "gettext",
    "pot" => "gettext",
    // conf
    "properties" => "conf",
    "conf" => "conf",
    "ini" => "conf",
    "cfg" => "conf",
    "toml" => "conf",
    // C or C++
    "cc" => "c",
    "cpp" => "c",
    "c" => "c",
    // plain
    "text" => "text",
    "plain" => "text",
    "txt" => "text"
  );
}

// dectermines file_type is support
pub fn match_filename(filename_or_ext: &str) -> &str {
    let ext = get_file_extension(filename_or_ext);
    if !is_support_type(ext.as_str()) {
        return filename_or_ext;
    }

    return FILE_TYPES[ext.as_str()];
}

// dectermines file_type is support
pub fn is_support_type(filename_or_ext: &str) -> bool {
    FILE_TYPES.contains_key(filename_or_ext)
}

// get file extension from filepath, return filename if not has exit
#[allow(clippy::comparison_chain)]
pub fn get_file_extension(filename: &str) -> String {
    let filename = filename.trim();
    if is_support_type(filename) {
        return String::from(filename);
    }

    let filename = filename.split('/').last().unwrap().to_string();
    let path_parts: Vec<&str> = filename.split('.').collect();
    let mut ext: String = path_parts.last().unwrap().to_string();

    let part_len = path_parts.len();
    if part_len > 2 {
        let double_ext = path_parts[(part_len - 2)..part_len].join(".");
        if is_support_type(double_ext.as_str()) {
            ext = double_ext
        }
    } else if part_len < 2 {
        ext = filename;
    }

    ext
}

#[cfg(test)]
mod tests {
    use super::*;
    use pretty_assertions::assert_eq;

    #[test]
    fn is_is_support_type() {
        assert!(is_support_type("html"));
        assert!(is_support_type("htm"));
        assert!(is_support_type("html.erb"));
        assert!(is_support_type("rust"));
        assert!(is_support_type("rs"));

        assert!(!is_support_type("foo"));
        assert!(!is_support_type("index.html"));
        assert!(!is_support_type("gettext"));
        assert!(!is_support_type("gettext.po"));
        assert!(!is_support_type("gettext.pot"));
    }

    #[test]
    fn is_get_file_extension() {
        assert_eq!("text", get_file_extension("text"));
        assert_eq!("txt", get_file_extension("txt"));
        assert_eq!("rb", get_file_extension("/foo/bar/dar.rb"));
        assert_eq!("rb", get_file_extension("/foo/bar/aaa.dar.rb"));
        assert_eq!("html.erb", get_file_extension("/foo/bar/dar.html.erb"));
        assert_eq!("html.erb", get_file_extension("html.erb"));
        assert_eq!("Gemfile", get_file_extension("Gemfile"));
        assert_eq!("js", get_file_extension("/dar.js"));
        assert_eq!("dar", get_file_extension("/foo/bar/dar"));
    }
}