briefcase 0.1.0

Convert to any case!
Documentation
use anyhow::Error;
use fancy_regex::Regex;

pub fn convert(s: &str, mode: &str) -> Result<String, Error> {
    let re = Regex::new(r#"[A-Z]?[a-z0-9]+(?=[^a-z0-9]|\b)|[A-Z0-9]+(?=[^A-Z0-9]|\b)"#)?;
    let arr: Vec<String> = re.find_iter(s).map(|m| m.unwrap().as_str().to_string()).collect();

    let converted_arr: Vec<String> = arr
        .iter()
        .map(|val| {
            if mode == mode.to_uppercase() {
                val.to_uppercase()
            } else if mode.chars().any(|c| c != c.to_lowercase().to_string().chars().next().unwrap()) {
                val.chars().nth(0).unwrap().to_uppercase().to_string() + &val[1..].to_lowercase()
            } else {
                val.to_lowercase()
            }
        })
        .collect();

    let result = if mode.chars().nth(0).unwrap().is_lowercase() {
        converted_arr[0].to_lowercase()
    } else {
        converted_arr[0].to_uppercase()
    };

    let separator = mode.chars().position(|c| !c.is_alphanumeric()).unwrap_or_else(|| mode.len());
    let joined_result = converted_arr[1..].iter().fold(result, |acc, val| acc + &mode[separator..separator + val.len()] + val);

    Ok(joined_result)
}

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

    #[test]
    fn rename_test() {
        let result = convert("Test-Case", "camelCase").unwrap();
        assert_eq!(result, "testCase");
    }
}