| 1 |
//! Various helpers and utilities. |
| 2 |
|
| 3 |
use anyhow::Error; |
| 4 |
use std::fmt::Write; |
| 5 |
use tracing::error; |
| 6 |
|
| 7 |
pub mod fs; |
| 8 |
mod html; |
| 9 |
mod toml_ext; |
| 10 |
|
| 11 |
pub(crate) use self::toml_ext::TomlExt; |
| 12 |
|
| 13 |
pub use self::html::{escape_html, escape_html_attribute}; |
| 14 |
|
| 15 |
/// Defines a `static` with a [`regex::Regex`]. |
| 16 |
#[macro_export] |
| 17 |
macro_rules! static_regex { |
| 18 |
($name:ident, $regex:literal) => { |
| 19 |
static $name: std::sync::LazyLock<regex::Regex> = |
| 20 |
std::sync::LazyLock::new(|| regex::Regex::new($regex).unwrap()); |
| 21 |
}; |
| 22 |
($name:ident, bytes, $regex:literal) => { |
| 23 |
static $name: std::sync::LazyLock<regex::bytes::Regex> = |
| 24 |
std::sync::LazyLock::new(|| regex::bytes::Regex::new($regex).unwrap()); |
| 25 |
}; |
| 26 |
} |
| 27 |
|
| 28 |
/// Prints a "backtrace" of some `Error`. |
| 29 |
pub fn log_backtrace(e: &Error) { |
| 30 |
let mut message = format!("{e}"); |
| 31 |
|
| 32 |
for cause in e.chain().skip(1) { |
| 33 |
write!(message, "\n\tCaused by: {cause}").unwrap(); |
| 34 |
} |
| 35 |
|
| 36 |
error!("{message}"); |
| 37 |
} |