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

//! Pool token role.

/// Role of one token inside a normalized pool.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum PoolTokenRole {
    /// Base token.
    Base,
    /// Quote token.
    Quote,
    /// LP mint token.
    LpMint,
    /// Reserve token or reserve-side tracked token.
    Reserve,
    /// Other role.
    Other,
}

impl PoolTokenRole {
    /// Converts the role to its stable integer representation.
    pub fn to_i16(self) -> i16 {
        match self {
            Self::Base => return 0,
            Self::Quote => return 1,
            Self::LpMint => return 2,
            Self::Reserve => return 3,
            Self::Other => return 4,
        }
    }

    /// Restores a role from its stable integer representation.
    pub fn from_i16(value: i16) -> Result<Self, crate::Error> {
        match value {
            0 => return Ok(Self::Base),
            1 => return Ok(Self::Quote),
            2 => return Ok(Self::LpMint),
            3 => return Ok(Self::Reserve),
            4 => return Ok(Self::Other),
            _ => {
                return Err(crate::Error::Db(format!("invalid PoolTokenRole value: {}", value)));
            },
        }
    }
}
