<commit_before>// spell-checker:ignore (ToDO) fperm

extern crate libc;
use libc::{mode_t, S_IRGRP, S_IROTH, S_IRUSR, S_IWGRP, S_IWOTH, S_IWUSR};

use uucore::mode;

pub fn parse_mode(mode: Option<String>) -> Result<mode_t, String> {
    let fperm = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
    if let Some(mode) = mode {
        let arr: &[char] = &['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
        let result = if mode.contains(arr) {
            mode::parse_numeric(fperm as u32, mode.as_str())
        } else {
            mode::parse_symbolic(fperm as u32, mode.as_str(), true)
        };
        result.map(|mode| mode as mode_t)
    } else {
        Ok(fperm)
    }
}

#[cfg(test)]
mod test {
    /// Test if the program is running under WSL
    // ref: <https://github.com/microsoft/WSL/issues/4555> @@ <https://archive.is/dP0bz>
    // ToDO: test on WSL2 which likely doesn't need special handling; plan change to `is_wsl_1()` if WSL2 is less needy
    pub fn is_wsl() -> bool {
        #[cfg(target_os = "linux")]
        {
            if let Ok(b) = std::fs::read("/proc/sys/kernel/osrelease") {
                if let Ok(s) = std::str::from_utf8(&b) {
                    let a = s.to_ascii_lowercase();
                    return a.contains("microsoft") || a.contains("wsl");
                }
            }
        }
        false
    }

    #[test]
    fn symbolic_modes() {
        assert_eq!(super::parse_mode(Some("u+x".to_owned())).unwrap(), 0o766);
        assert_eq!(super::parse_mode(Some("+x".to_owned())).unwrap(), if !is_wsl() { 0o777 } else {0o776});
        assert_eq!(super::parse_mode(Some("a-w".to_owned())).unwrap(), 0o444);
        assert_eq!(super::parse_mode(Some("g-r".to_owned())).unwrap(), 0o626);
    }

    #[test]
    fn numeric_modes() {
        assert_eq!(super::parse_mode(Some("644".to_owned())).unwrap(), 0o644);
        assert_eq!(super::parse_mode(Some("+100".to_owned())).unwrap(), 0o766);
        assert_eq!(super::parse_mode(Some("-4".to_owned())).unwrap(), 0o662);
        assert_eq!(super::parse_mode(None).unwrap(), 0o666);
    }<commit_msg>refactor/polish ~ `cargo fmt`<commit_after>// spell-checker:ignore (ToDO) fperm

extern crate libc;
use libc::{mode_t, S_IRGRP, S_IROTH, S_IRUSR, S_IWGRP, S_IWOTH, S_IWUSR};

use uucore::mode;

pub fn parse_mode(mode: Option<String>) -> Result<mode_t, String> {
    let fperm = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
    if let Some(mode) = mode {
        let arr: &[char] = &['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
        let result = if mode.contains(arr) {
            mode::parse_numeric(fperm as u32, mode.as_str())
        } else {
            mode::parse_symbolic(fperm as u32, mode.as_str(), true)
        };
        result.map(|mode| mode as mode_t)
    } else {
        Ok(fperm)
    }
}

#[cfg(test)]
mod test {
    /// Test if the program is running under WSL
    // ref: <https://github.com/microsoft/WSL/issues/4555> @@ <https://archive.is/dP0bz>
    // ToDO: test on WSL2 which likely doesn't need special handling; plan change to `is_wsl_1()` if WSL2 is less needy
    pub fn is_wsl() -> bool {
        #[cfg(target_os = "linux")]
        {
            if let Ok(b) = std::fs::read("/proc/sys/kernel/osrelease") {
                if let Ok(s) = std::str::from_utf8(&b) {
                    let a = s.to_ascii_lowercase();
                    return a.contains("microsoft") || a.contains("wsl");
                }
            }
        }
        false
    }

    #[test]
    fn symbolic_modes() {
        assert_eq!(super::parse_mode(Some("u+x".to_owned())).unwrap(), 0o766);
        assert_eq!(
            super::parse_mode(Some("+x".to_owned())).unwrap(),
            if !is_wsl() { 0o777 } else { 0o776 }
        );
        assert_eq!(super::parse_mode(Some("a-w".to_owned())).unwrap(), 0o444);
        assert_eq!(super::parse_mode(Some("g-r".to_owned())).unwrap(), 0o626);
    }

    #[test]
    fn numeric_modes() {
        assert_eq!(super::parse_mode(Some("644".to_owned())).unwrap(), 0o644);
        assert_eq!(super::parse_mode(Some("+100".to_owned())).unwrap(), 0o766);
        assert_eq!(super::parse_mode(Some("-4".to_owned())).unwrap(), 0o662);
        assert_eq!(super::parse_mode(None).unwrap(), 0o666);
    }
{'fraction_non_alphanumeric': 0.18528864059590316, 'fraction_numerical': 0.030726256983240222, 'mean_word_length': 2.6139613120269134, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 4, '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': '218575', 'n_tokens_mistral': 1681, 'n_tokens_neox': 1588, 'n_words': 299}