use core::fmt;
pub type Result<T, E = Error> = core::result::Result<T, E>;
pub trait ResultExt {
fn ignore_exhausted(self) -> Result<()>;
}
impl<T> ResultExt for Result<T> {
#[inline]
fn ignore_exhausted(self) -> Result<()> {
match self {
Ok(_) => Ok(()),
Err(err) => {
if err.is_exhausted() {
Ok(())
} else {
Err(err)
}
}
}
}
}
enum ErrorInner {
#[cfg(feature = "alloc")]
Kind(alloc::boxed::Box<ErrorKind>),
#[cfg(not(feature = "alloc"))]
Kind(ErrorKind),
EarlyExit,
}
pub struct Error {
inner: ErrorInner,
}
impl From<ErrorKind> for Error {
#[inline]
fn from(kind: ErrorKind) -> Self {
#[cfg(feature = "alloc")]
let kind = alloc::boxed::Box::new(kind);
Self {
inner: ErrorInner::Kind(kind),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.inner {
ErrorInner::Kind(kind) => {
#[cfg(feature = "alloc")]
let kind = &**kind;
match kind {
ErrorKind::Exhausted => {
write!(f, "the mutator is exhausted")
}
ErrorKind::InvalidRange => {
write!(f, "the mutator was given an invalid range")
}
ErrorKind::Other(msg) => {
write!(f, "an unknown error occurred: {msg}")
}
}
}
ErrorInner::EarlyExit => {
write!(f, "internal error variant: early exit from mutation loop")
}
}
}
}
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {}
impl Error {
#[inline]
pub(crate) fn early_exit() -> Self {
Self {
inner: ErrorInner::EarlyExit,
}
}
#[inline]
pub(crate) fn is_early_exit(&self) -> bool {
matches!(self.inner, ErrorInner::EarlyExit)
}
#[must_use]
pub fn exhausted() -> Self {
ErrorKind::Exhausted.into()
}
#[must_use]
pub fn invalid_range() -> Self {
ErrorKind::InvalidRange.into()
}
#[must_use]
pub fn other(msg: impl Into<ErrorMessage>) -> Self {
ErrorKind::Other(msg.into()).into()
}
#[must_use]
pub fn kind(&self) -> &ErrorKind {
match &self.inner {
ErrorInner::Kind(kind) => kind,
ErrorInner::EarlyExit => unreachable!(),
}
}
#[must_use]
pub fn is_exhausted(&self) -> bool {
matches!(self.kind(), ErrorKind::Exhausted)
}
#[must_use]
pub fn is_invalid_range(&self) -> bool {
matches!(self.kind(), ErrorKind::InvalidRange)
}
#[must_use]
pub fn is_other(&self) -> bool {
matches!(self.kind(), ErrorKind::Other(_))
}
}
#[non_exhaustive]
#[derive(Debug)]
pub enum ErrorKind {
Exhausted,
InvalidRange,
Other(ErrorMessage),
}
impl From<Error> for ErrorKind {
#[inline]
fn from(err: Error) -> Self {
match err.inner {
#[cfg(feature = "alloc")]
ErrorInner::Kind(kind) => *kind,
#[cfg(not(feature = "alloc"))]
ErrorInner::Kind(kind) => kind,
ErrorInner::EarlyExit => unreachable!(),
}
}
}
#[derive(Debug)]
pub struct ErrorMessage {
#[cfg(feature = "alloc")]
inner: alloc::borrow::Cow<'static, str>,
#[cfg(not(feature = "alloc"))]
inner: &'static str,
}
impl ErrorMessage {
#[must_use]
pub fn new(msg: impl Into<ErrorMessage>) -> Self {
msg.into()
}
#[must_use]
pub fn as_str(&self) -> &str {
return &self.inner;
}
}
impl fmt::Display for ErrorMessage {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl From<&'static str> for ErrorMessage {
#[inline]
fn from(s: &'static str) -> Self {
let inner = s;
#[cfg(feature = "alloc")]
let inner = alloc::borrow::Cow::Borrowed(inner);
Self { inner }
}
}
#[cfg(feature = "alloc")]
impl From<alloc::borrow::Cow<'static, str>> for ErrorMessage {
#[inline]
fn from(s: alloc::borrow::Cow<'static, str>) -> Self {
Self { inner: s }
}
}
#[cfg(feature = "alloc")]
impl From<alloc::string::String> for ErrorMessage {
#[inline]
fn from(s: alloc::string::String) -> Self {
let inner = s.into();
Self { inner }
}
}