use num_enum::{IntoPrimitive, TryFromPrimitive};
use serde::{Deserialize, Serialize};
#[derive(
Debug, Clone, Copy, PartialEq, Eq, IntoPrimitive, TryFromPrimitive, Serialize, Deserialize,
)]
#[repr(u8)]
pub enum SmartShiftMode {
Free = 1,
Ratchet = 2,
}
impl SmartShiftMode {
#[must_use]
pub fn flipped(self) -> Self {
match self {
Self::Free => Self::Ratchet,
Self::Ratchet => Self::Free,
}
}
}
impl From<crate::config::WheelMode> for SmartShiftMode {
fn from(mode: crate::config::WheelMode) -> Self {
match mode {
crate::config::WheelMode::Free => Self::Free,
crate::config::WheelMode::Ratchet => Self::Ratchet,
}
}
}
impl From<SmartShiftMode> for crate::config::WheelMode {
fn from(mode: SmartShiftMode) -> Self {
match mode {
SmartShiftMode::Free => Self::Free,
SmartShiftMode::Ratchet => Self::Ratchet,
}
}
}
pub const AUTO_DISENGAGE_PERMANENT: u8 = 0xff;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct SmartShiftStatus {
pub mode: SmartShiftMode,
pub auto_disengage: u8,
pub tunable_torque: u8,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn flipped_is_an_involution() {
assert_eq!(SmartShiftMode::Free.flipped(), SmartShiftMode::Ratchet);
assert_eq!(SmartShiftMode::Ratchet.flipped(), SmartShiftMode::Free);
assert_eq!(
SmartShiftMode::Free.flipped().flipped(),
SmartShiftMode::Free
);
}
}