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
86
87
88
89
90
91
#[derive(Debug, Serialize, Deserialize)]
pub struct Articles {
    pub status: String,
    #[serde(rename = "totalResults")]
    pub total_results: usize,
    pub articles: Vec<Article>,
}

impl Articles {
    pub fn sources(&self) -> Vec<&ArticleSource> {
        self.articles.iter().map(|a| &a.source).collect()
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ArticleSource {
    pub id: Option<String>,
    pub name: String,
}

impl PartialEq for ArticleSource {
    fn eq(&self, other: &ArticleSource) -> bool {
        self.id == other.id && self.name == other.name
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Article {
    pub source: ArticleSource,
    pub author: Option<String>,
    pub title: String,
    pub description: Option<String>,
    pub url: String,
    #[serde(rename = "urlToImage")]
    pub url_to_image: Option<String>,
    #[serde(rename = "publishedAt")]
    pub published_at: String,
    pub content: Option<String>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::path::PathBuf;

    fn load_file(filename: &str) -> String {
        let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        d.push(filename);
        fs::read_to_string(d.as_path()).unwrap_or_else(|_| format!("Unable to read {}", filename))
    }

    fn read_articles(filename: &str) -> Articles {
        let contents = load_file(filename);
        serde_json::from_str(&contents).unwrap()
    }

    #[test]
    fn article_sources() {
        let articles = read_articles("resources/example_everything.json");

        let wired = ArticleSource {
            id: Some("wired".into()),
            name: "Wired".into(),
        };

        let engadget = ArticleSource {
            id: Some("engadget".into()),
            name: "Engadget".into(),
        };

        let expected_sources = vec![&wired, &engadget];
        let sources = articles.sources();
        assert_eq!(sources, expected_sources);
    }

    #[test]
    fn deserialize_everything() {
        let articles = read_articles("resources/example_everything.json");
        assert_eq!(articles.status, "ok");
        assert_eq!(articles.articles.len(), 2);
        assert_eq!(articles.total_results, articles.articles.len());
    }

    #[test]
    fn deserialize_headlines() {
        let articles = read_articles("resources/example_headlines.json");
        assert_eq!(articles.total_results, 2);
        assert_eq!(articles.articles.len(), 2);
    }
}