use std::fmt::{Display, Formatter};
use std::panic::PanicInfo;
pub fn extract_message(info: &PanicInfo) -> String {
let display = info.to_string();
let s = match display.strip_prefix("panicked at '") {
Some(s) => s,
None => return display,
};
let s = match s.rsplit_once('\'') {
Some((s, _)) => s,
None => return display,
};
s.to_string()
}
pub fn escape_text(text: &str) -> String {
text.replace('&', "&")
.replace('<', "<")
.replace('>', ">")
.replace('\'', "'")
.replace('"', """)
}
pub enum Unescaped {
Unsafe(String),
Safe(String),
}
impl Unescaped {
pub fn safe(s: impl Into<String>) -> Self {
Unescaped::Safe(s.into())
}
}
impl From<String> for Unescaped {
fn from(value: String) -> Self {
Self::Unsafe(value)
}
}
impl Display for Unescaped {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::Unsafe(s) => f.write_str(&escape_text(&s)),
Self::Safe(s) => f.write_str(s),
}
}
}