safe_regex_macro/
lib.rs

1//! [![crates.io version](https://img.shields.io/crates/v/safe-regex-macro.svg)](https://crates.io/crates/safe-regex-macro)
2//! [![license: Apache 2.0](https://gitlab.com/leonhard-llc/safe-regex-rs/-/raw/main/license-apache-2.0.svg)](http://www.apache.org/licenses/LICENSE-2.0)
3//! [![unsafe forbidden](https://gitlab.com/leonhard-llc/safe-regex-rs/-/raw/main/unsafe-forbidden-success.svg)](https://github.com/rust-secure-code/safety-dance/)
4//! [![pipeline status](https://gitlab.com/leonhard-llc/safe-regex-rs/badges/main/pipeline.svg)](https://gitlab.com/leonhard-llc/safe-regex-rs/-/pipelines)
5//!
6//! This crate provides the `regex!` macro used by the
7//! [`safe-regex`](https://crates.io/crates/safe-regex) crate.
8//!
9//! It is a thin wrapper around the
10//! [`safe-regex-compiler`](https://crates.io/crates/safe-regex-compiler)
11//! crate.
12//!
13//! # Cargo Geiger Safety Report
14//! # Changelog
15//! See [`safe_regex`](https://crates.io/crates/safe-regex) create.
16//!
17//! # Release Process
18//! 1. Edit `Cargo.toml` and bump version number.
19//! 1. Run `../release.sh`
20#![forbid(unsafe_code)]
21
22/// Compiles a regular expression into a Rust type.
23///
24/// Returns a `MatcherN` struct where `N` is the number of capturing groups.
25///
26/// Specify the type of the expected matcher so your editor can
27/// show its functions and documentation:
28/// `let matcher: Matcher0<_> = regex!(br".")`.
29///
30/// # Examples
31/// ```rust
32/// use safe_regex::{regex, Matcher0};
33/// let matcher: Matcher0<_> =
34///     regex!(br"[ab][0-9]*");
35/// assert!(matcher.is_match(b"a42"));
36/// assert!(!matcher.is_match(b"X"));
37/// ```
38///
39/// ```rust
40/// use safe_regex::{regex, Matcher3};
41/// let matcher: Matcher3<_> =
42///     regex!(br"([ab])([0-9]*)(suffix)?");
43/// let (prefix, digits, suffix) =
44///     matcher.match_slices(b"a42").unwrap();
45/// assert_eq!(b"a", prefix);
46/// assert_eq!(b"42", digits);
47/// assert_eq!(b"", suffix);
48/// let (prefix_range, digits_r, suffix_r)
49///     = matcher.match_ranges(b"a42").unwrap();
50/// assert_eq!(0..1_usize, prefix_range);
51/// assert_eq!(1..3_usize, digits_r);
52/// assert_eq!(0..0_usize, suffix_r);
53/// ```
54#[proc_macro]
55#[allow(clippy::missing_panics_doc)]
56pub fn regex(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
57    let input2 = safe_proc_macro2::TokenStream::from(input);
58    let output2 = match safe_regex_compiler::impl_regex(input2) {
59        Ok(output2) => output2,
60        Err(reason) => panic!("{}", reason),
61    };
62    proc_macro::TokenStream::from(output2)
63}