use std::error::Error;
use std::fmt;
#[derive(Debug)]
pub struct StringError {
message: String,
source: Option<Box<dyn Error + 'static>>,
}
impl fmt::Display for StringError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(source) = self.source() {
write!(f, "{}: {}", self.message, source)
} else {
write!(f, "{}", self.message)
}
}
}
impl Error for StringError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
self.source.as_deref()
}
}
impl StringError {
pub(crate) fn new(message: impl Into<String>) -> Self {
Self {
message: message.into(),
source: None,
}
}
pub(crate) fn with_source(
message: impl Into<String>,
source: impl Into<Box<dyn Error + 'static>>,
) -> Self {
Self {
message: message.into(),
source: Some(source.into()),
}
}
}