use std::fmt;
#[derive(Debug)]
#[non_exhaustive]
pub enum DocumentError {
#[non_exhaustive]
TypeMismatch {
message: String,
},
#[non_exhaustive]
NumericCoercionOverflow {
target: String,
value: String,
},
#[non_exhaustive]
InvalidInput {
message: String,
},
#[non_exhaustive]
Custom {
message: String,
},
#[non_exhaustive]
UnsupportedOperation {
message: String,
},
}
impl fmt::Display for DocumentError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DocumentError::TypeMismatch { message } => write!(f, "type mismatch: {message}"),
DocumentError::NumericCoercionOverflow { target, value } => {
write!(f, "numeric value {value} out of range for {target}")
}
DocumentError::InvalidInput { message } => write!(f, "invalid input: {message}"),
DocumentError::Custom { message } => f.write_str(message),
DocumentError::UnsupportedOperation { message } => {
write!(f, "unsupported operation: {message}")
}
}
}
}
impl std::error::Error for DocumentError {}
impl DocumentError {
pub fn type_mismatch(message: impl Into<String>) -> Self {
DocumentError::TypeMismatch {
message: message.into(),
}
}
pub fn numeric_coercion_overflow(target: impl Into<String>, value: impl Into<String>) -> Self {
DocumentError::NumericCoercionOverflow {
target: target.into(),
value: value.into(),
}
}
pub fn invalid_input(message: impl Into<String>) -> Self {
DocumentError::InvalidInput {
message: message.into(),
}
}
pub fn custom(message: impl Into<String>) -> Self {
DocumentError::Custom {
message: message.into(),
}
}
pub fn unsupported(message: impl Into<String>) -> Self {
DocumentError::UnsupportedOperation {
message: message.into(),
}
}
}