vrl/value/value/
regex.rs

1use bytes::Bytes;
2use regex::Regex;
3use std::cmp::Ordering;
4use std::sync::Arc;
5use std::{
6    hash::{Hash, Hasher},
7    ops::Deref,
8};
9
10/// Wraps a `Regex` and provides several trait implementations, such as `PartialOrd`
11#[derive(Debug, Clone)]
12#[allow(clippy::module_name_repetitions)]
13pub struct ValueRegex(Arc<Regex>);
14
15impl ValueRegex {
16    /// Create a new `ValueRegex` from the inner `Regex` that is wraps
17    #[must_use]
18    pub const fn new(regex: Arc<Regex>) -> Self {
19        Self(regex)
20    }
21
22    /// Returns a `Bytes` of the string representation of the regex
23    #[must_use]
24    pub fn as_bytes(&self) -> Bytes {
25        Bytes::copy_from_slice(self.as_bytes_slice())
26    }
27
28    /// Returns a byte array of the string representation of the regex
29    #[must_use]
30    pub fn as_bytes_slice(&self) -> &[u8] {
31        self.as_str().as_bytes()
32    }
33
34    /// Returns the inner Regex value
35    #[must_use]
36    pub fn into_inner(self) -> Arc<Regex> {
37        self.0
38    }
39}
40
41impl Eq for ValueRegex {}
42
43impl PartialEq for ValueRegex {
44    fn eq(&self, other: &Self) -> bool {
45        self.0.as_str() == other.0.as_str()
46    }
47}
48
49impl Hash for ValueRegex {
50    fn hash<H: Hasher>(&self, state: &mut H) {
51        self.0.as_str().hash(state);
52    }
53}
54
55impl Deref for ValueRegex {
56    type Target = Regex;
57
58    fn deref(&self) -> &Self::Target {
59        &self.0
60    }
61}
62
63impl From<Arc<Regex>> for ValueRegex {
64    fn from(regex: Arc<Regex>) -> Self {
65        Self(regex)
66    }
67}
68
69impl From<Regex> for ValueRegex {
70    fn from(r: Regex) -> Self {
71        Self::new(Arc::new(r))
72    }
73}
74
75impl PartialOrd for ValueRegex {
76    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
77        self.0
78            .as_str()
79            .as_bytes()
80            .partial_cmp(other.0.as_str().as_bytes())
81    }
82}