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
#[cfg(test)]
mod tests;
use nanoserde::DeJson;
use std::error::Error;
use unescape::unescape;
#[cfg(feature = "catfact_ninja")]
#[derive(DeJson)]
struct CatFact {
fact: String,
}
#[cfg(feature = "catfact_heroku")]
#[derive(DeJson)]
struct CatFact {
text: String,
}
#[cfg(feature = "catfact_ninja")]
pub fn fetch_cat_fact() -> Result<String, Box<dyn Error>> {
let data: CatFact = DeJson::deserialize_json(
&unescape(
minreq::get("https://catfact.ninja/fact")
.with_timeout(5)
.send()?
.as_str()?,
)
.unwrap_or("".into()),
)?;
Ok(data.fact)
}
#[cfg(feature = "catfact_heroku")]
pub fn fetch_cat_fact() -> Result<String, Box<dyn Error>> {
let data: CatFact = DeJson::deserialize_json(
&unescape(
minreq::get("https://cat-fact.herokuapp.com/facts/random?animal_type=cat")
.with_timeout(5)
.send()?
.as_str()?,
)
.unwrap_or("".into()),
)?;
Ok(data.text)
}