Skip to content

Commit

Permalink
✨ Add ship locker module
Browse files Browse the repository at this point in the history
  • Loading branch information
rster2002 committed May 16, 2024
1 parent c53b2e3 commit 61846a0
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub use modules::market;
pub use modules::nav_route;
pub use modules::modules_info;
pub use modules::backpack;
pub use modules::ship_locker;

#[cfg(test)]
mod tests {
Expand Down
1 change: 1 addition & 0 deletions src/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod nav_route;
pub mod modules_info;
pub mod backpack;
pub mod cargo;
pub mod ship_locker;

/// Contains structs and enums which are used in multiple places. Things like commodity and material
/// names, ship types, exobiology data etc. can be found here.
Expand Down
22 changes: 22 additions & 0 deletions src/modules/ship_locker/asynchronous.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::path::Path;
use thiserror::Error;
use tokio::fs;
use crate::modules::shared::asynchronous::live_json_file_watcher::LiveJsonFileWatcher;

pub type ShipLockerFileWatcher = LiveJsonFileWatcher<ShipLocker>;
pub use crate::modules::shared::asynchronous::live_json_file_watcher::LiveJsonFileWatcherError as ShipLockerFileWatcherError;
use crate::ship_locker::ShipLocker;

pub async fn read_ship_locker_file<P: AsRef<Path>>(path: P) -> Result<ShipLocker, ReadShipLockerFileError> {
Ok(serde_json::from_str(&fs::read_to_string(path)
.await?)?)
}

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

#[error("Failed to parse ship locker file: {0}")]
SerdeJson(#[from] serde_json::Error),
}
42 changes: 42 additions & 0 deletions src/modules/ship_locker/blocking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::fs;
use std::path::Path;
use thiserror::Error;
use crate::modules::shared::blocking::live_json_file_watcher::LiveJsonFileWatcher;

pub type ShipLockerFileWatcher = LiveJsonFileWatcher<ShipLocker>;
pub use crate::modules::shared::blocking::live_json_file_watcher::LiveJsonFileWatcherError as ShipLockerFileWatcherError;
use crate::ship_locker::ShipLocker;

pub fn read_ship_locker_file<P: AsRef<Path>>(path: P) -> Result<ShipLocker, ReadShipLockerFileError> {
Ok(serde_json::from_str(&fs::read_to_string(path)?)?)
}

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

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

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

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

let result = read_ship_locker_file(&path);

dbg!(&result);

assert!(result.is_ok());
}
}
8 changes: 8 additions & 0 deletions src/modules/ship_locker/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
mod models;
pub mod blocking;

#[cfg(feature = "asynchronous")]
pub mod asynchronous;

pub use models::ship_locker::ShipLocker;
pub use models::ship_locker_entry::ShipLockerEntry;
2 changes: 2 additions & 0 deletions src/modules/ship_locker/models.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod ship_locker;
pub mod ship_locker_entry;
17 changes: 17 additions & 0 deletions src/modules/ship_locker/models/ship_locker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use chrono::{DateTime, Utc};
use serde::Deserialize;
use crate::ship_locker::ShipLockerEntry;

#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "PascalCase")]
pub struct ShipLocker {
#[serde(rename = "timestamp")]
pub timestamp: DateTime<Utc>,

#[serde(rename = "event")]
pub event: String,
pub items: Vec<ShipLockerEntry>,
pub components: Vec<ShipLockerEntry>,
pub consumables: Vec<ShipLockerEntry>,
pub data: Vec<ShipLockerEntry>,
}
15 changes: 15 additions & 0 deletions src/modules/ship_locker/models/ship_locker_entry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use serde::Deserialize;
use crate::models::odyssey::item::Item;

#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "PascalCase")]
pub struct ShipLockerEntry {
pub name: Item,

#[serde(rename = "Name_Localised")]
pub name_localized: Option<String>,

#[serde(rename = "OwnerID")]
pub owner_id: u64,
pub count: u16,
}

0 comments on commit 61846a0

Please sign in to comment.