winnow_regex/
regex_trait.rs

1/// see
2/// [`regex::CaptureLocations`](https://docs.rs/regex/latest/regex/struct.CaptureLocations.html)
3/// for more details
4#[allow(clippy::len_without_is_empty)]
5pub trait CaptureLocations {
6    type Input: ?Sized;
7    fn get(&self, i: usize) -> Option<(usize, usize)>;
8    fn len(&self) -> usize;
9}
10
11impl CaptureLocations for regex::CaptureLocations {
12    type Input = str;
13    #[inline]
14    fn get(&self, i: usize) -> Option<(usize, usize)> {
15        regex::CaptureLocations::get(self, i)
16    }
17
18    #[inline]
19    fn len(&self) -> usize {
20        regex::CaptureLocations::len(self)
21    }
22}
23
24impl CaptureLocations for regex::bytes::CaptureLocations {
25    type Input = [u8];
26    #[inline]
27    fn get(&self, i: usize) -> Option<(usize, usize)> {
28        regex::bytes::CaptureLocations::get(self, i)
29    }
30
31    #[inline]
32    fn len(&self) -> usize {
33        regex::bytes::CaptureLocations::len(self)
34    }
35}
36
37pub trait Regex {
38    type Haystack<'h>;
39    type CaptureLocations: CaptureLocations;
40
41    fn capture_locations(&self) -> Self::CaptureLocations;
42    fn captures_read(
43        &self,
44        locs: &mut Self::CaptureLocations,
45        haystack: Self::Haystack<'_>,
46    ) -> Option<(usize, usize)>;
47}
48
49impl Regex for regex::Regex {
50    type Haystack<'h> = &'h str;
51    type CaptureLocations = regex::CaptureLocations;
52
53    #[inline]
54    fn capture_locations(&self) -> Self::CaptureLocations {
55        regex::Regex::capture_locations(self)
56    }
57
58    #[inline]
59    fn captures_read(
60        &self,
61        locs: &mut Self::CaptureLocations,
62        haystack: Self::Haystack<'_>,
63    ) -> Option<(usize, usize)> {
64        regex::Regex::captures_read(self, locs, haystack).map(|c| (c.start(), c.end()))
65    }
66}
67
68impl Regex for regex::bytes::Regex {
69    type Haystack<'h> = &'h [u8];
70    type CaptureLocations = regex::bytes::CaptureLocations;
71
72    #[inline]
73    fn capture_locations(&self) -> Self::CaptureLocations {
74        regex::bytes::Regex::capture_locations(self)
75    }
76
77    #[inline]
78    fn captures_read(
79        &self,
80        locs: &mut Self::CaptureLocations,
81        haystack: Self::Haystack<'_>,
82    ) -> Option<(usize, usize)> {
83        regex::bytes::Regex::captures_read(self, locs, haystack).map(|c| (c.start(), c.end()))
84    }
85}