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::char;
|
||||||
use nom::character::complete::one_of;
|
use nom::character::complete::one_of;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::cmp::Ordering;
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
use std::ops::Deref;
|
||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Default, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
|
||||||
pub struct Regex {
|
pub struct Regex(String);
|
||||||
pub input: String,
|
|
||||||
#[serde(skip)]
|
|
||||||
pub value: Option<regex::Regex>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> From<&'a str> for Regex {
|
impl<'a> From<&'a str> for Regex {
|
||||||
fn from(r: &str) -> Regex {
|
fn from(r: &str) -> Regex {
|
||||||
let r = r.replace("\\/", "/");
|
Regex(r.replace("\\/", "/").to_string())
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialOrd for Regex {
|
impl Deref for Regex {
|
||||||
#[inline]
|
type Target = String;
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
fn deref(&self) -> &Self::Target {
|
||||||
Some(self.input.cmp(&other.input))
|
&self.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Regex {
|
impl fmt::Display for Regex {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
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::False => other.is_false(),
|
||||||
Value::Thing(v) => match other {
|
Value::Thing(v) => match other {
|
||||||
Value::Thing(w) => v == w,
|
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()),
|
Some(ref r) => r.is_match(v.to_string().as_str()),
|
||||||
None => false,
|
None => false,
|
||||||
},
|
},
|
||||||
|
@ -774,11 +774,11 @@ impl Value {
|
||||||
},
|
},
|
||||||
Value::Regex(v) => match other {
|
Value::Regex(v) => match other {
|
||||||
Value::Regex(w) => v == w,
|
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()),
|
Some(ref r) => r.is_match(w.to_string().as_str()),
|
||||||
None => false,
|
None => false,
|
||||||
},
|
},
|
||||||
Value::Strand(w) => match v.value {
|
Value::Strand(w) => match v.regex() {
|
||||||
Some(ref r) => r.is_match(w.as_str()),
|
Some(ref r) => r.is_match(w.as_str()),
|
||||||
None => false,
|
None => false,
|
||||||
},
|
},
|
||||||
|
@ -794,7 +794,7 @@ impl Value {
|
||||||
},
|
},
|
||||||
Value::Strand(v) => match other {
|
Value::Strand(v) => match other {
|
||||||
Value::Strand(w) => v == w,
|
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()),
|
Some(ref r) => r.is_match(v.as_str()),
|
||||||
None => false,
|
None => false,
|
||||||
},
|
},
|
||||||
|
@ -803,7 +803,7 @@ impl Value {
|
||||||
Value::Number(v) => match other {
|
Value::Number(v) => match other {
|
||||||
Value::Number(w) => v == w,
|
Value::Number(w) => v == w,
|
||||||
Value::Strand(_) => v == &other.to_number(),
|
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()),
|
Some(ref r) => r.is_match(v.to_string().as_str()),
|
||||||
None => false,
|
None => false,
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in a new issue