use alloc::string::String;
use core::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum DynamicError {
BadParameter(String),
PreconditionNotMet(String),
IllegalOperation(String),
Unsupported(String),
Inconsistent(String),
BuilderConflict(String),
LoanError(String),
}
impl DynamicError {
pub(crate) fn bad_parameter(msg: impl Into<String>) -> Self {
Self::BadParameter(msg.into())
}
pub(crate) fn inconsistent(msg: impl Into<String>) -> Self {
Self::Inconsistent(msg.into())
}
pub(crate) fn builder(msg: impl Into<String>) -> Self {
Self::BuilderConflict(msg.into())
}
pub(crate) fn unsupported(msg: impl Into<String>) -> Self {
Self::Unsupported(msg.into())
}
pub(crate) fn loan(msg: impl Into<String>) -> Self {
Self::LoanError(msg.into())
}
}
impl fmt::Display for DynamicError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::BadParameter(m) => write!(f, "bad parameter: {m}"),
Self::PreconditionNotMet(m) => write!(f, "precondition not met: {m}"),
Self::IllegalOperation(m) => write!(f, "illegal operation: {m}"),
Self::Unsupported(m) => write!(f, "unsupported: {m}"),
Self::Inconsistent(m) => write!(f, "inconsistent: {m}"),
Self::BuilderConflict(m) => write!(f, "builder conflict: {m}"),
Self::LoanError(m) => write!(f, "loan error: {m}"),
}
}
}
#[cfg(feature = "std")]
extern crate std;
#[cfg(feature = "std")]
impl std::error::Error for DynamicError {}
#[cfg(test)]
mod tests {
use super::*;
use alloc::string::ToString;
#[test]
fn dynamic_error_display_emits_helpful_text() {
let e = DynamicError::bad_parameter("x");
assert!(e.to_string().contains("bad parameter"));
assert!(e.to_string().contains('x'));
}
}