surrealpatch/lib/src/fnc/is.rs

179 lines
4.5 KiB
Rust
Raw Normal View History

use crate::err::Error;
use crate::sql::value::Value;
use once_cell::sync::Lazy;
use regex::Regex;
use semver::Version;
use std::char;
use uuid::Uuid;
#[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());
#[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())
}
#[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())
}
#[inline]
2022-09-21 00:57:33 +00:00
pub fn ascii((arg,): (String,)) -> Result<Value, Error> {
Ok(arg.is_ascii().into())
}
#[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())
}
#[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())
}
#[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())
}
#[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())
}
#[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())
}
#[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())
}
#[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())
}
#[inline]
2022-09-21 00:57:33 +00:00
pub fn uuid((arg,): (Value,)) -> Result<Value, Error> {
Ok(match arg {
Value::Strand(v) => Uuid::parse_str(v.as_string().as_str()).is_ok(),
Value::Uuid(_) => true,
_ => false,
}
.into())
}
#[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();
assert_eq!(value, Value::True);
2022-09-21 00:57:33 +00:00
let value = super::alphanum((String::from("y%*"),)).unwrap();
assert_eq!(value, Value::False);
}
#[test]
fn alpha() {
2022-09-21 00:57:33 +00:00
let value = super::alpha((String::from("abc"),)).unwrap();
assert_eq!(value, Value::True);
2022-09-21 00:57:33 +00:00
let value = super::alpha((String::from("1234"),)).unwrap();
assert_eq!(value, Value::False);
}
#[test]
fn ascii() {
2022-09-21 00:57:33 +00:00
let value = super::ascii((String::from("abc"),)).unwrap();
assert_eq!(value, Value::True);
2022-09-21 00:57:33 +00:00
let value = super::ascii((String::from("中国"),)).unwrap();
assert_eq!(value, Value::False);
}
#[test]
fn domain() {
2022-09-21 00:57:33 +00:00
let value = super::domain((String::from("食狮.中国"),)).unwrap();
assert_eq!(value, Value::True);
2022-09-21 00:57:33 +00:00
let value = super::domain((String::from("example-.com"),)).unwrap();
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();
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();
assert_eq!(value, Value::False);
}
#[test]
fn hexadecimal() {
2022-09-21 00:57:33 +00:00
let value = super::hexadecimal((String::from("00FF00"),)).unwrap();
assert_eq!(value, Value::True);
2022-09-21 00:57:33 +00:00
let value = super::hexadecimal((String::from("SurrealDB"),)).unwrap();
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();
assert_eq!(value, Value::True);
2022-09-21 00:57:33 +00:00
let value = super::latitude((String::from("12345"),)).unwrap();
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();
assert_eq!(value, Value::True);
2022-09-21 00:57:33 +00:00
let value = super::longitude((String::from("12345"),)).unwrap();
assert_eq!(value, Value::False);
}
#[test]
fn numeric() {
2022-09-21 00:57:33 +00:00
let value = super::numeric((String::from("12345"),)).unwrap();
assert_eq!(value, Value::True);
2022-09-21 00:57:33 +00:00
let value = super::numeric((String::from("abcde"),)).unwrap();
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();
assert_eq!(value, Value::True);
2022-09-21 00:57:33 +00:00
let value = super::semver((String::from("1.0"),)).unwrap();
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();
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();
assert_eq!(value, Value::False);
}
}