From 6b4ef5fc0af275827a539c2f08f5dec097d82c0a Mon Sep 17 00:00:00 2001 From: Chad Baker Date: Tue, 14 Jan 2025 14:41:48 -0700 Subject: [PATCH] skip serializing history if empty --- rust/altrios-core/src/consist/consist_model.rs | 1 + rust/altrios-core/src/consist/locomotive/locomotive_model.rs | 2 +- .../src/consist/locomotive/powertrain/electric_drivetrain.rs | 5 ++++- .../src/consist/locomotive/powertrain/fuel_converter.rs | 5 ++++- .../src/consist/locomotive/powertrain/generator.rs | 2 +- .../locomotive/powertrain/reversible_energy_storage.rs | 5 ++++- rust/altrios-core/src/train/friction_brakes.rs | 2 +- rust/altrios-core/src/train/set_speed_train_sim.rs | 2 +- rust/altrios-core/src/train/speed_limit_train_sim.rs | 2 +- 9 files changed, 18 insertions(+), 8 deletions(-) diff --git a/rust/altrios-core/src/consist/consist_model.rs b/rust/altrios-core/src/consist/consist_model.rs index e48d4912..2a217fcf 100644 --- a/rust/altrios-core/src/consist/consist_model.rs +++ b/rust/altrios-core/src/consist/consist_model.rs @@ -115,6 +115,7 @@ pub struct Consist { #[serde(default)] #[serde(skip_serializing_if = "EqDefault::eq_default")] pub state: ConsistState, + #[serde(skip_serializing_if = "ConsistStateHistoryVec::is_empty", default)] /// Custom vector of [Self::state] pub history: ConsistStateHistoryVec, #[api(skip_set, skip_get)] // custom needed for this diff --git a/rust/altrios-core/src/consist/locomotive/locomotive_model.rs b/rust/altrios-core/src/consist/locomotive/locomotive_model.rs index 1a7c2b91..13751580 100644 --- a/rust/altrios-core/src/consist/locomotive/locomotive_model.rs +++ b/rust/altrios-core/src/consist/locomotive/locomotive_model.rs @@ -617,7 +617,7 @@ pub struct Locomotive { #[api(skip_set, skip_get)] save_interval: Option, /// Custom vector of [Self::state] - #[serde(default)] + #[serde(skip_serializing_if = "LocomotiveStateHistoryVec::is_empty", default)] pub history: LocomotiveStateHistoryVec, #[serde(default = "utils::return_true")] /// If true, requires power demand to not exceed consist diff --git a/rust/altrios-core/src/consist/locomotive/powertrain/electric_drivetrain.rs b/rust/altrios-core/src/consist/locomotive/powertrain/electric_drivetrain.rs index 8d3c629f..244af0d2 100644 --- a/rust/altrios-core/src/consist/locomotive/powertrain/electric_drivetrain.rs +++ b/rust/altrios-core/src/consist/locomotive/powertrain/electric_drivetrain.rs @@ -76,7 +76,10 @@ pub struct ElectricDrivetrain { /// Time step interval between saves. 1 is a good option. If None, no saving occurs. pub save_interval: Option, /// Custom vector of [Self::state] - #[serde(default)] + #[serde( + skip_serializing_if = "ElectricDrivetrainStateHistoryVec::is_empty", + default + )] pub history: ElectricDrivetrainStateHistoryVec, } diff --git a/rust/altrios-core/src/consist/locomotive/powertrain/fuel_converter.rs b/rust/altrios-core/src/consist/locomotive/powertrain/fuel_converter.rs index 16c431fc..11d06b27 100644 --- a/rust/altrios-core/src/consist/locomotive/powertrain/fuel_converter.rs +++ b/rust/altrios-core/src/consist/locomotive/powertrain/fuel_converter.rs @@ -74,7 +74,10 @@ pub struct FuelConverter { /// time step interval between saves. 1 is a good option. If None, no saving occurs. pub save_interval: Option, /// Custom vector of [Self::state] - #[serde(default)] + #[serde( + skip_serializing_if = "FuelConverterStateHistoryVec::is_empty", + default + )] pub history: FuelConverterStateHistoryVec, // TODO: spec out fuel tank size and track kg of fuel } diff --git a/rust/altrios-core/src/consist/locomotive/powertrain/generator.rs b/rust/altrios-core/src/consist/locomotive/powertrain/generator.rs index 4f786726..10fe88bd 100644 --- a/rust/altrios-core/src/consist/locomotive/powertrain/generator.rs +++ b/rust/altrios-core/src/consist/locomotive/powertrain/generator.rs @@ -102,7 +102,7 @@ pub struct Generator { /// Time step interval between saves. 1 is a good option. If None, no saving occurs. pub save_interval: Option, /// Custom vector of [Self::state] - #[serde(default)] + #[serde(skip_serializing_if = "GeneratorStateHistoryVec::is_empty", default)] pub history: GeneratorStateHistoryVec, } diff --git a/rust/altrios-core/src/consist/locomotive/powertrain/reversible_energy_storage.rs b/rust/altrios-core/src/consist/locomotive/powertrain/reversible_energy_storage.rs index e85a6df2..119464f8 100644 --- a/rust/altrios-core/src/consist/locomotive/powertrain/reversible_energy_storage.rs +++ b/rust/altrios-core/src/consist/locomotive/powertrain/reversible_energy_storage.rs @@ -186,7 +186,10 @@ pub struct ReversibleEnergyStorage { pub soc_lo_ramp_start: Option, /// Time step interval at which history is saved pub save_interval: Option, - #[serde(default)] + #[serde( + skip_serializing_if = "ReversibleEnergyStorageStateHistoryVec::is_empty", + default + )] /// Custom vector of [Self::state] pub history: ReversibleEnergyStorageStateHistoryVec, } diff --git a/rust/altrios-core/src/train/friction_brakes.rs b/rust/altrios-core/src/train/friction_brakes.rs index 8a5752c7..c982238f 100644 --- a/rust/altrios-core/src/train/friction_brakes.rs +++ b/rust/altrios-core/src/train/friction_brakes.rs @@ -43,7 +43,7 @@ pub struct FricBrake { #[serde(default)] #[serde(skip_serializing_if = "EqDefault::eq_default")] pub state: FricBrakeState, - #[serde(default)] + #[serde(skip_serializing_if = "FricBrakeStateHistoryVec::is_empty", default)] /// Custom vector of [Self::state] pub history: FricBrakeStateHistoryVec, pub save_interval: Option, diff --git a/rust/altrios-core/src/train/set_speed_train_sim.rs b/rust/altrios-core/src/train/set_speed_train_sim.rs index 69999853..75ec44ee 100644 --- a/rust/altrios-core/src/train/set_speed_train_sim.rs +++ b/rust/altrios-core/src/train/set_speed_train_sim.rs @@ -289,8 +289,8 @@ pub struct SetSpeedTrainSim { pub train_res: TrainRes, #[api(skip_set)] path_tpc: PathTpc, - #[serde(default)] /// Custom vector of [Self::state] + #[serde(skip_serializing_if = "TrainStateHistoryVec::is_empty", default)] pub history: TrainStateHistoryVec, #[api(skip_set, skip_get)] save_interval: Option, diff --git a/rust/altrios-core/src/train/speed_limit_train_sim.rs b/rust/altrios-core/src/train/speed_limit_train_sim.rs index 167acbb8..0b81c7fa 100644 --- a/rust/altrios-core/src/train/speed_limit_train_sim.rs +++ b/rust/altrios-core/src/train/speed_limit_train_sim.rs @@ -152,8 +152,8 @@ pub struct SpeedLimitTrainSim { #[api(skip_set)] pub braking_points: BrakingPoints, pub fric_brake: FricBrake, - #[serde(default)] /// Custom vector of [Self::state] + #[serde(skip_serializing_if = "TrainStateHistoryVec::is_empty", default)] pub history: TrainStateHistoryVec, #[api(skip_set, skip_get)] save_interval: Option,