// file: kb_lib/src/db/types/swap_trade_side.rs

//! Swap trade side.

/// Swap side relative to the normalized base token of the pair.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum SwapTradeSide {
    /// Unknown or not yet derived.
    Unknown,
    /// Buy of the base token against the quote token.
    BuyBase,
    /// Sell of the base token against the quote token.
    SellBase,
}

impl SwapTradeSide {
    /// Converts the trade side to its stable integer representation.
    pub fn to_i16(self) -> i16 {
        match self {
            Self::Unknown => return 0,
            Self::BuyBase => return 1,
            Self::SellBase => return 2,
        }
    }

    /// Restores a trade side from its stable integer representation.
    pub fn from_i16(value: i16) -> Result<Self, crate::Error> {
        match value {
            0 => return Ok(Self::Unknown),
            1 => return Ok(Self::BuyBase),
            2 => return Ok(Self::SellBase),
            _ => {
                return Err(crate::Error::Db(format!("invalid SwapTradeSide value: {}", value)));
            },
        }
    }
}
