-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathupdate_json.rs
More file actions
67 lines (59 loc) · 1.79 KB
/
update_json.rs
File metadata and controls
67 lines (59 loc) · 1.79 KB
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
use crate::DateVersion;
use std::fmt;
/// Example JSON structure:
///
/// ```json
/// {
/// "Gateway": {
/// "TargetVersion": "1.2.3.4"
/// }
/// }
/// ```
///
#[derive(Debug, Default, Deserialize, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct UpdateJson {
#[serde(skip_serializing_if = "Option::is_none")]
pub gateway: Option<ProductUpdateInfo>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum VersionSpecification {
Latest,
#[serde(untagged)]
Specific(DateVersion),
}
impl fmt::Display for VersionSpecification {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
VersionSpecification::Latest => write!(f, "latest"),
VersionSpecification::Specific(version) => write!(f, "{}", version),
}
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct ProductUpdateInfo {
/// The version of the product to update to.
pub target_version: VersionSpecification,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn version_specification_roundtrip() {
let cases: &[(&'static str, VersionSpecification)] = &[
(
"2022.2.24.0",
VersionSpecification::Specific("2022.2.24.0".parse().unwrap()),
),
("latest", VersionSpecification::Latest),
];
for (serialized, deserialized) in cases {
let parsed = serde_json::from_str::<VersionSpecification>(&format!("\"{}\"", serialized)).unwrap();
assert_eq!(parsed, *deserialized);
let reserialized = serde_json::to_string(&parsed).unwrap();
assert_eq!(reserialized, format!("\"{}\"", serialized));
}
}
}