tan/
lib.rs

1#[deprecated(since="2.0.0", note="async_get_words() is deprecated! use get_words() instead!")]
2pub async fn async_get_words (){
3    println!("async_get_words() is deprecated! use get_words() instead");
4    std::process::exit(-1);
5}
6
7#[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Clone, Default)]
8pub struct Word{
9    pub value: String,
10    pub amount: u64
11}
12
13pub fn get_words(input: &str, ignored_chars: std::collections::HashSet<char>) -> Vec<Word>{
14    let mut inside_closure = (false, ' ');
15    let mut temp_buf = Vec::new();
16    let mut final_vector = Vec::new();
17    let mut temp_hashmap : std::collections::HashMap<String, u64> = std::collections::HashMap::new();
18    for c in input.chars().collect::<Vec<char>>(){
19        if inside_closure == (true, '\"') && c == '\"'{
20            if let Some(element) = temp_hashmap.get(&String::from_iter(temp_buf.iter())) {
21                temp_hashmap.insert(String::from_iter(temp_buf.iter()).trim().to_string(), *element+1);
22            }
23            else{
24                temp_hashmap.insert(String::from_iter(temp_buf.iter()).trim().to_string(), 1);
25            }
26            temp_buf = Vec::new();
27            inside_closure = (false, ' ');
28            continue;
29        }
30        else if inside_closure == (true, '\'') && c == '\"'{
31            if let Some(element) = temp_hashmap.get(&String::from_iter(temp_buf.iter()).trim().to_string()) {
32                temp_hashmap.insert(String::from_iter(temp_buf.iter()).trim().to_string(), *element+1);
33            }
34            else{
35                temp_hashmap.insert(String::from_iter(temp_buf.iter()).trim().to_string(), 1);
36            }
37            temp_buf = Vec::new();
38            inside_closure = (false, ' ');
39            continue;
40        }
41        else if inside_closure == (false, ' ') && c == '\"' || c == '\''{
42            inside_closure = (true, c);
43            continue;
44        }
45        else if c == ' ' && inside_closure == (false, ' '){
46            if let Some(element) = temp_hashmap.get(&String::from_iter(temp_buf.iter()).trim().to_string()) {
47                temp_hashmap.insert(String::from_iter(temp_buf.iter()).trim().to_string(), *element+1);
48            }
49            else{
50                temp_hashmap.insert(String::from_iter(temp_buf.iter()).trim().to_string(), 1);
51            }
52            temp_buf = Vec::new();
53        }
54        else{
55            if !ignored_chars.contains(&c){
56                temp_buf.push(c);
57            }
58        }
59    }
60
61    if !temp_buf.is_empty(){
62            if let Some(element) = temp_hashmap.get(&String::from_iter(temp_buf.iter()).trim().to_string()) {
63                temp_hashmap.insert(String::from_iter(temp_buf.iter()).trim().to_string(), *element+1);
64            }
65            else{
66                temp_hashmap.insert(String::from_iter(temp_buf.iter()).trim().to_string(), 1);
67            }
68    }
69
70    for key in temp_hashmap.keys(){
71        if let Some(el) = temp_hashmap.get(key){
72            final_vector.push(Word{value: key.clone(), amount: *el});
73        }
74    }
75
76    return final_vector;
77}
78
79#[cfg(test)]
80mod tests{
81    use super::*;
82    #[test]
83    fn test(){
84        let words = get_words("Hello World 'Test Haha\"'", std::collections::HashSet::new());
85        assert_eq!(
86            vec![
87            Word{value:"Hello".to_string(),amount:1},
88            Word{value:"World".to_string(),amount:1},
89            Word{value:"Test Haha".to_string(),amount:1}], 
90            words)
91    }
92}