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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use crate::{Rc, client::*, constants::cep::{SVC_V1_URL, SVC_V2_URL}, errors::*, request::*};
use serde::{Deserialize, Serialize};
pub enum EnumCepRequestVersion {
V1,
V2
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Coordinates {
pub latitude: String,
pub longitude: String
}
impl PartialEq for Coordinates {
fn eq(&self, other: &Self) -> bool {
self.latitude == other.latitude && self.longitude == other.longitude
}
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Location
{
pub coordinates: Coordinates
}
impl Default for Location {
fn default() -> Self {
Location { coordinates: Coordinates { latitude: String::new(), longitude: String::new() } }
}
}
impl PartialEq for Location {
fn eq(&self, other: &Self) -> bool {
self.coordinates == other.coordinates
}
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct CepResponseData {
pub cep: String,
pub state: String,
pub city: String,
pub neighborhood: String,
pub street: String,
pub service: String,
#[serde(default)]
pub location: Location
}
impl PartialEq for CepResponseData {
fn eq(&self, other: &Self) -> bool {
self.cep == other.cep
&& self.state == other.state
&& self.city == other.city
&& self.neighborhood == other.neighborhood
&& self.street == other.street
&& self.location == other.location
}
}
impl BrasilApiClient {
pub async fn get_cep(&self, cep: &str, cep_version: Option<EnumCepRequestVersion>) -> Result<CepResponseData, Error> {
lazy_static! {
static ref RE: regex::Regex = regex::Regex::new(r"[^0-9]").unwrap();
}
let cepver = cep_version.unwrap_or(EnumCepRequestVersion::V1);
let url = match cepver {
EnumCepRequestVersion::V2 => SVC_V2_URL,
_ => SVC_V1_URL
};
let temp_zipcode = RE.replace_all(cep, "");
if temp_zipcode.is_empty() || temp_zipcode.len() > 8 {
return Err(Error::InvalidInputLenError
{
name: "cep".to_string(),
min: 8,
max: 8
})?
}
Ok(get::<(), CepResponseData>(
&format!("{}/{}/{}", self.base_url, url, temp_zipcode)
).await?)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::client::tests::*;
use futures_await_test::async_test;
#[async_test]
async fn testc_invalid_input_minlen_none_ver() {
let resp = cli().get_cep("09777", None)
.await;
assert!(resp.is_err());
}
#[async_test]
async fn test_invalid_input_empty_none_ver() {
let resp = cli().get_cep("", None)
.await;
assert!(resp.is_err());
}
#[async_test]
async fn test_valid_none_ver() {
let resp = cli().get_cep("01402-000", None).await;
assert!(resp.is_ok());
let expected_text = r#"{"cep":"01402000","state":"SP","city":"São Paulo","neighborhood":"Jardim Paulista","street":"Avenida Brigadeiro Luís Antônio","service":"viacep"}"#;
let mut expected_json = serde_json::from_str::<CepResponseData>(expected_text).unwrap();
expected_json.service = "".into();
let mut from_svc = resp.unwrap();
from_svc.service = "".into();
assert_eq!(from_svc, expected_json);
}
#[async_test]
async fn test_valid_v1_same_as_none() {
let resp_v1 = cli().get_cep("01402-000", Some(EnumCepRequestVersion::V1)).await;
let resp_none = cli().get_cep("01402-000", None).await;
assert!(resp_v1.is_ok());
assert!(resp_none.is_ok());
let expected_text = r#"{"cep":"01402000","state":"SP","city":"São Paulo","neighborhood":"Jardim Paulista","street":"Avenida Brigadeiro Luís Antônio","service":"viacep"}"#;
let mut expected_json = serde_json::from_str::<CepResponseData>(expected_text).unwrap();
expected_json.service = "".into();
let mut from_svc = resp_v1.unwrap();
from_svc.service = "".into();
assert_eq!(from_svc, expected_json);
let mut from_svc_none = resp_none.unwrap();
from_svc_none.service = "".into();
assert_eq!(from_svc, from_svc_none);
}
#[async_test]
async fn test_valid_v2() {
let resp = cli().get_cep("01402-000", Some(EnumCepRequestVersion::V2)).await;
assert!(resp.is_ok());
let expected_text = r#"{"cep":"01402000","state":"SP","city":"São Paulo","neighborhood":"Jardim Paulista","street":"Avenida Brigadeiro Luís Antônio","service":"viacep","location":{"type":"Point","coordinates":{"longitude":"-46.6367822","latitude":"-23.5507017"}}}"#;
let mut expected_json = serde_json::from_str::<CepResponseData>(expected_text).unwrap();
expected_json.service = "".into();
let mut from_svc = resp.unwrap();
from_svc.service = "".into();
assert_eq!(from_svc, expected_json);
}
#[async_test]
async fn test_valid_v2_not_equals_v1() {
let resp_v2 = cli().get_cep("01402-000", Some(EnumCepRequestVersion::V2)).await;
let resp_none = cli().get_cep("01402-000", None).await;
assert!(resp_v2.is_ok());
assert!(resp_none.is_ok());
let expected_text = r#"{"cep":"01402000","state":"SP","city":"São Paulo","neighborhood":"Jardim Paulista","street":"Avenida Brigadeiro Luís Antônio","service":"viacep","location":{"type":"Point","coordinates":{"longitude":"-46.6367822","latitude":"-23.5507017"}}}"#;
let mut expected_json = serde_json::from_str::<CepResponseData>(expected_text).unwrap();
expected_json.service = "".into();
let mut from_svc = resp_v2.unwrap();
from_svc.service = "".into();
assert_eq!(from_svc, expected_json);
let mut from_svc_none = resp_none.unwrap();
from_svc_none.service = "".into();
assert_ne!(from_svc, from_svc_none);
}
}