Skip to content

Commit d9e6f6d

Browse files
committed
Add Network and NetworkUpdate structs
1 parent 5dd12f4 commit d9e6f6d

File tree

2 files changed

+306
-0
lines changed

2 files changed

+306
-0
lines changed

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ pub mod features;
244244
pub mod indexes;
245245
/// Module containing the [`Key`](key::Key) struct.
246246
pub mod key;
247+
// Module containing the [`Network`](network::Network) struct.
248+
pub mod network;
247249
pub mod request;
248250
/// Module related to search queries and results.
249251
pub mod search;

src/network.rs

Lines changed: 304 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,304 @@
1+
use std::collections::{BTreeMap, HashMap};
2+
3+
use serde::{Deserialize, Deserializer, Serialize};
4+
5+
#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
6+
pub struct Network {
7+
#[serde(rename = "self")]
8+
pub self_: Option<String>,
9+
#[serde(deserialize_with = "network_deserializer")]
10+
pub remotes: Vec<Remote>,
11+
}
12+
13+
fn network_deserializer<'de, D>(d: D) -> Result<Vec<Remote>, D::Error>
14+
where
15+
D: Deserializer<'de>,
16+
{
17+
let map: BTreeMap<String, HashMap<String, String>> = Deserialize::deserialize(d)?;
18+
19+
Ok(map
20+
.into_iter()
21+
.map(|(name, remote)| Remote {
22+
name,
23+
url: remote.get("url").cloned().unwrap_or_default(),
24+
search_api_key: remote.get("searchApiKey").cloned(),
25+
})
26+
.collect())
27+
}
28+
29+
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
30+
#[serde(rename_all = "camelCase")]
31+
pub struct Remote {
32+
#[serde(skip_serializing, skip_deserializing)]
33+
pub name: String,
34+
35+
pub url: String,
36+
pub search_api_key: Option<String>,
37+
}
38+
39+
#[derive(Serialize, Default)]
40+
pub struct NetworkUpdate {
41+
#[serde(rename = "self", skip_serializing_if = "Option::is_none")]
42+
self_: Option<Option<String>>,
43+
remotes: Option<BTreeMap<String, Option<Remote>>>,
44+
}
45+
46+
impl NetworkUpdate {
47+
#[must_use]
48+
pub fn new() -> Self {
49+
NetworkUpdate {
50+
self_: None,
51+
remotes: Some(BTreeMap::new()),
52+
}
53+
}
54+
55+
pub fn reset_self(&mut self) -> &mut Self {
56+
self.self_ = Some(None);
57+
self
58+
}
59+
60+
pub fn reset_remotes(&mut self) -> &mut Self {
61+
self.remotes = None;
62+
self
63+
}
64+
65+
pub fn with_self(&mut self, new_self: &str) -> &mut Self {
66+
self.self_ = Some(Some(new_self.to_string()));
67+
self
68+
}
69+
70+
pub fn with_remotes(&mut self, new_remotes: &[Remote]) -> &mut Self {
71+
if self.remotes.is_none() {
72+
self.remotes = Some(BTreeMap::new());
73+
}
74+
75+
self.remotes.as_mut().unwrap().extend(
76+
new_remotes
77+
.iter()
78+
.map(|new_remote| (new_remote.name.clone(), Some(new_remote.clone()))),
79+
);
80+
81+
self
82+
}
83+
84+
pub fn delete_remotes(&mut self, remotes_to_delete: &[&str]) -> &mut Self {
85+
if self.remotes.is_none() {
86+
self.remotes = Some(BTreeMap::new());
87+
}
88+
89+
self.remotes.as_mut().unwrap().extend(
90+
remotes_to_delete
91+
.iter()
92+
.map(|remote_name| (remote_name.to_string(), None)),
93+
);
94+
95+
self
96+
}
97+
}
98+
99+
#[cfg(test)]
100+
mod tests {
101+
use super::*;
102+
103+
#[test]
104+
fn test_deserialize_network() {
105+
let example_json = r###"
106+
{
107+
"self": "ms-00",
108+
"remotes": {
109+
"ms-00": {
110+
"url": "http://ms-1235.example.meilisearch.io",
111+
"searchApiKey": "Ecd1SDDi4pqdJD6qYLxD3y7VZAEb4d9j6LJgt4d6xas"
112+
},
113+
"ms-01": {
114+
"url": "http://ms-4242.example.meilisearch.io",
115+
"searchApiKey": "hrVu-OMcjPGElK7692K7bwriBoGyHXTMvB5NmZkMKqQ"
116+
}
117+
}
118+
}
119+
"###;
120+
121+
let actual_remotes: Network = serde_json::from_str(example_json).unwrap();
122+
123+
let expected_remotes = Network {
124+
self_: Some("ms-00".to_string()),
125+
remotes: vec![
126+
Remote {
127+
name: "ms-00".to_string(),
128+
url: "http://ms-1235.example.meilisearch.io".to_string(),
129+
search_api_key: Some("Ecd1SDDi4pqdJD6qYLxD3y7VZAEb4d9j6LJgt4d6xas".to_string()),
130+
},
131+
Remote {
132+
name: "ms-01".to_string(),
133+
url: "http://ms-4242.example.meilisearch.io".to_string(),
134+
search_api_key: Some("hrVu-OMcjPGElK7692K7bwriBoGyHXTMvB5NmZkMKqQ".to_string()),
135+
},
136+
],
137+
};
138+
139+
assert_eq!(actual_remotes, expected_remotes);
140+
}
141+
142+
#[test]
143+
fn test_serialize_network_update_reset_all() {
144+
let mut expected_reset_self_json = r###"
145+
{
146+
"self": null,
147+
"remotes": null
148+
}
149+
"###
150+
.to_string();
151+
expected_reset_self_json.retain(|c| !c.is_whitespace());
152+
153+
let mut reset_self_json =
154+
serde_json::to_string(NetworkUpdate::new().reset_self().reset_remotes()).unwrap();
155+
reset_self_json.retain(|c| !c.is_whitespace());
156+
157+
assert_eq!(expected_reset_self_json, reset_self_json);
158+
}
159+
160+
#[test]
161+
fn test_serialize_network_update_reset_self() {
162+
let mut expected_reset_self_json = r###"
163+
{
164+
"self": null,
165+
"remotes": {}
166+
}
167+
"###
168+
.to_string();
169+
expected_reset_self_json.retain(|c| !c.is_whitespace());
170+
171+
let mut reset_self_json = serde_json::to_string(NetworkUpdate::new().reset_self()).unwrap();
172+
reset_self_json.retain(|c| !c.is_whitespace());
173+
174+
assert_eq!(expected_reset_self_json, reset_self_json);
175+
}
176+
177+
#[test]
178+
fn test_serialize_network_update_with_self() {
179+
let mut expected_with_self_json = r###"
180+
{
181+
"self": "ms-00",
182+
"remotes": {}
183+
}
184+
"###
185+
.to_string();
186+
expected_with_self_json.retain(|c| !c.is_whitespace());
187+
188+
let mut with_self_json =
189+
serde_json::to_string(NetworkUpdate::new().with_self("ms-00")).unwrap();
190+
with_self_json.retain(|c| !c.is_whitespace());
191+
192+
assert_eq!(expected_with_self_json, with_self_json);
193+
}
194+
195+
#[test]
196+
fn test_serialize_network_update_reset_remotes() {
197+
let mut expected_reset_remotes_json = r###"
198+
{
199+
"remotes": null
200+
}
201+
"###
202+
.to_string();
203+
expected_reset_remotes_json.retain(|c| !c.is_whitespace());
204+
205+
let mut reset_remotes_json =
206+
serde_json::to_string(NetworkUpdate::new().reset_remotes()).unwrap();
207+
reset_remotes_json.retain(|c| !c.is_whitespace());
208+
209+
assert_eq!(expected_reset_remotes_json, reset_remotes_json);
210+
}
211+
212+
#[test]
213+
fn test_serialize_network_update_add_remotes() {
214+
let mut expected_with_remotes_json = r###"
215+
{
216+
"remotes": {
217+
"ms-00": {
218+
"url": "http://localhost:7700",
219+
"searchApiKey": "hello_world"
220+
},
221+
"ms-01": {
222+
"url": "http://localhost:7701",
223+
"searchApiKey": "another_key"
224+
}
225+
}
226+
}
227+
"###
228+
.to_string();
229+
expected_with_remotes_json.retain(|c| !c.is_whitespace());
230+
231+
let mut with_remotes_json = serde_json::to_string(NetworkUpdate::new().with_remotes(&[
232+
Remote {
233+
name: "ms-00".to_string(),
234+
url: "http://localhost:7700".to_string(),
235+
search_api_key: Some("hello_world".to_string()),
236+
},
237+
Remote {
238+
name: "ms-01".to_string(),
239+
url: "http://localhost:7701".to_string(),
240+
search_api_key: Some("another_key".to_string()),
241+
},
242+
]))
243+
.unwrap();
244+
with_remotes_json.retain(|c| !c.is_whitespace());
245+
246+
assert_eq!(expected_with_remotes_json, with_remotes_json);
247+
}
248+
249+
#[test]
250+
fn test_serialize_network_update_delete_remotes() {
251+
let mut expected_with_remotes_json = r###"
252+
{
253+
"remotes": {
254+
"ms-00": null,
255+
"ms-01": null
256+
}
257+
}
258+
"###
259+
.to_string();
260+
expected_with_remotes_json.retain(|c| !c.is_whitespace());
261+
262+
let mut with_remotes_json =
263+
serde_json::to_string(NetworkUpdate::new().delete_remotes(&["ms-00", "ms-01"]))
264+
.unwrap();
265+
with_remotes_json.retain(|c| !c.is_whitespace());
266+
267+
assert_eq!(expected_with_remotes_json, with_remotes_json);
268+
}
269+
270+
#[test]
271+
fn test_serialize_network_update_operation_override() {
272+
let mut expected_with_remotes_json = r###"
273+
{
274+
"remotes": {
275+
"ms-00": null,
276+
"ms-01": null
277+
}
278+
}
279+
"###
280+
.to_string();
281+
expected_with_remotes_json.retain(|c| !c.is_whitespace());
282+
283+
let mut with_remotes_json = serde_json::to_string(
284+
NetworkUpdate::new()
285+
.with_remotes(&[
286+
Remote {
287+
name: "ms-00".to_string(),
288+
url: "http://localhost:7700".to_string(),
289+
search_api_key: Some("hello_world".to_string()),
290+
},
291+
Remote {
292+
name: "ms-01".to_string(),
293+
url: "http://localhost:7701".to_string(),
294+
search_api_key: Some("another_key".to_string()),
295+
},
296+
])
297+
.delete_remotes(&["ms-00", "ms-01"]),
298+
)
299+
.unwrap();
300+
with_remotes_json.retain(|c| !c.is_whitespace());
301+
302+
assert_eq!(expected_with_remotes_json, with_remotes_json);
303+
}
304+
}

0 commit comments

Comments
 (0)