browser_panic_hook/mode/
custom_body.rs

1use crate::mode::{PanicDetails, PresentationMode};
2use std::borrow::Cow;
3
4/// Replace the body with a string generated by a custom function.
5///
6/// **NOTE:** The function needs to ensure that the [`PanicDetails`] information is escaped. This
7/// can be done using [`crate::utils::escape_text`].
8pub struct CustomBody(pub Box<dyn Fn(PanicDetails) -> String + 'static + Send + Sync>);
9
10impl<F> From<F> for CustomBody
11where
12    F: for<'a> Fn(PanicDetails<'a>) -> String + 'static + Send + Sync,
13{
14    fn from(value: F) -> Self {
15        Self(Box::new(value))
16    }
17}
18
19impl PresentationMode for CustomBody {
20    fn present(&self, details: PanicDetails) -> Result<(), Cow<'static, str>> {
21        super::set_body(None, self.0(details))
22    }
23}