Skip to main content

routee_compass_core/model/state/
input_feature.rs

1use serde::{Deserialize, Serialize};
2
3use crate::model::unit::{
4    DistanceUnit, EnergyUnit, RatioUnit, SpeedUnit, TemperatureUnit, TimeUnit,
5};
6
7/// defines the required input feature and its requested unit type for a given state variable
8///
9/// if a unit type is provided, then the state variable is provided in the requested unit to the model.
10#[derive(Serialize, Deserialize, Clone, Debug, Hash, Eq, PartialEq)]
11#[serde(tag = "type", rename_all = "snake_case")]
12pub enum InputFeature {
13    Distance {
14        name: String,
15        unit: Option<DistanceUnit>,
16    },
17    Speed {
18        name: String,
19        unit: Option<SpeedUnit>,
20    },
21    Time {
22        name: String,
23        unit: Option<TimeUnit>,
24    },
25    Energy {
26        name: String,
27        unit: Option<EnergyUnit>,
28    },
29    Ratio {
30        name: String,
31        unit: Option<RatioUnit>,
32    },
33    Temperature {
34        name: String,
35        unit: Option<TemperatureUnit>,
36    },
37    Custom {
38        name: String,
39        unit: String,
40    },
41}
42
43impl InputFeature {
44    pub fn name(&self) -> String {
45        match self {
46            InputFeature::Distance { name, .. } => name.to_owned(),
47            InputFeature::Speed { name, .. } => name.to_owned(),
48            InputFeature::Time { name, .. } => name.to_owned(),
49            InputFeature::Energy { name, .. } => name.to_owned(),
50            InputFeature::Ratio { name, .. } => name.to_owned(),
51            InputFeature::Temperature { name, .. } => name.to_owned(),
52            InputFeature::Custom { name, .. } => name.to_owned(),
53        }
54    }
55}
56
57impl std::fmt::Display for InputFeature {
58    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59        let s = serde_json::to_string_pretty(self).unwrap_or_default();
60        write!(f, "{s}")
61    }
62}