Skip to main content

type_regex

Macro type_regex 

Source
macro_rules! type_regex {
    ($vis: vis $name: ident = $regex: expr $(=> $doc: expr)?) => { ... };
}
Expand description

Lifts strings to type-level regular expressions.

use refining_regex::type_regex;

type_regex!(pub Natural = "^0|[1-9][0-9]*$" => "Matches non-negative integers.");

Is essentially equivalent to:

use std::{marker::PhantomData, sync::LazyLock};

use refining_regex::types::{Regex, StaticRegex, TypeRegex, INVALID};

/// Matches non-negative integers.
pub struct Natural {
    private: PhantomData<()>,
}

impl TypeRegex for Natural {
    fn get() -> StaticRegex {
        static REGEX: LazyLock<Regex> = LazyLock::new(|| {
            Regex::new("^0|[1-9][0-9]*$").expect(INVALID)
        });

        LazyLock::force(&REGEX)
    }
}