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
/// Parsing of *Phonet* `Draft`
pub mod draft;
/// Error type for *Phonet*
pub mod error;
/// Running and displaying of *Phonet* `Draft`
pub mod outcome;

/// Color styles for stdout
mod color;
/// Generation of random words
mod generate;

pub use crate::{
    color::colorize,
    draft::Draft,
    outcome::{DisplayLevel, Outcome},
};

/// Message for failed matching of static regex
const REGEX_MATCH_FAIL: &str = "Regex failed on 'match' method. This should never happen";

/// Adds '.min' to filename, before last file extension
///
/// Returns 'min.' before the entire name, if the filename has no extension
///
/// Returns an empty string, if `file` is an empty string
pub fn get_min_filename(file: &str) -> String {
    let mut split = file.split('.');

    // Get last file extension
    let ext = match split.next_back() {
        // Return blank string if filename is empty
        None | Some("") => return String::new(),

        Some(string) => string,
    };

    let rest: Vec<&str> = split.collect();
    if !rest.is_empty() {
        // Filename and extension
        rest.join(".") + ".min." + ext
    } else {
        // No extension or only extension (no filename)
        "min.".to_string() + ext
    }
}

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

    #[test]
    fn get_min_filename_works() {
        assert_eq!(get_min_filename(""), "");
        assert_eq!(get_min_filename("phonet"), "min.phonet");
        assert_eq!(get_min_filename("myfile.phonet"), "myfile.min.phonet");
        assert_eq!(get_min_filename("one.two.phonet"), "one.two.min.phonet");
    }
}