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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use crate::{
    Error,
    FmlResult,
};
use serde::Deserialize;
use std::collections::HashMap;

/// An API Response
#[derive(Deserialize)]
pub struct ApiResponse<T> {
    /// A potential API error.
    ///
    /// Populated on error.
    pub error: Option<String>,

    /// A potential response payload.
    ///
    /// Populated if successful.
    pub data: Option<T>,

    /// Unknown data
    #[serde(flatten)]
    pub unknown: HashMap<String, serde_json::Value>,
}

impl<T> ApiResponse<T> {
    /// Whether the response is an error.
    ///
    /// This performs a check on the error field.
    pub fn is_error(&self) -> bool {
        self.error.is_some()
    }

    /// Whether the response is a success.
    ///
    /// This performs a check on the data field.
    pub fn is_success(&self) -> bool {
        self.error.is_none() && self.data.is_some()
    }

    /// Checks whether the data contained is valid.
    ///
    /// This looks to see if this is both an error and success or neither.
    pub fn is_valid_response(&self) -> bool {
        self.is_error() || self.is_success()
    }
}

impl<T> From<ApiResponse<T>> for FmlResult<T> {
    fn from(response: ApiResponse<T>) -> Self {
        match (response.data, response.error) {
            (Some(_data), Some(_e)) => Err(Error::InvalidApiResponse),
            (Some(data), None) => Ok(data),
            (None, Some(e)) => Err(Error::Api(e)),
            (None, None) => Err(Error::InvalidApiResponse),
        }
    }
}

/// An FML article
#[derive(Debug, Deserialize)]
pub struct Article {
    pub apikey: Option<String>,
    pub area: Option<String>,
    pub author: Option<String>,
    pub bitly: Option<String>,
    pub city: Option<String>,
    pub content: String,
    pub content_hidden: String,
    pub country: Option<String>,
    pub countrycode: Option<String>,
    pub created: String,
    pub flag: u32,
    pub gender: Option<u8>,
    pub id: u64,
    pub images: Vec<ArticleImage>,
    pub ip: Option<String>,
    pub keywords: Vec<ArticleKeyword>,
    pub layout: u32,
    pub metrics: ArticleMetrics,
    pub openview: u32,
    pub origin: Option<String>,
    pub paragraphs: Vec<serde_json::Value>,
    pub published: String,
    pub site: u32,
    pub siteorig: Option<serde_json::Value>,
    pub slug: String,
    #[serde(rename = "socialTruncate")]
    pub social_truncate: bool,
    pub spicy: bool,
    pub status: u32,
    pub title: Option<String>,
    #[serde(rename = "type")]
    pub article_type: u32,
    pub updated: String,
    pub url: String,
    pub user: u64,
    pub usermetrics: ArticleUsermetrics,
    pub videos: Vec<serde_json::Value>,
    pub vote: u32,

    #[serde(flatten)]
    pub unknown: HashMap<String, serde_json::Value>,
}

#[derive(Debug, Deserialize)]
pub struct ArticleImage {
    pub copyright: Option<String>,
    pub height: u32,
    pub legend: Option<serde_json::Value>,
    pub name: String,
    pub url: String,
    pub usage: u32,
    pub width: u32,

    #[serde(flatten)]
    pub unknown: HashMap<String, serde_json::Value>,
}

#[derive(Debug, Deserialize)]
pub struct ArticleKeyword {
    pub label: String,
    pub rub: bool,
    pub uid: u32,

    #[serde(flatten)]
    pub unknown: HashMap<String, serde_json::Value>,
}

#[derive(Debug, Deserialize)]
pub struct ArticleMetrics {
    pub article: u64,
    pub comment: u32,
    pub favorite: u32,
    pub mod_negative: u32,
    pub mod_positive: u32,
    pub reports: u32,
    pub smiley_amusing: u32,
    pub smiley_funny: u32,
    pub smiley_hilarious: u32,
    pub smiley_weird: u32,
    pub votes_down: u32,
    pub votes_up: u32,

    #[serde(flatten)]
    pub unknown: HashMap<String, serde_json::Value>,
}

#[derive(Debug, Deserialize)]
pub struct ArticleUsermetrics {
    pub favorite: bool,
    pub smiley: Option<serde_json::Value>,
    pub votes: Option<serde_json::Value>,
    #[serde(flatten)]
    pub unknown: HashMap<String, serde_json::Value>,
}

#[cfg(test)]
mod test {
    use super::*;

    const DATA_1: &str = include_str!("../test_data/DATA_1.json");

    #[test]
    fn data_1() {
        let _data_1: ApiResponse<Vec<Article>> =
            serde_json::from_str(DATA_1).expect("failed to parse");
    }
}