diff --git a/lib/src/sql/regex.rs b/lib/src/sql/regex.rs index 5d4cae40..172598c7 100644 --- a/lib/src/sql/regex.rs +++ b/lib/src/sql/regex.rs @@ -4,47 +4,38 @@ use nom::bytes::complete::is_not; use nom::character::complete::char; use nom::character::complete::one_of; use serde::{Deserialize, Serialize}; -use std::cmp::Ordering; use std::fmt; +use std::ops::Deref; use std::str; -#[derive(Clone, Debug, Default, Serialize, Deserialize)] -pub struct Regex { - pub input: String, - #[serde(skip)] - pub value: Option, -} +#[derive(Clone, Debug, Default, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)] +pub struct Regex(String); impl<'a> From<&'a str> for Regex { fn from(r: &str) -> Regex { - let r = r.replace("\\/", "/"); - let r = r.as_str(); - Regex { - input: String::from(r), - value: match regex::Regex::new(r) { - Ok(v) => Some(v), - Err(_) => None, - }, - } + Regex(r.replace("\\/", "/").to_string()) } } -impl PartialEq for Regex { - fn eq(&self, other: &Self) -> bool { - self.input == other.input - } -} - -impl PartialOrd for Regex { - #[inline] - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.input.cmp(&other.input)) +impl Deref for Regex { + type Target = String; + fn deref(&self) -> &Self::Target { + &self.0 } } impl fmt::Display for Regex { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "/{}/", &self.input) + write!(f, "/{}/", &self.0) + } +} + +impl Regex { + pub fn regex(&self) -> Option { + match regex::Regex::new(&self.0) { + Ok(v) => Some(v), + Err(_) => None, + } } } diff --git a/lib/src/sql/value/value.rs b/lib/src/sql/value/value.rs index 18c58ef4..a5de50f0 100644 --- a/lib/src/sql/value/value.rs +++ b/lib/src/sql/value/value.rs @@ -766,7 +766,7 @@ impl Value { Value::False => other.is_false(), Value::Thing(v) => match other { Value::Thing(w) => v == w, - Value::Regex(w) => match w.value { + Value::Regex(w) => match w.regex() { Some(ref r) => r.is_match(v.to_string().as_str()), None => false, }, @@ -774,11 +774,11 @@ impl Value { }, Value::Regex(v) => match other { Value::Regex(w) => v == w, - Value::Number(w) => match v.value { + Value::Number(w) => match v.regex() { Some(ref r) => r.is_match(w.to_string().as_str()), None => false, }, - Value::Strand(w) => match v.value { + Value::Strand(w) => match v.regex() { Some(ref r) => r.is_match(w.as_str()), None => false, }, @@ -794,7 +794,7 @@ impl Value { }, Value::Strand(v) => match other { Value::Strand(w) => v == w, - Value::Regex(w) => match w.value { + Value::Regex(w) => match w.regex() { Some(ref r) => r.is_match(v.as_str()), None => false, }, @@ -803,7 +803,7 @@ impl Value { Value::Number(v) => match other { Value::Number(w) => v == w, Value::Strand(_) => v == &other.to_number(), - Value::Regex(w) => match w.value { + Value::Regex(w) => match w.regex() { Some(ref r) => r.is_match(v.to_string().as_str()), None => false, },