// file: kb_lib/src/error.rs

//! Shared error type for `kb_lib`.

/// Global error type used by the `kb_lib` crate.
///
/// The project intentionally avoids `anyhow` and `thiserror`, so this
/// enum centralizes the main error families with explicit textual messages.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Error {
    /// Configuration error.
    Config(std::string::String),
    /// Filesystem or standard I/O error.
    Io(std::string::String),
    /// JSON serialization or deserialization error.
    Json(std::string::String),
    /// Tracing initialization or logging error.
    Tracing(std::string::String),
    /// HTTP transport error.
    Http(std::string::String),
    /// WebSocket transport error.
    Ws(std::string::String),
    /// Database error.
    Db(std::string::String),
    /// Invalid internal state error.
    InvalidState(std::string::String),
    /// Operation requested while the client is not connected.
    NotConnected(std::string::String),
    /// Placeholder for a feature intentionally scheduled for a later version.
    NotImplemented(std::string::String),
}

impl std::fmt::Display for Error {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::Config(message) => return write!(formatter, "configuration error: {message}"),
            Error::Io(message) => return write!(formatter, "io error: {message}"),
            Error::Json(message) => return write!(formatter, "json error: {message}"),
            Error::Tracing(message) => return write!(formatter, "tracing error: {message}"),
            Error::Http(message) => return write!(formatter, "http error: {message}"),
            Error::Ws(message) => return write!(formatter, "websocket error: {message}"),
            Error::Db(message) => return write!(formatter, "db error: {}", message),
            Error::InvalidState(message) => return write!(formatter, "invalid state: {message}"),
            Error::NotConnected(message) => return write!(formatter, "not connected: {message}"),
            Error::NotImplemented(message) => {
                return write!(formatter, "not implemented: {message}");
            },
        }
    }
}

impl std::error::Error for Error {}
