shift state enum

This commit is contained in:
Alex Janka 2024-01-18 10:01:08 +11:00
parent 64aa35a969
commit b13c40429f

View file

@ -7,7 +7,7 @@ use crate::{
get, get_args, post_arg, post_arg_empty, ApiValues, Empty, ExternalVehicleId, FleetApi, get, get_args, post_arg, post_arg_empty, ApiValues, Empty, ExternalVehicleId, FleetApi,
FleetVehicleApi, OwnerApi, VehicleApi, VehicleId, FleetVehicleApi, OwnerApi, VehicleApi, VehicleId,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Deserializer, Serialize};
use strum::{Display, EnumString}; use strum::{Display, EnumString};
#[rustfmt::skip] #[rustfmt::skip]
@ -317,7 +317,8 @@ pub struct DriveState {
pub native_type: Option<String>, pub native_type: Option<String>,
pub power: i64, pub power: i64,
pub shift_state: Option<String>, #[serde(deserialize_with = "map_null_to_default")]
pub shift_state: ShiftState,
/// gak: I've assumed this to be String. /// gak: I've assumed this to be String.
pub speed: Option<i64>, pub speed: Option<i64>,
pub timestamp: i64, pub timestamp: i64,
@ -325,6 +326,33 @@ pub struct DriveState {
pub active_route_traffic_minutes_delay: Option<f64>, pub active_route_traffic_minutes_delay: Option<f64>,
} }
#[derive(Debug, Clone, Copy, Display, Deserialize, Serialize, PartialEq)]
pub enum ShiftState {
#[serde(alias = "D")]
Drive,
#[serde(alias = "N")]
Neutral,
#[serde(alias = "R")]
Reverse,
#[serde(other)]
#[serde(alias = "P")]
Parked,
}
impl Default for ShiftState {
fn default() -> Self {
Self::Parked
}
}
fn map_null_to_default<'de, D, T>(deserializer: D) -> Result<T, D::Error>
where
T: Default + Deserialize<'de>,
D: Deserializer<'de>,
{
Ok(Option::deserialize(deserializer)?.unwrap_or_default())
}
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GuiSettings { pub struct GuiSettings {
pub gui_24_hour_time: bool, pub gui_24_hour_time: bool,