elite_journal 0.7.0

Elite: Dangerous journal file stuctures and parsers
Documentation
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
use crate::{de::*, prelude::*};
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::fmt;

#[cfg(feature = "with-postgis-sqlx")]
use geozero::{
    wkb::{FromWkb, WkbDialect},
    CoordDimensions, GeomProcessor, GeozeroGeometry,
};
#[cfg(feature = "with-postgis-sqlx")]
use std::io::Read;

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
pub struct System {
    #[serde(rename = "SystemAddress")]
    pub address: i64,
    #[serde(rename = "StarPos")]
    pub pos: Option<Coordinate>,
    #[serde(rename = "StarSystem")]
    pub name: String,

    #[serde(default)]
    #[serde(deserialize_with = "zero_is_none")]
    pub population: Option<u64>,
    #[serde(rename = "SystemSecurity")]
    #[serde(default)]
    #[serde(deserialize_with = "null_is_none")]
    pub security: Option<Security>,

    #[serde(rename = "SystemGovernment")]
    #[serde(default)]
    #[serde(deserialize_with = "null_is_none")]
    pub government: Option<Government>,
    #[serde(rename = "SystemAllegiance")]
    #[serde(default)]
    #[serde(deserialize_with = "null_is_none")]
    pub allegiance: Option<Allegiance>,
    #[serde(rename = "SystemEconomy")]
    #[serde(default)]
    #[serde(deserialize_with = "null_is_none")]
    pub economy: Option<Economy>,
    #[serde(rename = "SystemSecondEconomy")]
    #[serde(default)]
    #[serde(deserialize_with = "null_is_none")]
    pub second_economy: Option<Economy>,

    #[serde(rename = "SystemFaction")]
    #[serde(default)]
    #[serde(deserialize_with = "crate::de::empty_map_is_none")]
    pub controlling_faction: Option<Faction>,
    #[serde(default)]
    pub factions: Vec<FactionInfo>,
    #[serde(default)]
    pub conflicts: Vec<FactionConflict>,

    pub powers: Option<Vec<Power>>,
    pub powerplay_state: Option<PowerplayState>,
}

impl System {
    pub fn new(address: i64, name: &str) -> Self {
        System {
            address,
            name: name.to_string(),

            pos: None,
            population: None,
            security: None,
            government: None,
            allegiance: None,
            economy: None,
            second_economy: None,
            controlling_faction: None,
            factions: vec![],
            conflicts: vec![],
            powers: None,
            powerplay_state: None,
        }
    }
}

#[test]
fn system() {
    let system = serde_json::from_str::<System>(
        r#"
        {
            "StarPos": [123.321, 1337.42, 0.0],
            "StarSystem": "Somewhere",
            "SystemAddress": 1928374650,
            "Population": 0,
            "SystemSecurity": "",
            "SystemGovernment": "",
            "SystemAllegiance": "",
            "SystemEconomy": "",
            "SystemSecondEconomy": ""
        }
    "#,
    )
    .unwrap();
    assert_eq!(Some(0.), system.pos.map(|p| p.z));
    assert_eq!(None, system.population);
    assert_eq!(None, system.security);
    assert_eq!(None, system.government);
    assert_eq!(None, system.allegiance);
    assert_eq!(None, system.economy);
    assert_eq!(None, system.second_economy);
}

#[derive(Serialize, Deserialize, Debug, Copy, Clone)]
#[cfg_attr(feature = "with-sqlx", derive(sqlx::Type))]
#[serde(rename_all = "PascalCase")]
pub enum Security {
    #[serde(alias = "$SYSTEM_SECURITY_high;")]
    #[serde(alias = "$GAlAXY_MAP_INFO_state_high;")]
    High,
    #[serde(alias = "$SYSTEM_SECURITY_medium;")]
    #[serde(alias = "$GAlAXY_MAP_INFO_state_medium;")]
    Medium,
    #[serde(alias = "$SYSTEM_SECURITY_low;")]
    #[serde(alias = "$GAlAXY_MAP_INFO_state_low;")]
    Low,
    #[serde(alias = "$SYSTEM_SECURITY_anarchy;")]
    #[serde(alias = "$GAlAXY_MAP_INFO_state_anarchy;")]
    Anarchy,
    #[serde(alias = "")]
    None,
}

impl fmt::Display for Security {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl Nullable for Security {
    fn is_null(&self) -> bool {
        match self {
            Security::None => true,
            Security::Anarchy => true,
            _ => false,
        }
    }
}

impl PartialEq for Security {
    fn eq(&self, other: &Self) -> bool {
        match (*other, *self) {
            (Security::None, Security::None) => false,
            (l, r) => l as u8 == r as u8,
        }
    }
}

impl PartialOrd for Security {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        match (*other, *self) {
            (_, Security::None) | (Security::None, _) => None,
            (l, r) => (l as u8).partial_cmp(&(r as u8)),
        }
    }
}

#[test]
fn security() {
    let high = serde_json::from_str(
        r#"
        "$SYSTEM_SECURITY_high;"
    "#,
    )
    .unwrap();
    assert_eq!(Security::High, high);
    let low = serde_json::from_str(
        r#"
        "$GAlAXY_MAP_INFO_state_low;"
    "#,
    )
    .unwrap();
    assert_eq!(Security::Low, low);
    let anarchy = serde_json::from_str(
        r#"
        "Anarchy"
    "#,
    )
    .unwrap();
    assert_eq!(Security::Anarchy, anarchy);
    assert!(anarchy.is_null());
    let none = serde_json::from_str(r#""""#).unwrap();
    assert!(anarchy != none);
    assert!(Security::None != none);
    assert!(none.is_null());
    assert!(high > low);
    assert!(low > anarchy);
    assert!(!(anarchy > none));
    assert!(!(anarchy < none));
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "with-sqlx", derive(sqlx::Type))]
pub enum PowerplayState {
    InPrepareRadius,
    Prepared,
    Exploited,
    Contested,
    Controlled,
    Turmoil,
    HomeSystem,
    // The standings the second Powerplay added. A system is acquired and then
    // built up, so it stands at one of these rather than merely exploited.
    Unoccupied,
    Fortified,
    Stronghold,
}

#[test]
fn powerplay_state() {
    let read = |json: &str| {
        serde_json::from_str::<PowerplayState>(json)
            .unwrap_or_else(|e| panic!("{} should read: {}", json, e))
    };

    assert_eq!(PowerplayState::Exploited, read(r#""Exploited""#));
    // The second Powerplay's three, which a live feed sends more of than it
    // sends the first Powerplay's.
    assert_eq!(PowerplayState::Unoccupied, read(r#""Unoccupied""#));
    assert_eq!(PowerplayState::Fortified, read(r#""Fortified""#));
    assert_eq!(PowerplayState::Stronghold, read(r#""Stronghold""#));
}

/// A Powerplay power holding or contesting a system
///
/// Spelled as the journal's `Powers` array spells them. Arissa Lavigny-Duval
/// is `A. Lavigny-Duval` in the journal and `Arissa Lavigny-Duval` everywhere
/// the API and the dumps repeat her; both read to the one power. The second
/// Powerplay added Nakato Kaine and Jerome Archer and retired no one from the
/// historical record, so Zachary Hudson stays.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "with-sqlx", derive(sqlx::Type))]
pub enum Power {
    #[serde(rename = "Aisling Duval")]
    AislingDuval,
    #[serde(rename = "Archon Delaine")]
    ArchonDelaine,
    #[serde(rename = "A. Lavigny-Duval", alias = "Arissa Lavigny-Duval")]
    ArissaLavignyDuval,
    #[serde(rename = "Denton Patreus")]
    DentonPatreus,
    #[serde(rename = "Edmund Mahon")]
    EdmundMahon,
    #[serde(rename = "Felicia Winters")]
    FeliciaWinters,
    #[serde(rename = "Jerome Archer")]
    JeromeArcher,
    #[serde(rename = "Li Yong-Rui")]
    LiYongRui,
    #[serde(rename = "Nakato Kaine")]
    NakatoKaine,
    #[serde(rename = "Pranav Antal")]
    PranavAntal,
    #[serde(rename = "Yuri Grom")]
    YuriGrom,
    #[serde(rename = "Zachary Hudson")]
    ZacharyHudson,
    #[serde(rename = "Zemina Torval")]
    ZeminaTorval,
}

#[test]
fn power() {
    let read = |json: &str| {
        serde_json::from_str::<Power>(json)
            .unwrap_or_else(|e| panic!("{} should read: {}", json, e))
    };

    assert_eq!(Power::EdmundMahon, read(r#""Edmund Mahon""#));
    // The journal abbreviates her; the API and the dumps do not.
    assert_eq!(Power::ArissaLavignyDuval, read(r#""A. Lavigny-Duval""#));
    assert_eq!(Power::ArissaLavignyDuval, read(r#""Arissa Lavigny-Duval""#));
    // The second Powerplay's additions.
    assert_eq!(Power::NakatoKaine, read(r#""Nakato Kaine""#));
    assert_eq!(Power::JeromeArcher, read(r#""Jerome Archer""#));
}

#[derive(Serialize, Deserialize, Debug, Copy, Clone)]
#[cfg_attr(feature = "with-sqlx", derive(sqlx::Type))]
#[serde(rename_all = "PascalCase")]
pub enum Economy {
    #[serde(alias = "$economy_Agri;")]
    Agriculture,
    #[serde(alias = "$economy_Colony;")]
    Colony,
    #[serde(alias = "$economy_Extraction;")]
    Extraction,
    #[serde(alias = "$economy_HighTech;")]
    #[serde(alias = "High Tech")]
    HighTech,
    #[serde(alias = "$economy_Industrial;")]
    Industrial,
    #[serde(alias = "$economy_Military;")]
    Military,
    #[serde(alias = "$economy_Refinery;")]
    Refinery,
    #[serde(alias = "$economy_Service;")]
    Service,
    #[serde(alias = "$economy_Terraforming;")]
    Terraforming,
    #[serde(alias = "$economy_Tourism;")]
    Tourism,
    #[serde(alias = "$economy_Carrier;")]
    Carrier,
    #[serde(alias = "$economy_Prison;")]
    Prison,
    #[serde(alias = "$economy_Rescue;")]
    Rescue,
    #[serde(alias = "$economy_Undefined;")]
    Undefined,
    #[serde(alias = "")]
    #[serde(alias = "$economy_None;")]
    None,
}

impl fmt::Display for Economy {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl Nullable for Economy {
    fn is_null(&self) -> bool {
        match self {
            Economy::None => true,
            _ => false,
        }
    }
}

impl PartialEq for Economy {
    fn eq(&self, other: &Self) -> bool {
        match (*other, *self) {
            (Economy::None, Economy::None) => false,
            (l, r) => l as u8 == r as u8,
        }
    }
}

#[test]
fn economy() {
    let high_tech = serde_json::from_str(
        r#"
        "High Tech"
    "#,
    )
    .unwrap();
    assert_eq!(Economy::HighTech, high_tech);
    let extraction = serde_json::from_str(
        r#"
        "$economy_Extraction;"
    "#,
    )
    .unwrap();
    assert_eq!(Economy::Extraction, extraction);
    assert!(Economy::None != Economy::None);
    assert!(serde_json::from_str::<Economy>(r#""$economy_None;""#)
        .unwrap()
        .is_null());
    assert!(serde_json::from_str::<Economy>(r#""None""#).unwrap().is_null());
    assert!(serde_json::from_str::<Economy>(r#""""#).unwrap().is_null());
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, Copy)]
pub struct Coordinate {
    pub x: f64,
    pub y: f64,
    pub z: f64,
}

impl fmt::Display for Coordinate {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "({},{},{})", self.x, self.y, self.z)
    }
}

#[cfg(feature = "with-postgis-sqlx")]
impl GeomProcessor for Coordinate {
    fn dimensions(&self) -> CoordDimensions {
        CoordDimensions::xyz()
    }

    fn coordinate(
        &mut self,
        x: f64,
        y: f64,
        z: Option<f64>,
        _m: Option<f64>,
        _t: Option<f64>,
        _tm: Option<u64>,
        _idx: usize,
    ) -> geozero::error::Result<()> {
        self.x = x;
        self.y = y;
        self.z = z.unwrap_or(0.0);
        Ok(())
    }
}

#[cfg(feature = "with-postgis-sqlx")]
impl GeozeroGeometry for Coordinate {
    fn process_geom<P: GeomProcessor>(
        &self,
        processor: &mut P,
    ) -> std::result::Result<(), geozero::error::GeozeroError> {
        processor.point_begin(0)?;
        processor.coordinate(
            self.x,
            self.y,
            Some(self.z),
            None,
            None,
            None,
            0,
        )?;
        processor.point_end(0)
    }

    fn dims(&self) -> CoordDimensions {
        CoordDimensions::xyz()
    }
}

#[cfg(feature = "with-postgis-sqlx")]
impl FromWkb for Coordinate {
    fn from_wkb<R: Read>(
        rdr: &mut R,
        dialect: WkbDialect,
    ) -> geozero::error::Result<Self> {
        let mut pt = Coordinate { x: 0., y: 0., z: 0. };
        geozero::wkb::process_wkb_type_geom(rdr, &mut pt, dialect)?;
        Ok(pt)
    }
}