#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Side {
Buy,
Sell,
}
impl std::fmt::Display for Side {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str(match self {
Self::Buy => "BUY",
Self::Sell => "SELL",
})
}
}
impl Side {
#[inline]
pub fn is_buy(self) -> bool {
match self {
Side::Buy => true,
Side::Sell => false,
}
}
#[inline]
pub fn is_sell(self) -> bool {
match self {
Side::Buy => false,
Side::Sell => true,
}
}
#[inline]
pub fn opposite(self) -> Self {
match self {
Side::Buy => Side::Sell,
Side::Sell => Side::Buy,
}
}
#[inline]
pub fn sign(self) -> i8 {
match self {
Side::Buy => 1,
Side::Sell => -1,
}
}
}
#[cfg(test)]
mod tests {
use super::Side;
#[test]
fn side_predicates_work() {
assert!(Side::Buy.is_buy());
assert!(!Side::Buy.is_sell());
assert!(Side::Sell.is_sell());
assert!(!Side::Sell.is_buy());
}
#[test]
fn opposite_returns_other_side() {
assert_eq!(Side::Buy.opposite(), Side::Sell);
assert_eq!(Side::Sell.opposite(), Side::Buy);
}
#[test]
fn sign_matches_direction() {
assert_eq!(Side::Buy.sign(), 1);
assert_eq!(Side::Sell.sign(), -1);
}
#[test]
fn display_uses_api_names() {
assert_eq!(Side::Buy.to_string(), "BUY");
assert_eq!(Side::Sell.to_string(), "SELL");
}
}