catfx 0.1.0

Apply effects to a text stream
Documentation
//! Mod for error types raised by this library.
use std::error::Error;
use std::fmt::{self, Display};

/// Raised when a X per second is `<= 0`.
#[derive(Debug)]
pub struct UnsafeInterval<T> {
    unit: &'static str,
    interval: T,
}

impl<T> UnsafeInterval<T> {
    /// Create a new unsafe interval error.
    #[inline]
    pub const fn new(unit: &'static str, interval: T) -> Self {
        Self { unit, interval }
    }
}

impl<T> Display for UnsafeInterval<T>
where
    T: Display,
{
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "invalid {} per second: {}", self.unit, self.interval)
    }
}

impl<T> Error for UnsafeInterval<T> where T: fmt::Debug + Display {}

/// Error that gets raised if it could not be parsed to a string.
#[derive(Debug)]
pub enum ColorFromStrErr {
    /// We don't know what the user is trying to do.
    UnknownColor(String),
    /// The string started with `#` and is most likely a hex code, but it couldn't
    /// parse.
    InvalidHex(String),
}

impl ColorFromStrErr {
    /// Generates an `InvalidHex` error.
    pub fn invalid_hex<S>(hex: S) -> Self
    where
        S: Into<String>,
    {
        Self::InvalidHex(hex.into())
    }
}

impl Display for ColorFromStrErr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::UnknownColor(s) => write!(f, "unknown color: {s}"),
            Self::InvalidHex(s) => write!(f, "invalid hex code: {s}"),
        }
    }
}

impl Error for ColorFromStrErr {}