use crate::context::call_reply::{ErrorCallReply, ErrorReply};
pub use crate::raw;
use std::ffi::CStr;
use std::fmt;
#[derive(Debug)]
pub enum ValkeyError {
    WrongArity,
    Str(&'static str),
    String(String),
    WrongType,
}
impl<'root> From<ErrorCallReply<'root>> for ValkeyError {
    fn from(err: ErrorCallReply<'root>) -> Self {
        ValkeyError::String(
            err.to_utf8_string()
                .unwrap_or("can not convert error into String".into()),
        )
    }
}
impl<'root> From<ErrorReply<'root>> for ValkeyError {
    fn from(err: ErrorReply<'root>) -> Self {
        ValkeyError::String(
            err.to_utf8_string()
                .unwrap_or("can not convert error into String".into()),
        )
    }
}
impl ValkeyError {
    #[must_use]
    pub const fn nonexistent_key() -> Self {
        Self::Str("ERR could not perform this operation on a key that doesn't exist")
    }
    #[must_use]
    pub const fn short_read() -> Self {
        Self::Str("ERR short read or OOM loading DB")
    }
}
impl<T: std::error::Error> From<T> for ValkeyError {
    fn from(e: T) -> Self {
        Self::String(format!("ERR {e}"))
    }
}
impl fmt::Display for ValkeyError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let d = match self {
            Self::WrongArity => "Wrong Arity",
            Self::WrongType => std::str::from_utf8(
                CStr::from_bytes_with_nul(raw::REDISMODULE_ERRORMSG_WRONGTYPE)
                    .unwrap()
                    .to_bytes(),
            )
            .unwrap(),
            Self::Str(s) => s,
            Self::String(s) => s.as_str(),
        };
        write!(f, "{d}")
    }
}