use core::{result, str::Utf8Error};
pub type TextosResult<N> = result::Result<N, TextosError>;
#[non_exhaustive]
#[derive(Debug)]
pub enum TextosError {
OutOfBounds,
NotEnoughCapacity(usize),
NotEnoughElements(usize),
Utf8(Utf8Error),
#[cfg(feature = "std")]
Error(String),
}
impl TextosError {
pub const fn is_utf8(&self) -> bool {
matches![self, TextosError::Utf8(_)]
}
}
mod core_impls {
use super::TextosError;
use core::{fmt, str::Utf8Error};
impl From<Utf8Error> for TextosError {
fn from(e: Utf8Error) -> Self {
TextosError::Utf8(e)
}
}
impl fmt::Display for TextosError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use TextosError::*;
match self {
OutOfBounds => write!(f, "The value is out of bounds."),
NotEnoughCapacity(c) => write!(f, "Not enough capacity. Needed: {c}"),
NotEnoughElements(e) => write!(f, "Not enough elements. Needed: {e}"),
Utf8(e) => fmt::Debug::fmt(e, f),
#[cfg(feature = "std")]
Error(s) => write!(f, "Error: {s}"),
}
}
}
}
#[cfg(feature = "std")]
mod std_impls {
use super::TextosError;
use std::error::Error as StdError;
impl StdError for TextosError {}
}