-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathmod.rs
284 lines (257 loc) · 8.11 KB
/
mod.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
pub mod light_rolling_stock;
pub mod rolling_stock_livery;
use diesel_json::Json as DieselJson;
use serde::{Deserialize, Deserializer, Serialize};
use std::collections::HashMap;
use strum_macros::{Display, EnumString};
use utoipa::ToSchema;
use crate::{
models::rolling_stock::rolling_stock_livery::RollingStockLiveryMetadata,
schema::rolling_stock::rolling_stock_livery::RollingStockLivery,
};
crate::schemas! {
RollingStockComfortType,
RollingStockCommon,
RollingStockWithLiveries,
RollingResistance,
RollingStockMetadata,
RollingStockComfortType,
Gamma,
EffortCurve,
EffortCurves,
EffortCurveConditions,
ConditionalEffortCurve,
ModeEffortCurves,
EnergySource,
SpeedDependantPower,
EnergyStorage,
RefillLaw,
RollingStockLivery,
light_rolling_stock::schemas(),
}
pub const ROLLING_STOCK_RAILJSON_VERSION: &str = "3.2";
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]
pub struct RollingStockCommon {
pub name: String,
pub effort_curves: EffortCurves,
#[schema(example = "5", required)]
pub base_power_class: Option<String>,
pub length: f64,
pub max_speed: f64,
pub startup_time: f64,
pub startup_acceleration: f64,
pub comfort_acceleration: f64,
pub gamma: Gamma,
pub inertia_coefficient: f64,
pub mass: f64,
pub rolling_resistance: RollingResistance,
#[schema(value_type = LoadingGaugeType)]
pub loading_gauge: String,
/// Mapping of power restriction code to power class
#[schema(value_type = HashMap<String, String>)]
pub power_restrictions: Option<DieselJson<HashMap<String, String>>>,
#[serde(default)]
pub energy_sources: Vec<EnergySource>,
/// The time the train takes before actually using electrical power (in seconds). Is null if the train is not electric.
#[schema(example = 5.0)]
pub electrical_power_startup_time: Option<f64>,
/// The time it takes to raise this train's pantograph in seconds. Is null if the train is not electric.
#[schema(example = 15.0)]
pub raise_pantograph_time: Option<f64>,
pub supported_signaling_systems: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, ToSchema)]
pub struct RollingStock {
pub id: i64,
#[serde(flatten)]
pub common: RollingStockCommon,
pub railjson_version: String,
/// Whether the rolling stock can be edited/deleted or not.
pub locked: bool,
pub metadata: RollingStockMetadata,
}
#[derive(Debug, Serialize, ToSchema)]
pub struct RollingStockWithLiveries {
#[serde(flatten)]
pub rolling_stock: RollingStock,
pub liveries: Vec<RollingStockLiveryMetadata>,
}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize, ToSchema)]
#[serde(deny_unknown_fields)]
pub struct Gamma {
#[serde(rename = "type")]
gamma_type: String,
value: f64,
}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize, ToSchema)]
#[serde(deny_unknown_fields)]
#[allow(non_snake_case)]
pub struct RollingResistance {
#[serde(rename = "type")]
rolling_resistance_type: String,
A: f64,
B: f64,
C: f64,
}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize, ToSchema)]
#[serde(deny_unknown_fields)]
pub struct RollingStockMetadata {
detail: String,
family: String,
#[serde(rename = "type")]
rolling_stock_type: String,
grouping: String,
series: String,
subseries: String,
unit: String,
number: String,
reference: String,
}
// Effort curves schema
#[derive(
Display, Clone, Default, Debug, EnumString, PartialEq, Deserialize, Serialize, ToSchema,
)]
#[strum(serialize_all = "UPPERCASE")]
#[serde(rename_all = "UPPERCASE")]
pub enum RollingStockComfortType {
#[default]
Standard,
AC,
Heating,
}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize, ToSchema)]
#[serde(deny_unknown_fields)]
pub struct EffortCurveConditions {
#[schema(required)]
comfort: Option<RollingStockComfortType>,
#[schema(required)]
electrical_profile_level: Option<String>,
#[schema(required)]
power_restriction_code: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize, ToSchema)]
#[serde(deny_unknown_fields)]
pub struct ConditionalEffortCurve {
cond: EffortCurveConditions,
curve: EffortCurve,
}
#[derive(Clone, Debug, PartialEq, Serialize, ToSchema)]
#[serde(deny_unknown_fields)]
pub struct EffortCurve {
#[schema(min_items = 2, example = json!([0.0, 2.958, 46.719]))]
speeds: Vec<f64>,
#[schema(min_items = 2, example = json!([23500.0, 23200.0, 21200.0]))]
max_efforts: Vec<f64>,
}
impl<'de> Deserialize<'de> for EffortCurve {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
struct InnerParams {
speeds: Vec<f64>,
max_efforts: Vec<f64>,
}
let inner = InnerParams::deserialize(deserializer)?;
// Validate the curve
if inner.max_efforts.len() != inner.speeds.len() {
return Err(serde::de::Error::custom(
"effort curve invalid, max_efforts and speeds arrays should have the same length",
));
}
Ok(EffortCurve {
speeds: inner.speeds,
max_efforts: inner.max_efforts,
})
}
}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize, ToSchema)]
#[serde(deny_unknown_fields)]
pub struct ModeEffortCurves {
curves: Vec<ConditionalEffortCurve>,
default_curve: EffortCurve,
pub is_electric: bool,
}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize, ToSchema)]
#[serde(deny_unknown_fields)]
pub struct EffortCurves {
pub modes: HashMap<String, ModeEffortCurves>,
default_mode: String,
}
// Energy sources schema
/// physical law defining how the storage can be refilled
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize, ToSchema)]
#[serde(deny_unknown_fields)]
pub struct RefillLaw {
#[schema(minimum = 0)]
tau: f64,
#[schema(minimum = 0, maximum = 1)]
soc_ref: f64,
}
/// energy storage of an energy source (of a rolling stock, can be a electrical battery or a hydrogen/fuel powerPack)
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize, ToSchema)]
#[serde(deny_unknown_fields)]
pub struct EnergyStorage {
capacity: f64,
#[schema(minimum = 0, maximum = 1)]
soc: f64,
#[schema(minimum = 0, maximum = 1)]
soc_min: f64,
#[schema(minimum = 0, maximum = 1)]
soc_max: f64,
#[schema(required)]
refill_law: Option<RefillLaw>,
}
/// power-speed curve (in an energy source)
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize, ToSchema)]
#[serde(deny_unknown_fields)]
pub struct SpeedDependantPower {
speeds: Vec<f64>,
powers: Vec<f64>,
}
/// energy source of a rolling stock
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize, ToSchema)]
#[serde(tag = "energy_source_type", deny_unknown_fields)]
pub enum EnergySource {
/// energy source for a rolling stock representing a electrification
Electrification {
max_input_power: SpeedDependantPower,
max_output_power: SpeedDependantPower,
#[schema(minimum = 0, maximum = 1)]
efficiency: f64,
},
PowerPack {
max_input_power: SpeedDependantPower,
max_output_power: SpeedDependantPower,
energy_storage: EnergyStorage,
#[schema(minimum = 0, maximum = 1)]
efficiency: f64,
},
/// energy source for a rolling stock representing a battery
Battery {
max_input_power: SpeedDependantPower,
max_output_power: SpeedDependantPower,
energy_storage: EnergyStorage,
#[schema(minimum = 0, maximum = 1)]
efficiency: f64,
},
}
#[cfg(test)]
mod tests {
use crate::schema::rolling_stock::EffortCurve;
#[test]
fn test_de_effort_curve_valid() {
assert!(serde_json::from_str::<EffortCurve>(
r#"{ "speeds": [0, 1], "max_efforts": [0, 2] }"#
)
.is_ok());
}
#[test]
fn test_de_effort_curve_unvalid() {
assert!(
serde_json::from_str::<EffortCurve>(r#"{ "speeds": [0, 1], "max_efforts": [] }"#)
.is_err()
);
}
}