2022-01-13 17:37:46 +00:00
|
|
|
use crate::err::Error;
|
|
|
|
use crate::sql::value::Value;
|
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
use regex::Regex;
|
2022-09-01 17:46:36 +00:00
|
|
|
use semver::Version;
|
2022-01-13 17:37:46 +00:00
|
|
|
use std::char;
|
2022-09-01 17:46:36 +00:00
|
|
|
use uuid::Uuid;
|
2022-01-13 17:37:46 +00:00
|
|
|
|
|
|
|
#[rustfmt::skip] static LATITUDE_RE: Lazy<Regex> = Lazy::new(|| Regex::new("^[-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?)$").unwrap());
|
|
|
|
#[rustfmt::skip] static LONGITUDE_RE: Lazy<Regex> = Lazy::new(|| Regex::new("^[-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?)$").unwrap());
|
|
|
|
|
2022-09-01 17:46:36 +00:00
|
|
|
#[inline]
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn alphanum((arg,): (String,)) -> Result<Value, Error> {
|
|
|
|
Ok(arg.chars().all(char::is_alphanumeric).into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 17:46:36 +00:00
|
|
|
#[inline]
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn alpha((arg,): (String,)) -> Result<Value, Error> {
|
|
|
|
Ok(arg.chars().all(char::is_alphabetic).into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 17:46:36 +00:00
|
|
|
#[inline]
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn ascii((arg,): (String,)) -> Result<Value, Error> {
|
|
|
|
Ok(arg.is_ascii().into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 17:46:36 +00:00
|
|
|
#[inline]
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn domain((arg,): (String,)) -> Result<Value, Error> {
|
|
|
|
Ok(addr::parse_domain_name(arg.as_str()).is_ok().into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 17:46:36 +00:00
|
|
|
#[inline]
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn email((arg,): (String,)) -> Result<Value, Error> {
|
|
|
|
Ok(addr::parse_email_address(arg.as_str()).is_ok().into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 17:46:36 +00:00
|
|
|
#[inline]
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn hexadecimal((arg,): (String,)) -> Result<Value, Error> {
|
|
|
|
Ok(arg.chars().all(|x| char::is_ascii_hexdigit(&x)).into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 17:46:36 +00:00
|
|
|
#[inline]
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn latitude((arg,): (String,)) -> Result<Value, Error> {
|
|
|
|
Ok(LATITUDE_RE.is_match(arg.as_str()).into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 17:46:36 +00:00
|
|
|
#[inline]
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn longitude((arg,): (String,)) -> Result<Value, Error> {
|
|
|
|
Ok(LONGITUDE_RE.is_match(arg.as_str()).into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 17:46:36 +00:00
|
|
|
#[inline]
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn numeric((arg,): (String,)) -> Result<Value, Error> {
|
|
|
|
Ok(arg.chars().all(char::is_numeric).into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 17:46:36 +00:00
|
|
|
#[inline]
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn semver((arg,): (String,)) -> Result<Value, Error> {
|
|
|
|
Ok(Version::parse(arg.as_str()).is_ok().into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 17:46:36 +00:00
|
|
|
#[inline]
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn uuid((arg,): (Value,)) -> Result<Value, Error> {
|
|
|
|
Ok(match arg {
|
2022-09-22 23:54:53 +00:00
|
|
|
Value::Strand(v) => Uuid::parse_str(v.as_string().as_str()).is_ok(),
|
|
|
|
Value::Uuid(_) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
.into())
|
2022-09-01 17:46:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::sql::value::Value;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn alphanum() {
|
2022-09-21 00:57:33 +00:00
|
|
|
let value = super::alphanum((String::from("abc123"),)).unwrap();
|
2022-09-01 17:46:36 +00:00
|
|
|
assert_eq!(value, Value::True);
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
let value = super::alphanum((String::from("y%*"),)).unwrap();
|
2022-09-01 17:46:36 +00:00
|
|
|
assert_eq!(value, Value::False);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn alpha() {
|
2022-09-21 00:57:33 +00:00
|
|
|
let value = super::alpha((String::from("abc"),)).unwrap();
|
2022-09-01 17:46:36 +00:00
|
|
|
assert_eq!(value, Value::True);
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
let value = super::alpha((String::from("1234"),)).unwrap();
|
2022-09-01 17:46:36 +00:00
|
|
|
assert_eq!(value, Value::False);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ascii() {
|
2022-09-21 00:57:33 +00:00
|
|
|
let value = super::ascii((String::from("abc"),)).unwrap();
|
2022-09-01 17:46:36 +00:00
|
|
|
assert_eq!(value, Value::True);
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
let value = super::ascii((String::from("中国"),)).unwrap();
|
2022-09-01 17:46:36 +00:00
|
|
|
assert_eq!(value, Value::False);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn domain() {
|
2022-09-21 00:57:33 +00:00
|
|
|
let value = super::domain((String::from("食狮.中国"),)).unwrap();
|
2022-09-01 17:46:36 +00:00
|
|
|
assert_eq!(value, Value::True);
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
let value = super::domain((String::from("example-.com"),)).unwrap();
|
2022-09-01 17:46:36 +00:00
|
|
|
assert_eq!(value, Value::False);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn email() {
|
2022-09-21 00:57:33 +00:00
|
|
|
let input = (String::from("user@[fd79:cdcb:38cc:9dd:f686:e06d:32f3:c123]"),);
|
|
|
|
let value = super::email(input).unwrap();
|
2022-09-01 17:46:36 +00:00
|
|
|
assert_eq!(value, Value::True);
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
let input = (String::from("john..doe@example.com"),);
|
|
|
|
let value = super::email(input).unwrap();
|
2022-09-01 17:46:36 +00:00
|
|
|
assert_eq!(value, Value::False);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn hexadecimal() {
|
2022-09-21 00:57:33 +00:00
|
|
|
let value = super::hexadecimal((String::from("00FF00"),)).unwrap();
|
2022-09-01 17:46:36 +00:00
|
|
|
assert_eq!(value, Value::True);
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
let value = super::hexadecimal((String::from("SurrealDB"),)).unwrap();
|
2022-09-01 17:46:36 +00:00
|
|
|
assert_eq!(value, Value::False);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn latitude() {
|
2022-09-21 00:57:33 +00:00
|
|
|
let value = super::latitude((String::from("-0.118092"),)).unwrap();
|
2022-09-01 17:46:36 +00:00
|
|
|
assert_eq!(value, Value::True);
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
let value = super::latitude((String::from("12345"),)).unwrap();
|
2022-09-01 17:46:36 +00:00
|
|
|
assert_eq!(value, Value::False);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn longitude() {
|
2022-09-21 00:57:33 +00:00
|
|
|
let value = super::longitude((String::from("51.509865"),)).unwrap();
|
2022-09-01 17:46:36 +00:00
|
|
|
assert_eq!(value, Value::True);
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
let value = super::longitude((String::from("12345"),)).unwrap();
|
2022-09-01 17:46:36 +00:00
|
|
|
assert_eq!(value, Value::False);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn numeric() {
|
2022-09-21 00:57:33 +00:00
|
|
|
let value = super::numeric((String::from("12345"),)).unwrap();
|
2022-09-01 17:46:36 +00:00
|
|
|
assert_eq!(value, Value::True);
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
let value = super::numeric((String::from("abcde"),)).unwrap();
|
2022-09-01 17:46:36 +00:00
|
|
|
assert_eq!(value, Value::False);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn semver() {
|
2022-09-21 00:57:33 +00:00
|
|
|
let value = super::semver((String::from("1.0.0"),)).unwrap();
|
2022-09-01 17:46:36 +00:00
|
|
|
assert_eq!(value, Value::True);
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
let value = super::semver((String::from("1.0"),)).unwrap();
|
2022-09-01 17:46:36 +00:00
|
|
|
assert_eq!(value, Value::False);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn uuid() {
|
2022-09-21 00:57:33 +00:00
|
|
|
let input = (String::from("123e4567-e89b-12d3-a456-426614174000").into(),);
|
|
|
|
let value = super::uuid(input).unwrap();
|
2022-09-01 17:46:36 +00:00
|
|
|
assert_eq!(value, Value::True);
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
let input = (String::from("foo-bar").into(),);
|
|
|
|
let value = super::uuid(input).unwrap();
|
2022-09-01 17:46:36 +00:00
|
|
|
assert_eq!(value, Value::False);
|
|
|
|
}
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|