ed_journals/modules/status/
blocking.rs

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
use std::fs;
use std::path::Path;

use thiserror::Error;

use crate::modules::shared::blocking::live_json_file_watcher::LiveJsonFileWatcher;
pub use crate::modules::shared::blocking::live_json_file_watcher::LiveJsonFileWatcherError as StatusFileWatcherError;
use crate::status::Status;

pub type StatusFileWatcher = LiveJsonFileWatcher<Status>;

pub fn read_status_file<P: AsRef<Path>>(path: P) -> Result<Status, ReadStatusFileError> {
    let string_content = fs::read_to_string(path)?;

    if string_content.is_empty() {
        return Err(ReadStatusFileError::Empty);
    }

    Ok(serde_json::from_str(&string_content)?)
}

#[derive(Debug, Error)]
pub enum ReadStatusFileError {
    #[error(transparent)]
    IO(#[from] std::io::Error),

    #[error("The status file was empty")]
    Empty,

    #[error("Failed to parse status file: {0}")]
    SerdeJson(#[from] serde_json::Error),
}

#[cfg(test)]
mod tests {
    use crate::status::Status;
    use crate::tests::test_root;
    use std::fs::read_to_string;

    #[test]
    fn none_status_file_is_parsed_correctly() {
        let file = test_root().join("json").join("StatusNone.json");

        let string_contents = read_to_string(file).unwrap();

        let status = serde_json::from_str::<Status>(&string_contents);

        dbg!(&status);
        assert!(status.is_ok());
        assert!(status.unwrap().contents.is_none());
    }

    #[test]
    fn supercruise_status_file_is_parsed_correctly() {
        let file = test_root().join("json").join("StatusSupercruise.json");

        let string_contents = read_to_string(file).unwrap();

        let status = serde_json::from_str::<Status>(&string_contents);

        dbg!(&status);

        let status = status.unwrap().contents.unwrap();

        assert!(!status.flags.landed());
        assert!(!status.flags.landing_gear_down());
        assert!(status.planet_status.is_none());
    }

    #[test]
    fn landed_status_file_is_parsed_correctly() {
        let file = test_root().join("json").join("StatusLanded.json");

        let string_contents = read_to_string(file).unwrap();

        let status = serde_json::from_str::<Status>(&string_contents);

        dbg!(&status);
        let status = status.unwrap().contents.unwrap();

        assert!(!status.flags.in_srv());
        assert!(status.flags.landed());
        assert!(status.flags.landing_gear_down());
        assert!(status.planet_status.is_some());
        assert!(status.kind.is_ship_status());
    }

    #[test]
    fn srv_status_file_is_parsed_correctly() {
        let file = test_root().join("json").join("StatusSRV.json");

        let string_contents = read_to_string(file).unwrap();

        let status = serde_json::from_str::<Status>(&string_contents);

        dbg!(&status);
        let status = status.unwrap().contents.unwrap();

        assert!(status.flags.in_srv());
        assert!(status.planet_status.is_some());
        assert!(status.kind.is_ship_status());
    }

    #[test]
    fn on_foot_status_file_is_parsed_correctly() {
        let file = test_root().join("json").join("StatusOnFoot.json");

        let string_contents = read_to_string(file).unwrap();

        let status = serde_json::from_str::<Status>(&string_contents);

        dbg!(&status);
        let status = status.unwrap().contents.unwrap();

        assert!(status.flags2.on_foot());
        assert!(status.planet_status.is_some());
        assert!(status.kind.is_on_foot_status());
    }

    #[test]
    fn combat_status_file_is_parsed_correctly() {
        let file = test_root().join("json").join("StatusCombat.json");

        let string_contents = read_to_string(file).unwrap();

        let status = serde_json::from_str::<Status>(&string_contents);

        dbg!(&status);
        let status = status.unwrap().contents.unwrap();

        assert!(!status.flags.landed());
        assert!(status.kind.is_ship_status());
        assert!(status.kind.ship_status().is_some());
        assert!(status.kind.ship_status().unwrap().destination.is_some());
    }
}