use crate::net::endpoint::metadata::Value;
#[derive(serde::Serialize, serde::Deserialize, Debug, schemars::JsonSchema)]
pub struct Regex {
#[serde(with = "serde_regex")]
#[schemars(with = "String")]
pub pattern: regex::bytes::Regex,
}
impl super::CaptureStrategy for Regex {
fn capture(&self, contents: &[u8]) -> Option<(Value, isize)> {
let matches = self
.pattern
.find_iter(contents)
.map(|mat| Value::Bytes(bytes::Bytes::copy_from_slice(mat.as_bytes())))
.collect::<Vec<_>>();
if matches.len() > 1 {
Some((Value::List(matches), 0))
} else {
matches.into_iter().next().map(|v| (v, 0))
}
}
}
impl PartialEq for Regex {
fn eq(&self, rhs: &Self) -> bool {
self.pattern.as_str() == rhs.pattern.as_str()
}
}