ed_journals/modules/outfitting/
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
use std::fs;
use std::path::Path;

use thiserror::Error;

use crate::modules::outfitting::Outfitting;
use crate::modules::shared::blocking::live_json_file_watcher::LiveJsonFileWatcher;
pub use crate::modules::shared::blocking::live_json_file_watcher::LiveJsonFileWatcherError as OutfittingFileWatcherError;

pub type OutfittingFileWatcher = LiveJsonFileWatcher<Outfitting>;

pub fn read_outfitting_file<P: AsRef<Path>>(
    path: P,
) -> Result<Outfitting, ReadOutfittingFileError> {
    Ok(serde_json::from_str(&fs::read_to_string(path)?)?)
}

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

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

#[cfg(test)]
mod tests {
    use std::env::current_dir;

    use crate::modules::outfitting::blocking::read_outfitting_file;

    #[test]
    fn outfitting_file_is_parsed_correctly() {
        let path = current_dir()
            .unwrap()
            .join("test-files")
            .join("json")
            .join("Outfitting.json");

        let result = read_outfitting_file(path);

        dbg!(&result);

        assert!(result.is_ok());
        assert!(result.unwrap().items.len() > 10);
    }
}