qsv_docopt 1.10.0

Command line argument parsing.
Documentation
//! Utilities that needed a home.

/// Wrapper for lazily compiled regexes
pub struct RegexWrap(&'static str, ::std::sync::OnceLock<::regex::Regex>);

impl RegexWrap {
    /// Create a new const instances with the given regexp
    #[must_use]
    pub const fn new(re: &'static str) -> Self {
        Self(re, ::std::sync::OnceLock::<::regex::Regex>::new())
    }
}

impl ::std::ops::Deref for RegexWrap {
    type Target = ::regex::Regex;
    fn deref(&self) -> &Self::Target {
        self.1.get_or_init(|| ::regex::Regex::new(self.0).unwrap())
    }
}

/// Declares a `OnceLock` regex
macro_rules! decl_regex {
    ($($name:ident : $re:literal; )*) => {
        $(
            static $name: $crate::utils::RegexWrap = $crate::utils::RegexWrap::new($re);
        )*
    };
}

/// Print an error.
macro_rules! werr(
    ($($arg:tt)*) => ({
        use std::io::{Write, stderr};
        write!(&mut stderr(), $($arg)*).unwrap();
    })
);

/// return the value of a capture group or an empty string
#[inline]
#[must_use]
pub fn cap_or_empty<'t>(caps: &regex::Captures<'t>, name: &str) -> &'t str {
    caps.name(name).map_or("", |m| m.as_str())
}