use std::any::Any;
use std::error::Error;
use std::fmt;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AttemptPanic {
message: Box<str>,
}
impl AttemptPanic {
#[inline]
pub fn new(message: &str) -> Self {
Self::from_string(message.to_string())
}
#[inline]
pub(crate) fn from_string(message: String) -> Self {
Self {
message: message.into_boxed_str(),
}
}
pub(crate) fn from_payload(payload: Box<dyn Any + Send + 'static>) -> Self {
match payload.downcast::<String>() {
Ok(message) => Self::from_string(*message),
Err(payload) => match payload.downcast::<&'static str>() {
Ok(message) => Self::new(*message),
Err(_) => Self::new("attempt panicked with a non-string payload"),
},
}
}
#[inline]
pub fn message(&self) -> &str {
&self.message
}
}
impl fmt::Display for AttemptPanic {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.message)
}
}
impl Error for AttemptPanic {}