[go: up one dir, main page]

const_format 0.2.1

Concatenate/format contants into `&'static str` constants.
Documentation
use core::fmt::{self, Display};

/// An error while trying to write into a StrWriter.
#[non_exhaustive]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Error {
    /// Attempted to write something into the buffer when there isn't enough space to write it.
    NotEnoughSpace,
    /// For compatibility with [`NotAsciiError`](../wrapper_types/struct.NotAsciiError.html)
    NotAscii,
    /// Attempted to index a string arguent by an range where the one of the bounds
    /// was not on a char boundary.
    NotOnCharBoundary,
}

impl Display for Error {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        match self {
            Self::NotEnoughSpace => {
                fmt.write_str("The was not enough space to write the formatted output")
            }
            Self::NotAscii => fmt.write_str("Attempted to write non-ascii text"),
            Self::NotOnCharBoundary => {
                fmt.write_str("Attempted to index a byte that's not on a char boundary.")
            }
        }
    }
}

macro_rules! index_vars{
    ($self:ident, $index:ident; $($variant:ident),* $(,)? ) => (
        enum Index{
            $($variant,)*
        }

        let $index = match &$self {
            $(Error::$variant{..} => 3300 + Index::$variant as usize,)*
        };
    )
}

impl Error {
    /// For panicking at compile-time, with a compile-time error that says what the error is.
    #[track_caller]
    pub const fn unwrap<T>(&self) -> T {
        index_vars! {
            self,i;
            NotEnoughSpace,
            NotAscii,
            NotOnCharBoundary,
        };

        match self {
            Error::NotEnoughSpace => ["The was not enough space to write the formatted output"][i],
            Error::NotAscii => ["Attempted to write non-ascii text"][i],
            Error::NotOnCharBoundary => {
                ["Attempted to index a byte that's not on a char boundary."][i]
            }
        };
        loop {}
    }
}