Skip to content

Commit

Permalink
Merge pull request #14 from rster2002/json-files-outfitting
Browse files Browse the repository at this point in the history
Json files outfitting
  • Loading branch information
rster2002 committed May 15, 2024
2 parents ecb4c1d + 5678191 commit a727fea
Show file tree
Hide file tree
Showing 12 changed files with 1,248 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ async-mutex = { version = "1.4.0", optional = true }
tokio-test = "0.4.4"

[features]
default = []
default = ["asynchronous", "strict"]
asynchronous = ["dep:tokio", "dep:async-mutex"]
strict = []
3 changes: 0 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ mod tests {
use std::env::current_dir;
use crate::logs::LogDir;
use crate::logs::content::LogEventContent;
// use crate::blocking::JournalDir;
//
// use crate::modules::logs::content::log_event_content::JournalEventContent;

#[test]
fn test_journals_are_parsed_correctly() {
Expand Down
1 change: 0 additions & 1 deletion src/modules/models/exploration/species.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::str::FromStr;
use serde::{Serialize, Deserialize};
use serde_json::Value;
use crate::modules::exobiology::models::spawn_condition::SpawnCondition;
use crate::modules::models::galaxy::atmosphere::{Atmosphere, AtmosphereDensity};
use crate::modules::models::galaxy::atmosphere_type::AtmosphereType;
use crate::modules::models::galaxy::planet_class::PlanetClass;
use crate::modules::models::galaxy::star_class::StarClass;
Expand Down
1 change: 1 addition & 0 deletions src/modules/models/exploration/variant_color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,7 @@ impl TryFrom<(&Species, &VariantSource)> for VariantColor {

#[cfg(feature = "strict")]
(_, _, _) => {
#[cfg(test)]
dbg!(value);
return Err(VariantColorError::UnknownVariant);
}
Expand Down
28 changes: 28 additions & 0 deletions src/modules/models/ship/ship_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,31 @@ impl Display for ShipModule {
}
}
}

#[cfg(test)]
mod tests {
use serde_json::{Error, Value};
use crate::models::ship::ship_module::ShipModule;

#[test]
fn modules_are_parsed_correctly() {
let mut test_cases = include_str!("zz_ship_module_test_cases.txt")
.lines();

let mut count = 0;

for line in test_cases {
let result = serde_json::from_value::<ShipModule>(Value::String(line.to_string()));
count += 1;

if result.is_err() {
dbg!(&line);
dbg!(&result);
}

assert!(result.is_ok());
}

assert!(count > 1000);
}
}
3 changes: 3 additions & 0 deletions src/modules/models/ship/ship_module/module_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ impl From<&ArmorGrade> for ModuleClass {

#[cfg(not(feature = "strict"))]
ArmorGrade::Unknown(_) => ModuleClass::Unknown,

#[cfg(not(feature = "strict"))]
ArmorGrade::UnknownString(_) => ModuleClass::Unknown,
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ pub enum HardpointModule {
#[serde(rename = "dumbfiremissilerack")]
MissileRack,

#[serde(rename = "dumbfiremissilerack_advanced")]
AdvancedMissileRack,

#[serde(rename = "basicmissilerack")]
SeekerMissileRack,

Expand Down Expand Up @@ -173,6 +176,9 @@ pub enum HardpointModule {
#[serde(rename = "railgun_burst")]
ImperialHammerRailGun,

#[serde(rename = "xenoscanner_basic")]
XenoScanner,

#[cfg(not(feature = "strict"))]
#[serde(untagged)]
Unknown(String),
Expand All @@ -196,7 +202,13 @@ impl HardpointModule {
| HardpointModule::ThargoidPulseNeutralizer
| HardpointModule::ShutdownFieldNeutralizer
| HardpointModule::ElectronicCountermeasures
| HardpointModule::WakeScanner => HardpointType::Utility,
| HardpointModule::WakeScanner
| HardpointModule::PulseWaveAnalyzer
| HardpointModule::ManifestScanner
| HardpointModule::KillWarrantScanner
| HardpointModule::EnhancedXenoScanner
| HardpointModule::XenoScanner
| HardpointModule::ShieldBooster => HardpointType::Utility,

_ => HardpointType::FullSized,
}
Expand All @@ -209,6 +221,23 @@ impl HardpointModule {
pub fn is_utility(&self) -> bool {
matches!(self.hardpoint_type(), HardpointType::Utility)
}

pub fn is_powerplay_module(&self) -> bool {
match self {
HardpointModule::RetributorBeamLaser
| HardpointModule::CytoscramblerBurstLaser
| HardpointModule::PacifierFragCannon
| HardpointModule::MiningLanceBeamLaser
| HardpointModule::PackHoundMissileRack
| HardpointModule::RocketPropelledFSDDisruptor
| HardpointModule::EnforcerCannon
| HardpointModule::AdvancedPlasmaAccelerator
| HardpointModule::PulseDisruptorLaser
| HardpointModule::ImperialHammerRailGun => true,

_ => false,
}
}
}

impl Display for HardpointModule {
Expand Down Expand Up @@ -244,6 +273,36 @@ impl Display for HardpointModule {
HardpointModule::WakeScanner => "Wake Scanner",
HardpointModule::PulseWaveAnalyzer => "Pulse Wave Analyzer",
HardpointModule::RailGun => "Rail Gun",
HardpointModule::RetributorBeamLaser => "Retributor Beam Laser",
HardpointModule::PulseDisruptorLaser => "Pulse Disruptor Laser",
HardpointModule::BurstLaser => "Burst Laser",
HardpointModule::CytoscramblerBurstLaser => "Cytoscrambler Burst Laser",
HardpointModule::FragmentCannon => "Fragment Cannon",
HardpointModule::PacifierFragCannon => "Pacifier Frag-Cannon",
HardpointModule::Cannon => "Cannon",
HardpointModule::AdvancedMultiCannon => "Advanced Multi-Cannon",
HardpointModule::EnforcerCannon => "Enforcer Cannon",
HardpointModule::RocketPropelledFSDDisruptor => "Rocket Propelled FSD Disruptor",
HardpointModule::GuardianPlasmaCharger => "Guardian Plasma Charger",
HardpointModule::GuardianShardCannon => "Guardian Shard Cannon",
HardpointModule::KillWarrantScanner => "Kill Warrant Scanner",
HardpointModule::ManifestScanner => "Manifest Scanner",
HardpointModule::ShockCannon => "Shock Cannon",
HardpointModule::SeekerMissileRack => "Seeker Missile Rack",
HardpointModule::PackHoundMissileRack => "Pack-Hound Missile Rack",
HardpointModule::ShockMineLauncher => "Shock Mine Launcher",
HardpointModule::MineLauncher => "Mine Launcher",
HardpointModule::TorpedoPylon => "Torpedo Pylon",
HardpointModule::PlasmaAccelerator => "Plasma Accelerator",
HardpointModule::AdvancedPlasmaAccelerator => "Advanced Plasma Accelerator",
HardpointModule::EnhancedAXMultiCannon => "Enhanced AX Multi-Cannon",
HardpointModule::RemoteReleaseFlechetteLauncher => "Remote Release Flechette Launcher",
HardpointModule::EnzymeMissileRack => "Enzyme Missile Rack",
HardpointModule::MiningLanceBeamLaser => "Mining Lance Beam Laser",
HardpointModule::SubSurfaceExtractionMissile => "Sub-Surface Extraction Missile",
HardpointModule::ImperialHammerRailGun => "Imperial Hammer Rail Gun",
HardpointModule::XenoScanner => "Xeno Scanner",
HardpointModule::AdvancedMissileRack => "Advanced Missile Rack",

#[cfg(not(feature = "strict"))]
HardpointModule::Unknown(unknown) => return write!(f, "Unknown: {}", unknown),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fmt::{Display, Formatter};
use std::str::FromStr;
use serde::Serialize;

use thiserror::Error;
Expand All @@ -13,12 +14,18 @@ pub enum ArmorGrade {

#[cfg(not(feature = "strict"))]
Unknown(u8),

#[cfg(not(feature = "strict"))]
UnknownString(String),
}

#[derive(Debug, Error)]
pub enum ArmorGradeError {
#[error("Unknown armor grade: {0}")]
UnknownArmorGrade(u8),

#[error("Unknown armor grade string: '{0}'")]
UnknownArmorGradeString(String),
}

impl TryFrom<u8> for ArmorGrade {
Expand All @@ -41,6 +48,23 @@ impl TryFrom<u8> for ArmorGrade {
}
}

impl FromStr for ArmorGrade {
type Err = ArmorGradeError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"reactive" => Ok(ArmorGrade::ReactiveSurfaceComposite),
"mirrored" => Ok(ArmorGrade::MirroredSurfaceComposite),

#[cfg(not(feature = "strict"))]
_ => Ok(ArmorGrade::UnknownString(s.to_string())),

#[cfg(feature = "strict")]
_ => Err(ArmorGradeError::UnknownArmorGradeString(s.to_string())),
}
}
}

impl Display for ArmorGrade {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
Expand All @@ -55,6 +79,9 @@ impl Display for ArmorGrade {

#[cfg(not(feature = "strict"))]
ArmorGrade::Unknown(_) => "Unknown composite",

#[cfg(not(feature = "strict"))]
ArmorGrade::UnknownString(_) => "Unknown composite",
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub enum ArmorModuleError {

lazy_static! {
static ref ARMOR_MODULE_REGEX: Regex =
Regex::new(r#"^\$?(\w+?)_armour_grade(\d+)(_name;)?$"#).unwrap();
Regex::new(r#"^\$?(\w+?)_armour(_grade(\d+)|_(\w+))(_name;)?$"#).unwrap();
}

impl FromStr for ArmorModule {
Expand All @@ -60,14 +60,24 @@ impl FromStr for ArmorModule {
.parse()
.map_err(ArmorModuleError::FailedToParseShipType)?;

let grade = captures
.get(2)
.expect("Should have already been matched")
.as_str()
.parse::<u8>()?
.try_into()?;
if let Some(capture) = captures.get(3) {
return Ok(ArmorModule {
ship,
grade: capture.as_str()
.parse::<u8>()?
.try_into()?,
});
}

if let Some(capture) = captures.get(4) {
return Ok(ArmorModule {
ship,
grade: capture.as_str()
.parse()?,
});
}

Ok(ArmorModule { ship, grade })
Err(ArmorModuleError::FailedToParse(s.to_string()))
}
}

Expand Down Expand Up @@ -112,6 +122,13 @@ mod tests {
grade: ArmorGrade::MirroredSurfaceComposite,
},
),
(
"dolphin_armour_reactive",
ArmorModule {
ship: ShipType::Dolphin,
grade: ArmorGrade::ReactiveSurfaceComposite,
},
),
];

for (case, expected) in test_cases {
Expand Down
Loading

0 comments on commit a727fea

Please sign in to comment.