mdbook_core/utils/
mod.rs

1//! Various helpers and utilities.
2
3use anyhow::Error;
4use std::fmt::Write;
5use tracing::error;
6
7pub mod fs;
8mod html;
9mod toml_ext;
10
11pub(crate) use self::toml_ext::TomlExt;
12
13pub use self::html::{escape_html, escape_html_attribute};
14
15/// Defines a `static` with a [`regex::Regex`].
16#[macro_export]
17macro_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`.
29pub 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}