Skip to main content

ct_regex_internal/expr/
anon.rs

1use std::ops::Range;
2
3use super::Regex;
4use crate::expr::{FindAllCaptures, RangeOfAllMatches, SliceAllMatches};
5use crate::haystack::{HaystackItem, HaystackIter, HaystackOf, IntoHaystack, OwnedHaystackable};
6
7/// A trait that is automatically implemented for 'anonymous' regular expression types. There is
8/// only one difference between this and [`Regex`]: all functions take self as the first parameter,
9/// removing the need to name the expression itself.
10///
11/// An `AnonRegex` can be created by invoking `regex!()` without a type identifier or visibility.
12/// The result is an instance of an unnamable type implementing `AnonRegex`.
13pub trait AnonRegex<I: HaystackItem, const N: usize>: Regex<I, N> {
14    /// See [`Regex::is_match`].
15    fn is_match<'a, H: HaystackOf<'a, I>>(&self, hay: impl IntoHaystack<'a, H>) -> bool {
16        <Self as Regex<I, N>>::is_match(hay)
17    }
18
19    /// See [`Regex::contains_match`].
20    fn contains_match<'a, H: HaystackOf<'a, I>>(&self, hay: impl IntoHaystack<'a, H>) -> bool {
21        <Self as Regex<I, N>>::contains_match(hay)
22    }
23
24    /// See [`Regex::count_matches`].
25    fn count_matches<'a, H: HaystackOf<'a, I>>(
26        &self,
27        hay: impl IntoHaystack<'a, H>,
28        overlapping: bool,
29    ) -> usize {
30        <Self as Regex<I, N>>::count_matches(hay, overlapping)
31    }
32
33    /// See [`Regex::range_of_match`].
34    fn range_of_match<'a, H: HaystackOf<'a, I>>(
35        &self,
36        hay: impl IntoHaystack<'a, H>,
37    ) -> Option<Range<usize>> {
38        <Self as Regex<I, N>>::range_of_match(hay)
39    }
40
41    /// See [`Regex::range_of_all_matches`].
42    fn range_of_all_matches<'a, H: HaystackOf<'a, I>>(
43        &self,
44        hay: impl IntoHaystack<'a, H>,
45        overlapping: bool,
46    ) -> RangeOfAllMatches<'a, I, H, Self::Pattern> {
47        <Self as Regex<I, N>>::range_of_all_matches(hay, overlapping)
48    }
49
50    /// See [`Regex::slice_match`].
51    fn slice_match<'a, H: HaystackOf<'a, I>>(
52        &self,
53        hay: impl IntoHaystack<'a, H>,
54    ) -> Option<H::Slice> {
55        <Self as Regex<I, N>>::slice_match(hay)
56    }
57
58    /// See [`Regex::slice_all_matches`].
59    fn slice_all_matches<'a, H: HaystackOf<'a, I>>(
60        &self,
61        hay: impl IntoHaystack<'a, H>,
62        overlapping: bool,
63    ) -> SliceAllMatches<'a, I, H, Self::Pattern> {
64        <Self as Regex<I, N>>::slice_all_matches(hay, overlapping)
65    }
66
67    /// See [`Regex::do_capture`].
68    fn do_capture<'a, H: HaystackOf<'a, I>>(
69        &self,
70        hay: impl IntoHaystack<'a, H>,
71    ) -> Option<Self::Capture<'a, H::Slice>> {
72        <Self as Regex<I, N>>::do_capture(hay)
73    }
74
75    /// See [`Regex::find_capture`].
76    fn find_capture<'a, H: HaystackOf<'a, I>>(
77        &self,
78        hay: impl IntoHaystack<'a, H>,
79    ) -> Option<Self::Capture<'a, H::Slice>> {
80        <Self as Regex<I, N>>::find_capture(hay)
81    }
82
83    /// See [`Regex::find_all_captures`].
84    fn find_all_captures<'a, H: HaystackOf<'a, I>>(
85        &self,
86        hay: impl IntoHaystack<'a, H>,
87        overlapping: bool,
88    ) -> FindAllCaptures<'a, Self, I, H, N> {
89        <Self as Regex<I, N>>::find_all_captures(hay, overlapping)
90    }
91
92    /// See [`Regex::replace`].
93    fn replace<'a, M: OwnedHaystackable<I>>(
94        &self,
95        hay_mut: &mut M,
96        with: <M::Hay<'a> as HaystackIter<'a>>::Slice
97    ) -> bool {
98        <Self as Regex<I, N>>::replace(hay_mut, with)
99    }
100
101    /// See [`Regex::replace_all`].
102    fn replace_all<'a, M: OwnedHaystackable<I>>(
103        &self,
104        hay_mut: &mut M,
105        with: <M::Hay<'a> as HaystackIter<'a>>::Slice
106    ) -> usize {
107        <Self as Regex<I, N>>::replace_all(hay_mut, with)
108    }
109
110    /// See [`Regex::replace_all_using`].
111    fn replace_all_using<M: OwnedHaystackable<I>>(
112        &self,
113        hay_mut: &mut M,
114        using: impl FnMut() -> M,
115    ) -> usize {
116        <Self as Regex<I, N>>::replace_all_using(hay_mut, using)
117    }
118
119    /// See [`Regex::replace_captured`].
120    fn replace_captured<M, F>(&self, hay_mut: &mut M, replacer: F) -> bool
121    where
122        M: OwnedHaystackable<I>,
123        F: for<'a> FnOnce(Self::Capture<'a, <M::Hay<'a> as HaystackIter<'a>>::Slice>) -> M,
124    {
125        <Self as Regex<I, N>>::replace_captured::<M, F>(hay_mut, replacer)
126    }
127
128    /// See [`Regex::replace_all_captured`].
129    fn replace_all_captured<M, F>(&self, hay_mut: &mut M, replacer: F) -> usize
130    where
131        M: OwnedHaystackable<I>,
132        F: for<'a> FnMut(Self::Capture<'a, <M::Hay<'a> as HaystackIter<'a>>::Slice>) -> M,
133    {
134        <Self as Regex<I, N>>::replace_all_captured::<M, F>(hay_mut, replacer)
135    }
136}