use std::fmt::{Debug, Display, Formatter};
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
NotYetImplemented(String),
External(String, Box<dyn std::error::Error + Send + Sync>),
Io(std::io::Error),
InvalidArgumentError(String),
ExternalFormat(String),
Overflow,
OutOfSpec(String),
}
impl Error {
pub fn from_external_error(error: impl std::error::Error + Send + Sync + 'static) -> Self {
Self::External("".to_string(), Box::new(error))
}
pub(crate) fn oos<A: Into<String>>(msg: A) -> Self {
Self::OutOfSpec(msg.into())
}
#[allow(dead_code)]
pub(crate) fn nyi<A: Into<String>>(msg: A) -> Self {
Self::NotYetImplemented(msg.into())
}
}
impl From<::std::io::Error> for Error {
fn from(error: std::io::Error) -> Self {
Error::Io(error)
}
}
impl From<std::str::Utf8Error> for Error {
fn from(error: std::str::Utf8Error) -> Self {
Error::External("".to_string(), Box::new(error))
}
}
impl From<std::string::FromUtf8Error> for Error {
fn from(error: std::string::FromUtf8Error) -> Self {
Error::External("".to_string(), Box::new(error))
}
}
impl From<simdutf8::basic::Utf8Error> for Error {
fn from(error: simdutf8::basic::Utf8Error) -> Self {
Error::External("".to_string(), Box::new(error))
}
}
impl From<std::collections::TryReserveError> for Error {
fn from(_: std::collections::TryReserveError) -> Error {
Error::Overflow
}
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Error::NotYetImplemented(source) => {
write!(f, "Not yet implemented: {}", &source)
}
Error::External(message, source) => {
write!(f, "External error{}: {}", message, &source)
}
Error::Io(desc) => write!(f, "Io error: {desc}"),
Error::InvalidArgumentError(desc) => {
write!(f, "Invalid argument error: {desc}")
}
Error::ExternalFormat(desc) => {
write!(f, "External format error: {desc}")
}
Error::Overflow => {
write!(f, "Operation overflew the backing container.")
}
Error::OutOfSpec(message) => {
write!(f, "{message}")
}
}
}
}
impl std::error::Error for Error {}
pub type Result<T> = std::result::Result<T, Error>;