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
use crate::{
data::RoomName,
decoders::{double_optional_timespec_seconds, optional_timespec_seconds},
};
with_update_struct! {
#[derive(serde_derive::Deserialize, Clone, Debug, PartialEq)]
pub struct PortalDestination {
pub room: RoomName,
pub x: u32,
pub y: u32,
}
#[derive(serde_derive::Deserialize, Clone, Debug)]
pub struct PortalDestinationUpdate { ... }
}
with_base_fields_and_update_struct! {
#[derive(Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct StructurePortal {
#[serde(default, with = "optional_timespec_seconds")]
pub unstable_date: Option<time::Timespec>,
pub decay_time: Option<u32>,
pub destination: PortalDestination,
}
#[derive(Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct StructurePortalUpdate {
#[serde(default, with = "double_optional_timespec_seconds")]
(no_extra_meta)
- unstable_date: Option<time::Timespec>,
- decay_time: Option<u32>,
- destination: PortalDestination,
}
}
#[cfg(test)]
mod test {
use serde::Deserialize;
use {serde_json, time};
use crate::data::RoomName;
use super::{PortalDestination, StructurePortal};
#[test]
fn parse_portal_decaying() {
let json = json!({
"_id": "59570dde2d46c88436d2ab7e",
"decayTime": 20197693,
"destination": {
"room": "E95S15",
"x": 31,
"y": 26
},
"room": "W5N35",
"type": "portal",
"unstableDate": null,
"x": 13,
"y": 30
});
let obj = StructurePortal::deserialize(json).unwrap();
assert_eq!(
obj,
StructurePortal {
room: RoomName::new("W5N35").unwrap(),
x: 13,
y: 30,
id: "59570dde2d46c88436d2ab7e".to_owned(),
unstable_date: None,
decay_time: Some(20197693),
destination: PortalDestination {
room: RoomName::new("E95S15").unwrap(),
x: 31,
y: 26,
},
}
);
}
#[test]
fn parse_portal_stable_and_update() {
let json = json!({
"_id": "595fc9cd28a6884ac973e390",
"destination": {
"room": "W25N35",
"x": 16,
"y": 41
},
"room": "W5N85",
"type": "portal",
"unstableDate": 1500313804391i64,
"x": 22,
"y": 37
});
let mut obj = StructurePortal::deserialize(json).unwrap();
assert_eq!(
obj,
StructurePortal {
room: RoomName::new("W5N85").unwrap(),
x: 22,
y: 37,
id: "595fc9cd28a6884ac973e390".to_owned(),
unstable_date: Some(time::Timespec::new(1500313804391, 0)),
decay_time: None,
destination: PortalDestination {
room: RoomName::new("W25N35").unwrap(),
x: 16,
y: 41,
},
}
);
obj.update(
serde_json::from_value(json!({
"decayTime": 20197693,
"unstableDate": null,
}))
.unwrap(),
);
assert_eq!(
obj,
StructurePortal {
room: RoomName::new("W5N85").unwrap(),
x: 22,
y: 37,
id: "595fc9cd28a6884ac973e390".to_owned(),
unstable_date: None,
decay_time: Some(20197693),
destination: PortalDestination {
room: RoomName::new("W25N35").unwrap(),
x: 16,
y: 41,
},
}
);
}
}