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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
pub use v1::{Field, FieldContent, GameServer, GameServerList};
pub mod v1 {
use country_parser::Country;
use serde::{
de::{Deserializer, Error, Unexpected},
ser::Serializer,
Deserialize, Serialize,
};
use std::collections::HashMap;
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct GameServerList {
pub servers: Vec<GameServer>,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct GameServer {
pub name: String,
pub address: String,
pub port: u16,
pub description: String,
#[serde(deserialize_with = "deserialize_country")]
#[serde(serialize_with = "serialize_country")]
#[serde(default)]
pub location: Option<Country>,
pub auth_server: String,
pub channel: Option<String>,
pub official: bool,
#[serde(
default,
skip_serializing_if = "HashMap::is_empty",
serialize_with = "ordered_map"
)]
pub extra: HashMap<String, Field>,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct Field {
pub name: String,
pub content: FieldContent,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
#[serde(rename_all = "lowercase")]
pub enum FieldContent {
Text(String),
Url(String),
#[serde(other)]
#[serde(skip_serializing)]
Unknown,
}
fn deserialize_country<'de, D: Deserializer<'de>>(de: D) -> Result<Option<Country>, D::Error> {
country_parser::parse(String::deserialize(de)?)
.map(Some)
.ok_or_else(|| {
D::Error::invalid_value(
Unexpected::Other("invalid country"),
&"valid ISO-3166 country",
)
})
}
fn serialize_country<S: Serializer>(
country: &Option<Country>,
ser: S,
) -> Result<S::Ok, S::Error> {
match country {
Some(country) => ser.serialize_str(&country.iso2),
None => ser.serialize_none(),
}
}
impl GameServer {
pub fn new(
name: &str,
address: &str,
port: u16,
desc: &str,
location: Option<Country>,
auth: &str,
channel: Option<&str>,
official: bool,
extra: HashMap<String, Field>,
) -> Self {
Self {
name: name.to_string(),
address: address.to_string(),
port,
description: desc.to_string(),
location,
auth_server: auth.to_string(),
channel: channel.map(|c| c.to_string()),
official,
extra,
}
}
}
fn ordered_map<S>(value: &HashMap<String, Field>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let ordered: std::collections::BTreeMap<_, _> = value.iter().collect();
ordered.serialize(serializer)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn check_server_list_ron_deserialize() {
ron::de::from_reader::<_, GameServerList>(
&include_bytes!("../examples/v1/example_server_list.ron")[..],
)
.unwrap();
}
#[test]
fn check_server_list_json_deserialize() {
serde_json::de::from_reader::<_, GameServerList>(
&include_bytes!("../examples/v1/example_server_list.json")[..],
)
.unwrap();
}
#[test]
fn check_server_list_json_roundtrip() {
let data = serde_json::de::from_reader::<_, GameServerList>(
&include_bytes!("../examples/v1/example_server_list.json")[..],
)
.unwrap();
serde_json::to_string_pretty(&data).unwrap();
}
#[test]
fn serialize_unknown_is_not_possible() {
let field = Field {
name: "never_serialze".to_string(),
content: FieldContent::Unknown,
};
let result = serde_json::to_string(&field);
assert!(result.is_err());
assert!(result.unwrap_err().is_data());
}
#[test]
fn check_json_schema() {
use jsonschema::JSONSchema;
use serde_json::Value;
let schema = serde_json::de::from_reader::<_, Value>(
&include_bytes!("../examples/v1/schema.json")[..],
)
.unwrap();
JSONSchema::compile(&schema).expect("A valid schema");
}
#[test]
fn validate_json_schema() {
use jsonschema::JSONSchema;
use serde_json::Value;
let schema = serde_json::de::from_reader::<_, Value>(
&include_bytes!("../examples/v1/schema.json")[..],
)
.unwrap();
let json = serde_json::de::from_reader::<_, Value>(
&include_bytes!("../examples/v1/example_server_list.json")[..],
)
.unwrap();
let compiled = JSONSchema::compile(&schema).expect("A valid schema");
let result = compiled.validate(&json);
if let Err(errors) = result {
for error in errors {
println!("Validation error: {}", error);
println!("Instance path: {}", error.instance_path);
}
panic!("json schema isn't valid");
}
}
}