pzzld-core 0.0.0

The pzzld platform facilitates unique experiences enabled by generative technologies and WebAssembly (WASM)
Documentation
/*
    Appellation: error <module>
    Contrib: FL03 <jo3mccain@icloud.com>
*/

#[derive(
    Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, strum::EnumDiscriminants, strum::EnumIs,
)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Deserialize, serde::Serialize),
    serde(rename_all = "kebab-case"),
    strum_discriminants(
        derive(serde::Deserialize, serde::Serialize),
        serde(rename_all = "kebab-case"),
    )
)]
#[strum(serialize_all = "kebab-case")]
#[strum_discriminants(
    name(ErrorKind),
    derive(
        Hash,
        Ord,
        PartialOrd,
        strum::AsRefStr,
        strum::Display,
        strum::EnumCount,
        strum::EnumIs,
        strum::EnumIter,
        strum::EnumString,
        strum::VariantArray,
        strum::VariantNames
    )
)]
pub enum Error {
    /// Configuration error.
    IOError(String),
    Unknown(String),
}

impl Error {
    pub fn new(kind: ErrorKind, message: impl ToString) -> Self {
        let message = message.to_string();
        match kind {
            ErrorKind::IOError => Self::IOError(message),
            ErrorKind::Unknown => Self::Unknown(message),
        }
    }
    /// Returns the kind of discriminated error.
    pub fn kind(&self) -> ErrorKind {
        match self {
            Self::IOError(_) => ErrorKind::IOError,
            Self::Unknown(_) => ErrorKind::Unknown,
        }
    }
    /// Returns the message of the error.
    pub fn message(&self) -> &str {
        match self {
            Self::IOError(e) => e,
            Self::Unknown(e) => e,
        }
    }
}

impl core::fmt::Display for Error {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        write!(
            f,
            "[{tag}] {content}",
            tag = self.kind(),
            content = self.message()
        )
    }
}

impl core::error::Error for Error {}

unsafe impl Send for Error {}

unsafe impl Sync for Error {}

impl From<&str> for Error {
    fn from(e: &str) -> Self {
        Self::Unknown(e.to_string())
    }
}

impl From<String> for Error {
    fn from(e: String) -> Self {
        Self::Unknown(e)
    }
}