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
/// Generates a an unique name
/// * len - length of the need to generate symbols  // WARNING: maximum value is '32' symbols
pub fn gen_unique_name(len: usize) -> String {
    // checking the length max value:
    if len > 32 { panic!("Maximum value of length is '32' symbols"); }
    // get datetime:
    let datatime = chrono::Utc::now().to_string();
    // generating 'MD5' hash:
    let mut hash = format!("{:x}", md5::compute(datatime));
    // truncating the MD5 hash string:
    hash.truncate(len);
    
    hash
}

/// Converts the text to latin symbols
/// * text - the input text
/// * rm_spaces - the option to remove spaces from string
pub fn to_latin_text(text: &str, rm_spaces: bool) -> String {
    use regex::Regex;

    // replacing non-latin symbols into dash "-":
    let text = Regex::new(&format!(r"[^A-Za-z0-9\-\_{}]+", if !rm_spaces { r"\s" }else{ "" })).unwrap()
        .replace_all(text.trim(), "-");
    
    // removing underscores && dashes from the start && end of string:
    let text = Regex::new(r"(^[_\-]+|[_\-]+$)+").unwrap()
        .replace(&text, "");

    text.to_string()
}