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

//! Observation source kind.

/// Source family for one on-chain observation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum ObservationSourceKind {
    /// HTTP RPC source.
    HttpRpc,
    /// WebSocket RPC source.
    WsRpc,
    /// Yellowstone gRPC source.
    Grpc,
    /// DEX connector source.
    Dex,
    /// Other source kind.
    Other,
}

impl ObservationSourceKind {
    /// Converts the source kind to its stable integer representation.
    pub fn to_i16(self) -> i16 {
        match self {
            Self::HttpRpc => return 0,
            Self::WsRpc => return 1,
            Self::Grpc => return 2,
            Self::Dex => return 3,
            Self::Other => return 4,
        }
    }

    /// Restores a source kind from its stable integer representation.
    pub fn from_i16(value: i16) -> Result<Self, crate::Error> {
        match value {
            0 => return Ok(Self::HttpRpc),
            1 => return Ok(Self::WsRpc),
            2 => return Ok(Self::Grpc),
            3 => return Ok(Self::Dex),
            4 => return Ok(Self::Other),
            _ => {
                return Err(crate::Error::Db(format!(
                    "invalid ObservationSourceKind value: {}",
                    value
                )));
            },
        }
    }
}
