ccdata_api/
schemas.rs

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
170
171
172
173
pub mod min_api;
pub mod data_api;


use std::io::Error;
use serde::Deserialize;
use crate::utils::some_or_error;


// Helper wrapper to parse empty JSON objects.
// Implemented as per: https://github.com/serde-rs/json/issues/660


/// Enum to parse empty JSON objects as Option::None.
#[derive(Deserialize, Debug)]
#[serde(deny_unknown_fields)]
#[serde(untagged)]
pub enum EmptyObject<T> {
    Some(T),
    None {},
}

impl<T> From<EmptyObject<T>> for Option<T> {
    fn from(empty_object: EmptyObject<T>) -> Option<T> {
        match empty_object {
            EmptyObject::Some(v) => Some(v),
            EmptyObject::None {  } => None,
        }
    }
}

impl<T> From<Option<T>> for EmptyObject<T> {
    fn from(option: Option<T>) -> EmptyObject <T> {
        match option {
            Some(v) => EmptyObject::Some(v),
            None => EmptyObject::None {  },
        }
    }
}

impl<T> EmptyObject<T> {
    pub fn into_option(self) -> Option<T> {
        self.into()
    }

    pub fn as_option(&self) -> Option<&T> {
        match self {
            EmptyObject::Some(v) => Some(v),
            EmptyObject::None {} => None,
        }
    }
}


/// Trait that unwraps data or propagates the error.
pub trait DataUnwrap<T> {
    fn data_unwrap(self) -> Result<T, Error>;
}


// Min-API Wrappers


#[derive(Deserialize, Debug)]
pub struct CCCallsMade {
    pub second: i32,
    pub minute: i32,
    pub hour: i32,
    pub day: i32,
    pub month: i32,
    pub total_calls: i32,
}

#[derive(Deserialize, Debug)]
pub struct CCMaxCalls {
    pub second: i32,
    pub minute: i32,
    pub hour: i32,
    pub day: i32,
    pub month: i32,
}

#[derive(Deserialize, Debug)]
pub struct CCRateLimit {
    pub calls_made: Option<CCCallsMade>,
    pub max_calls: Option<CCMaxCalls>,
}

#[derive(Deserialize, Debug)]
pub struct CCMinWrapper<T> {
    #[serde(rename = "Aggregated")]
    pub aggregated: Option<bool>,
    #[serde(rename = "TimeFrom")]
    pub time_from: Option<i64>,
    #[serde(rename = "TimeTo")]
    pub time_to: Option<i64>,
    #[serde(rename = "Data")]
    pub data: Option<T>,
}

impl<T> DataUnwrap<T> for CCMinWrapper<T> {
    fn data_unwrap(self) -> Result<T, Error> {
        some_or_error(self.data, "No data inside the CCMinWrapper.")
    }
}

#[derive(Deserialize, Debug)]
pub struct CCMinResponse<T> {
    #[serde(rename = "Response")]
    pub response: String,
    #[serde(rename = "Message")]
    pub message: String,
    #[serde(rename = "HasWarning")]
    pub has_warning: bool,
    #[serde(rename = "Type")]
    pub type_: i32,
    #[serde(rename = "Data")]
    pub data: EmptyObject<T>,
    #[serde(rename = "RateLimit")]
    pub rate_limit: Option<CCRateLimit>,
}

impl<T> DataUnwrap<T> for CCMinResponse<T> {
    fn data_unwrap(self) -> Result<T, Error> {
        some_or_error(self.data.into_option(), "No data inside the CCMinResponse.")
    }
}


// Data-API Wrappers


#[derive(Deserialize, Debug)]
pub struct CCErrorOtherInfo {
    pub param: Option<String>,
    pub values: Option<Vec<String>>,
}

#[derive(Deserialize, Debug)]
pub struct CCError {
    #[serde(rename = "type")]
    pub type_: i32,
    pub message: String,
    pub other_info: Option<CCErrorOtherInfo>,
}

#[derive(Deserialize, Debug)]
pub struct CCDataResponse<T> {
    #[serde(rename = "Data")]
    pub data: EmptyObject<T>,
    #[serde(rename = "Err")]
    pub error: EmptyObject<CCError>,
}

impl<T> DataUnwrap<T> for CCDataResponse<T> {
    fn data_unwrap(self) -> Result<T, Error> {
        some_or_error(self.data.into_option(), "No data inside the CCDataResponse.")
    }
}


#[cfg(test)]
mod tests {

    #[test]
    fn unit_test_nullable_field() -> () {
        use serde_json;
        use crate::schemas;
        let d: String = String::from("{\"Data\":{}, \"Err\":{\"type\": 23, \"message\": \"hello\", \"other_info\":null}}");
        let response: schemas::CCDataResponse<String> = serde_json::from_str(&d.to_string()).unwrap();
        assert_eq!(response.data.into_option(), None);
    }
}