73 lines
1.5 KiB
Rust
73 lines
1.5 KiB
Rust
use crate::sql::error::IResult;
|
|
use nom::bytes::complete::escaped;
|
|
use nom::bytes::complete::is_not;
|
|
use nom::character::complete::char;
|
|
use nom::character::complete::one_of;
|
|
use serde::{Deserialize, Serialize};
|
|
use std::fmt;
|
|
use std::ops::Deref;
|
|
use std::str;
|
|
|
|
#[derive(Clone, Debug, Default, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
|
|
pub struct Regex(String);
|
|
|
|
impl From<&str> for Regex {
|
|
fn from(r: &str) -> Regex {
|
|
Regex(r.replace("\\/", "/"))
|
|
}
|
|
}
|
|
|
|
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.0)
|
|
}
|
|
}
|
|
|
|
impl Regex {
|
|
pub fn regex(&self) -> Option<regex::Regex> {
|
|
match regex::Regex::new(&self.0) {
|
|
Ok(v) => Some(v),
|
|
Err(_) => None,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn regex(i: &str) -> IResult<&str, Regex> {
|
|
let (i, _) = char('/')(i)?;
|
|
let (i, v) = escaped(is_not("\\/"), '\\', one_of("/"))(i)?;
|
|
let (i, _) = char('/')(i)?;
|
|
Ok((i, Regex::from(v)))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn regex_simple() {
|
|
let sql = "/test/";
|
|
let res = regex(sql);
|
|
assert!(res.is_ok());
|
|
let out = res.unwrap().1;
|
|
assert_eq!("/test/", format!("{}", out));
|
|
assert_eq!(out, Regex::from("test"));
|
|
}
|
|
|
|
#[test]
|
|
fn regex_complex() {
|
|
let sql = r"/test\/[a-z]+\/.*/";
|
|
let res = regex(sql);
|
|
assert!(res.is_ok());
|
|
let out = res.unwrap().1;
|
|
assert_eq!(r"/test/[a-z]+/.*/", format!("{}", out));
|
|
assert_eq!(out, Regex::from("test/[a-z]+/.*"));
|
|
}
|
|
}
|