use std::any::Any;
use std::fmt;
pub(crate) struct Panic(pub(crate) Option<String>);
impl Panic {
pub(crate) fn from_payload(payload: Option<Box<dyn Any + Send>>) -> Self {
let message = payload.and_then(|payload| match payload.downcast::<String>() {
Ok(message) => Some((*message).clone()),
Err(payload) => payload
.downcast::<&str>()
.ok()
.map(|message| (*message).to_owned()),
});
Self(message)
}
}
impl fmt::Display for Panic {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("the module panicked")?;
if let Some(message) = &self.0 {
write!(f, ": {message}")?;
}
Ok(())
}
}
impl fmt::Debug for Panic {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Panic")
}
}
impl std::error::Error for Panic {}