Implement SQL Regex as a newtype tuple struct
This commit is contained in:
parent
79e9269f14
commit
a03d5a7551
2 changed files with 23 additions and 32 deletions
|
@ -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<regex::Regex>,
|
||||
}
|
||||
#[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,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Regex {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.input == other.input
|
||||
Regex(r.replace("\\/", "/").to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for Regex {
|
||||
#[inline]
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
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<regex::Regex> {
|
||||
match regex::Regex::new(&self.0) {
|
||||
Ok(v) => Some(v),
|
||||
Err(_) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue