From 8ca211f96d96ce9fd663415d9502848f4a1e50fc Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Mon, 4 Jul 2022 10:41:18 +0100 Subject: [PATCH] Simplify SQL string::slug function implementation --- Cargo.lock | 15 +++------------ lib/Cargo.toml | 2 +- lib/src/fnc/string.rs | 4 ++-- lib/src/fnc/util/mod.rs | 1 + lib/src/fnc/util/string/mod.rs | 23 +++++++++++++++++++++++ 5 files changed, 30 insertions(+), 15 deletions(-) create mode 100644 lib/src/fnc/util/string/mod.rs diff --git a/Cargo.lock b/Cargo.lock index c30fcb45..74574b3e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -614,9 +614,9 @@ dependencies = [ [[package]] name = "deunicode" -version = "0.4.3" +version = "1.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "850878694b7933ca4c9569d30a34b55031b9b139ee1fc7b94a527c4ef960d690" +checksum = "f2c9736e15e7df1638a7f6eee92a6511615c738246a052af5ba86f039b65aede" [[package]] name = "digest" @@ -2575,15 +2575,6 @@ version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eb703cfe953bccee95685111adeedb76fabe4e97549a58d16f03ea7b9367bb32" -[[package]] -name = "slug" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3bc762e6a4b6c6fcaade73e77f9ebc6991b676f88bb2358bddb56560f073373" -dependencies = [ - "deunicode", -] - [[package]] name = "smallvec" version = "1.8.0" @@ -2694,6 +2685,7 @@ dependencies = [ "bigdecimal", "boa_engine", "chrono", + "deunicode", "dmp", "echodb", "futures 0.3.21", @@ -2715,7 +2707,6 @@ dependencies = [ "serde", "sha-1 0.10.0", "sha2", - "slug", "storekey", "surrealdb-derive", "thiserror", diff --git a/lib/Cargo.toml b/lib/Cargo.toml index ff343c0a..4b6bb95e 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -22,6 +22,7 @@ boa = { version = "0.15.0", package = "boa_engine", optional = true } channel = { version = "1.6.1", package = "async-channel" } chrono = { version = "0.4.19", features = ["serde"] } derive = { version = "0.3.0", package = "surrealdb-derive" } +deunicode = "1.3.1" dmp = "0.1.1" echodb = { version = "0.3.0", optional = true } executor = { version = "1.4.1", package = "async-executor", optional = true } @@ -44,7 +45,6 @@ scrypt = "0.10.0" serde = { version = "1.0.138", features = ["derive"] } sha-1 = "0.10.0" sha2 = "0.10.2" -slug = "0.1.4" storekey = "0.3.0" thiserror = "1.0.31" tikv = { version = "0.1.0", package = "tikv-client", optional = true } diff --git a/lib/src/fnc/string.rs b/lib/src/fnc/string.rs index 7b86345c..67b5c831 100644 --- a/lib/src/fnc/string.rs +++ b/lib/src/fnc/string.rs @@ -1,7 +1,7 @@ use crate::ctx::Context; use crate::err::Error; +use crate::fnc::util::string; use crate::sql::value::Value; -use slug::slugify; pub fn concat(_: &Context, args: Vec) -> Result { Ok(args.into_iter().map(|x| x.as_string()).collect::>().concat().into()) @@ -56,7 +56,7 @@ pub fn slice(_: &Context, mut args: Vec) -> Result { } pub fn slug(_: &Context, mut args: Vec) -> Result { - Ok(slugify(&args.remove(0).as_string()).into()) + Ok(string::slug(&args.remove(0).as_string()).into()) } pub fn split(_: &Context, mut args: Vec) -> Result { diff --git a/lib/src/fnc/util/mod.rs b/lib/src/fnc/util/mod.rs index a529dc13..2cd099a5 100644 --- a/lib/src/fnc/util/mod.rs +++ b/lib/src/fnc/util/mod.rs @@ -1,3 +1,4 @@ pub mod geo; pub mod http; pub mod math; +pub mod string; diff --git a/lib/src/fnc/util/string/mod.rs b/lib/src/fnc/util/string/mod.rs new file mode 100644 index 00000000..e9a08259 --- /dev/null +++ b/lib/src/fnc/util/string/mod.rs @@ -0,0 +1,23 @@ +use deunicode::deunicode; +use once_cell::sync::Lazy; +use regex::Regex; + +static SIMPLES: Lazy = Lazy::new(|| Regex::new("[^a-z0-9-_]").unwrap()); +static HYPHENS: Lazy = Lazy::new(|| Regex::new("-+").unwrap()); + +pub fn slug>(s: S) -> String { + // Get a reference + let s = s.as_ref(); + // Convert unicode to ascii + let s = deunicode(s); + // Convert strgin to lowercase + let s = s.to_ascii_lowercase(); + // Replace any non-simple characters + let s = SIMPLES.replace_all(s.as_ref(), "-"); + // Replace any duplicated hyphens + let s = HYPHENS.replace_all(s.as_ref(), "-"); + // Remove any surrounding hyphens + let s = s.trim_matches('-'); + // Return the string + s.to_owned() +}