use serde::{Deserialize, Serialize};
crate::api_id_enum! {
#[non_exhaustive]
pub RobotMode {
Unknown = -1,
Damping = 0,
Prepare = 1,
Walking = 2,
Custom = 3,
Soccer = 4,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[repr(usize)]
pub enum Hand {
Left = 0,
Right = 1,
}
impl From<Hand> for usize {
fn from(hand: Hand) -> Self {
hand as usize
}
}
impl From<Hand> for i32 {
fn from(hand: Hand) -> Self {
match hand {
Hand::Left => 0,
Hand::Right => 1,
}
}
}
impl From<Hand> for u8 {
fn from(hand: Hand) -> Self {
match hand {
Hand::Left => 0,
Hand::Right => 1,
}
}
}
impl TryFrom<usize> for Hand {
type Error = ();
fn try_from(value: usize) -> Result<Self, Self::Error> {
match value {
0 => Ok(Hand::Left),
1 => Ok(Hand::Right),
_ => Err(()),
}
}
}
impl TryFrom<i32> for Hand {
type Error = ();
fn try_from(value: i32) -> Result<Self, Self::Error> {
match value {
0 => Ok(Hand::Left),
1 => Ok(Hand::Right),
_ => Err(()),
}
}
}
crate::api_id_enum! {
GripperMode {
Position = 0,
Force = 1,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_robot_mode_conversion() {
assert_eq!(RobotMode::try_from(-1), Ok(RobotMode::Unknown));
assert_eq!(RobotMode::try_from(0), Ok(RobotMode::Damping));
assert_eq!(RobotMode::try_from(2), Ok(RobotMode::Walking));
assert_eq!(RobotMode::try_from(99), Err("invalid value"));
assert_eq!(i32::from(RobotMode::Walking), 2);
}
#[test]
fn test_gripper_mode_conversion() {
assert_eq!(GripperMode::try_from(0), Ok(GripperMode::Position));
assert_eq!(GripperMode::try_from(1), Ok(GripperMode::Force));
assert_eq!(GripperMode::try_from(2), Err("invalid value"));
}
}