zebra_test/command/to_regex.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
//! Convenience traits for converting to [`Regex`] and [`RegexSet`].
use std::{collections::HashSet, iter};
use itertools::Itertools;
use regex::{Error, Regex, RegexBuilder, RegexSet, RegexSetBuilder};
/// A trait for converting a value to a [`Regex`].
pub trait ToRegex {
/// Converts the given value to a [`Regex`].
///
/// Returns an [`Error`] if conversion fails.
fn to_regex(&self) -> Result<Regex, Error>;
}
// Identity conversions
impl ToRegex for Regex {
fn to_regex(&self) -> Result<Regex, Error> {
Ok(self.clone())
}
}
impl ToRegex for &Regex {
fn to_regex(&self) -> Result<Regex, Error> {
Ok((*self).clone())
}
}
// Builder Conversions
impl ToRegex for RegexBuilder {
fn to_regex(&self) -> Result<Regex, Error> {
self.build()
}
}
impl ToRegex for &RegexBuilder {
fn to_regex(&self) -> Result<Regex, Error> {
self.build()
}
}
// String conversions
impl ToRegex for String {
fn to_regex(&self) -> Result<Regex, Error> {
Regex::new(self)
}
}
impl ToRegex for &String {
fn to_regex(&self) -> Result<Regex, Error> {
Regex::new(self)
}
}
impl ToRegex for &str {
fn to_regex(&self) -> Result<Regex, Error> {
Regex::new(self)
}
}
/// A trait for converting a value to a [`RegexSet`].
pub trait ToRegexSet {
/// Converts the given values to a [`RegexSet`].
///
/// When converting from a [`Regex`] or [`RegexBuilder`],
/// resets match flags and limits to the defaults.
/// Use a [`RegexSet`] or [`RegexSetBuilder`] to preserve these settings.
///
/// Returns an [`Error`] if any conversion fails.
fn to_regex_set(&self) -> Result<RegexSet, Error>;
}
// Identity conversions
impl ToRegexSet for RegexSet {
fn to_regex_set(&self) -> Result<RegexSet, Error> {
Ok(self.clone())
}
}
impl ToRegexSet for &RegexSet {
fn to_regex_set(&self) -> Result<RegexSet, Error> {
Ok((*self).clone())
}
}
// Builder Conversions
impl ToRegexSet for RegexSetBuilder {
fn to_regex_set(&self) -> Result<RegexSet, Error> {
self.build()
}
}
impl ToRegexSet for &RegexSetBuilder {
fn to_regex_set(&self) -> Result<RegexSet, Error> {
self.build()
}
}
// Single item conversion
impl<T> ToRegexSet for T
where
T: ToRegex,
{
fn to_regex_set(&self) -> Result<RegexSet, Error> {
let regex = self.to_regex()?;
// This conversion discards flags and limits from Regex and RegexBuilder.
let regex = regex.as_str();
RegexSet::new(iter::once(regex))
}
}
/// A trait for collecting an iterator into a [`RegexSet`].
pub trait CollectRegexSet {
/// Collects the iterator values to a [`RegexSet`].
///
/// When converting from a [`Regex`] or [`RegexBuilder`],
/// resets match flags and limits to the defaults.
///
/// Use a [`RegexSet`] or [`RegexSetBuilder`] to preserve these settings,
/// via the `*_regex_set` methods.
///
/// Returns an [`Error`] if any conversion fails.
fn collect_regex_set(self) -> Result<RegexSet, Error>;
}
// Multi item conversion
impl<I> CollectRegexSet for I
where
I: IntoIterator,
I::Item: ToRegexSet,
{
fn collect_regex_set(self) -> Result<RegexSet, Error> {
let regexes: Result<Vec<RegexSet>, Error> = self
.into_iter()
.map(|item| item.to_regex_set())
.try_collect();
let regexes = regexes?;
// This conversion discards flags and limits from Regex and RegexBuilder.
let regexes = regexes.iter().flat_map(|regex_set| regex_set.patterns());
RegexSet::new(regexes)
}
}
/// A trait for getting additional information from a [`RegexSet`].
pub trait RegexSetExt {
/// Returns the regex patterns for the supplied `indexes`.
fn patterns_for_indexes(&self, indexes: &HashSet<usize>) -> Vec<String>;
}
impl RegexSetExt for RegexSet {
fn patterns_for_indexes(&self, indexes: &HashSet<usize>) -> Vec<String> {
self.patterns()
.iter()
.enumerate()
.filter(|(index, _regex)| indexes.contains(index))
.map(|(_index, regex)| regex.to_string())
.collect()
}
}