use core::fmt;
#[cfg(feature = "serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer, de, ser::SerializeStruct};
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Event(pub u8);
impl Event {
pub const TIMER: Event = Event(0);
pub const WORKOUT: Event = Event(3);
pub const WORKOUT_STEP: Event = Event(4);
pub const POWER_DOWN: Event = Event(5);
pub const POWER_UP: Event = Event(6);
pub const OFF_COURSE: Event = Event(7);
pub const SESSION: Event = Event(8);
pub const LAP: Event = Event(9);
pub const COURSE_POINT: Event = Event(10);
pub const BATTERY: Event = Event(11);
pub const VIRTUAL_PARTNER_PACE: Event = Event(12);
pub const HR_HIGH_ALERT: Event = Event(13);
pub const HR_LOW_ALERT: Event = Event(14);
pub const SPEED_HIGH_ALERT: Event = Event(15);
pub const SPEED_LOW_ALERT: Event = Event(16);
pub const CAD_HIGH_ALERT: Event = Event(17);
pub const CAD_LOW_ALERT: Event = Event(18);
pub const POWER_HIGH_ALERT: Event = Event(19);
pub const POWER_LOW_ALERT: Event = Event(20);
pub const RECOVERY_HR: Event = Event(21);
pub const BATTERY_LOW: Event = Event(22);
pub const TIME_DURATION_ALERT: Event = Event(23);
pub const DISTANCE_DURATION_ALERT: Event = Event(24);
pub const CALORIE_DURATION_ALERT: Event = Event(25);
pub const ACTIVITY: Event = Event(26);
pub const FITNESS_EQUIPMENT: Event = Event(27);
pub const LENGTH: Event = Event(28);
pub const USER_MARKER: Event = Event(32);
pub const SPORT_POINT: Event = Event(33);
pub const CALIBRATION: Event = Event(36);
pub const FRONT_GEAR_CHANGE: Event = Event(42);
pub const REAR_GEAR_CHANGE: Event = Event(43);
pub const RIDER_POSITION_CHANGE: Event = Event(44);
pub const ELEV_HIGH_ALERT: Event = Event(45);
pub const ELEV_LOW_ALERT: Event = Event(46);
pub const COMM_TIMEOUT: Event = Event(47);
pub const AUTO_ACTIVITY_DETECT: Event = Event(54);
pub const DIVE_ALERT: Event = Event(56);
pub const DIVE_GAS_SWITCHED: Event = Event(57);
pub const TANK_PRESSURE_RESERVE: Event = Event(71);
pub const TANK_PRESSURE_CRITICAL: Event = Event(72);
pub const TANK_LOST: Event = Event(73);
pub const RADAR_THREAT_ALERT: Event = Event(75);
pub const TANK_BATTERY_LOW: Event = Event(76);
pub const TANK_POD_CONNECTED: Event = Event(81);
pub const TANK_POD_DISCONNECTED: Event = Event(82);
fn as_str(self) -> Option<&'static str> {
match self.0 {
0 => Some("timer"),
3 => Some("workout"),
4 => Some("workout_step"),
5 => Some("power_down"),
6 => Some("power_up"),
7 => Some("off_course"),
8 => Some("session"),
9 => Some("lap"),
10 => Some("course_point"),
11 => Some("battery"),
12 => Some("virtual_partner_pace"),
13 => Some("hr_high_alert"),
14 => Some("hr_low_alert"),
15 => Some("speed_high_alert"),
16 => Some("speed_low_alert"),
17 => Some("cad_high_alert"),
18 => Some("cad_low_alert"),
19 => Some("power_high_alert"),
20 => Some("power_low_alert"),
21 => Some("recovery_hr"),
22 => Some("battery_low"),
23 => Some("time_duration_alert"),
24 => Some("distance_duration_alert"),
25 => Some("calorie_duration_alert"),
26 => Some("activity"),
27 => Some("fitness_equipment"),
28 => Some("length"),
32 => Some("user_marker"),
33 => Some("sport_point"),
36 => Some("calibration"),
42 => Some("front_gear_change"),
43 => Some("rear_gear_change"),
44 => Some("rider_position_change"),
45 => Some("elev_high_alert"),
46 => Some("elev_low_alert"),
47 => Some("comm_timeout"),
54 => Some("auto_activity_detect"),
56 => Some("dive_alert"),
57 => Some("dive_gas_switched"),
71 => Some("tank_pressure_reserve"),
72 => Some("tank_pressure_critical"),
73 => Some("tank_lost"),
75 => Some("radar_threat_alert"),
76 => Some("tank_battery_low"),
81 => Some("tank_pod_connected"),
82 => Some("tank_pod_disconnected"),
_ => None,
}
}
}
impl Default for Event {
fn default() -> Self {
Self(u8::MAX)
}
}
impl fmt::Display for Event {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.as_str() {
Some(s) => write!(f, "{}", s),
None => write!(f, "Event({})", self.0),
}
}
}
#[cfg(feature = "serde")]
impl Serialize for Event {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut state = serializer.serialize_struct("Event", 2)?;
if let Some(s) = self.as_str() {
state.serialize_field("t", s)?;
}
state.serialize_field("c", &self.0)?;
state.end()
}
}
#[cfg(feature = "serde")]
#[cfg_attr(feature = "serde", derive(Deserialize))]
struct De<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
t: Option<&'a str>,
c: u8,
}
#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for Event {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let repr = De::deserialize(deserializer)?;
let v = Self(repr.c);
if let Some(t) = repr.t
&& let Some(s) = v.as_str()
&& t != s
{
return Err(de::Error::custom("tag and content mismatch"));
}
Ok(v)
}
}