wedb_embed 0.1.1

Embedded database engine providing Redis-like APIs, built on fjall / 嵌入式数据库引擎,提供类似 Redis 的接口,底层基于 fjall 开发
Documentation
use std::error::Error as StdError;
use std::io;
use std::result::Result as StdResult;
use thiserror::Error;

pub type Result<T, E = Error> = StdResult<T, E>;

#[derive(Error, Debug)]
pub enum Error {
    #[error("Storage error: {0}")]
    Storage(String),

    #[error("Config error: {0}")]
    Config(String),

    #[error("Internal error: {0}")]
    Internal(String),

    #[error("IO error: {0}")]
    Io(#[from] io::Error),

    #[error("Fjall error: {0}")]
    Fjall(#[from] fjall::Error),

    #[error("Serialization error: {0}")]
    Serialization(String),

    #[error("Redis error: {0}")]
    Redis(String),

    #[error("Invalid data: {0}")]
    InvalidData(String),

    #[error("Not found: {0}")]
    NotFound(String),
}

impl Error {
    pub fn io(e: io::Error) -> Self {
        Self::Io(e)
    }

    pub fn conf(msg: impl Into<String>) -> Self {
        Self::Config(msg.into())
    }

    pub fn internal(msg: impl Into<String>) -> Self {
        Self::Internal(msg.into())
    }

    pub fn internal_with_source<E: StdError + Send + Sync + 'static>(
        msg: impl Into<String>,
        source: E,
    ) -> Self {
        let msg_str: String = msg.into();
        Self::Internal(format!("{msg_str}: {source}"))
    }

    pub fn invalid_data(msg: impl Into<String>) -> Self {
        Self::InvalidData(msg.into())
    }

    pub fn not_found(msg: impl Into<String>) -> Self {
        Self::NotFound(msg.into())
    }

    pub fn redis(msg: impl Into<String>) -> Self {
        Self::Redis(msg.into())
    }

    pub fn storage(msg: impl Into<String>) -> Self {
        Self::Storage(msg.into())
    }

    pub fn wrong_type(msg: impl Into<String>) -> Self {
        Self::Redis(msg.into())
    }

    /// 便捷构造 WRONGTYPE 错误(使用统一常量)
    #[inline]
    pub fn wrong_type_default() -> Self {
        Self::Redis(ERR_WRONG_TYPE.to_string())
    }

    #[inline]
    pub fn is_wrong_type(&self) -> bool {
        match self {
            Self::Redis(msg) => msg.starts_with("WRONGTYPE"),
            _ => false,
        }
    }
}

/// 跨类型操作错误(统一定义,各模块共用)
pub const ERR_WRONG_TYPE: &str =
    "WRONGTYPE Operation against a key holding the wrong kind of value";