use crate::protocol::opcode::{
LOCK_DIR_AGAINST, LOCK_DIR_BOTH, LOCK_DIR_NEG, LOCK_DIR_POS, LOCK_DIR_WITH,
};
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub enum Direction {
#[default]
Both = LOCK_DIR_BOTH,
Positive = LOCK_DIR_POS,
Negative = LOCK_DIR_NEG,
With = LOCK_DIR_WITH,
Against = LOCK_DIR_AGAINST,
}
impl Direction {
pub const PRESS: Direction = Direction::Positive;
pub const RELEASE: Direction = Direction::Negative;
pub const IN: Direction = Direction::Positive;
pub const OUT: Direction = Direction::Negative;
pub fn as_u8(self) -> u8 {
self as u8
}
pub fn from_u8(v: u8) -> Option<Direction> {
Some(match v {
LOCK_DIR_BOTH => Direction::Both,
LOCK_DIR_POS => Direction::Positive,
LOCK_DIR_NEG => Direction::Negative,
LOCK_DIR_WITH => Direction::With,
LOCK_DIR_AGAINST => Direction::Against,
_ => return None,
})
}
pub fn is_relative(self) -> bool {
matches!(self, Direction::With | Direction::Against)
}
pub fn admits(self, other: Direction) -> bool {
self == Direction::Both || other == Direction::Both || self == other
}
pub fn of_delta(delta: i16) -> Direction {
match delta {
d if d > 0 => Direction::Positive,
d if d < 0 => Direction::Negative,
_ => Direction::Both,
}
}
}