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
60
61
62
63
64
65
66
67
use serde::Deserialize;
use std::error::Error;
use std::fs::File;
use std::io::BufReader;
use std::path::Path;

///Struct that contains the informations about asciimojis.
#[derive(Deserialize, Debug)]
pub struct Asciimoji {
    names: Vec<String>,
    moji: String,
}

impl Asciimoji {
    ///This function edit the names Vec into a Vec of Strings in lowercase and return the new
    ///object.
    pub fn to_lowercase(mut self) -> Asciimoji{
        self.names = self.names.into_iter().map(|name| name.to_lowercase()).collect();
        self
    }
}

///This function read the yaml file from path and return a Result containing the Vec<Asciimoji>
///that will be used to search and print the asciimoji.
pub fn read_asciimojis_from_file<P: AsRef<Path>>(path: P) -> Result<Vec<Asciimoji>, Box<dyn Error>> {
    let yaml = serde_yaml::from_reader(BufReader::new(File::open(path)?))?;
    Ok(yaml)
}

///This function search in the Vec<Asciimoji> the name of of the asciimoji to show. It returns a
///Vec of String in case multiple asccimojis have that name.
pub fn search_name<'a>(asciimojis: &'a Vec<Asciimoji>, name: &String) -> Vec<&'a String> {
    asciimojis
        .into_iter()
        .filter(|ascimoji| ascimoji.names.contains(name))
        .map(|ascimoji| &ascimoji.moji)
        .collect()
}

pub mod downloader {
    use std::error::Error;
    use curl::easy::Easy;
    use std::fs::File;
    use std::io::prelude::*;
    use std::path::Path;

    const YAML_URL: &str = "https://gitlab.com/jjocram/asciimoji/-/raw/master/asciimojis.yml";

    ///This function download the yaml file that contains the list of asciimojis from the YAML_URL
    ///and save its content in the file located at the YAML_PATH.
    pub fn download_yaml_file_from_repository(yaml_path: &str) -> Result<(), Box<dyn Error>> { 
        if Path::new(yaml_path).exists(){
            return Ok(());
        }
        let mut yaml_file = File::create(yaml_path).unwrap();    

        let mut easy = Easy::new();
        easy.url(YAML_URL)?;
        easy.write_function(move |data| {
            yaml_file.write_all(data).unwrap();
            Ok(data.len())
        })?; 
        easy.perform().unwrap();
        
        Ok(())
    }
}