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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use reqwest::get;
use reqwest::StatusCode::NotFound;
use percent_encoding::{utf8_percent_encode, PATH_SEGMENT_ENCODE_SET};

pub use reqwest::Result;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Trovo {
    pub preciza: Vec<String>,
    pub malpreciza: Vec<String>,
    pub vortfarado: Vec<Vortfarado>,
    pub tradukoj: Vec<Traduko>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Vortfarado {
    pub partoj: Vec<Parto>,
    pub rezulto: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Parto {
    pub vorto: Option<String>,
    pub parto: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Traduko {
    pub kodo: String,
    pub vorto: Option<String>,
    pub lingvo: String,
    pub traduko: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Vorto {
    pub vorto: String,
    pub difinoj: Vec<Difino>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Difino {
    pub difino: Option<String>,
    pub pludifinoj: Vec<Pludifino>,
    pub ekzemploj: Vec<Ekzemplo>,
    pub tradukoj: Vec<Traduko>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Pludifino {
    pub difino: String,
    pub ekzemploj: Vec<Ekzemplo>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Ekzemplo {
    pub ekzemplo: String,
    pub fonto: Option<String>,
}

pub fn trovi<S>(vorto: S) -> Result<Trovo>
where
    S: AsRef<str>
{
    let vorto = utf8_percent_encode(vorto.as_ref(), PATH_SEGMENT_ENCODE_SET);
    let url = format!("http://www.simplavortaro.org/api/v1/trovi/{}", vorto);
    
    get(&url)?.error_for_status()?.json()
}

pub fn vorto<S>(vorto: S) -> Result<Option<Vorto>>
where
    S: AsRef<str>
{
    let vorto = utf8_percent_encode(vorto.as_ref(), PATH_SEGMENT_ENCODE_SET);
    let url = format!("http://www.simplavortaro.org/api/v1/vorto/{}", vorto);
    
    let res = get(&url)?;
    
    if res.status() == NotFound {
        return Ok(None)
    }

    res.error_for_status()?.json()
}