extract str functions into separate modules.
add first sql based unit test.
This commit is contained in:
178
src/lib.rs
178
src/lib.rs
@@ -1,149 +1,39 @@
|
||||
pub mod modules {
|
||||
pub mod after;
|
||||
pub mod ascii;
|
||||
pub mod before;
|
||||
pub mod case;
|
||||
pub mod contains;
|
||||
pub mod length;
|
||||
pub mod markdown;
|
||||
pub mod random;
|
||||
pub mod split;
|
||||
pub mod start;
|
||||
pub mod substr;
|
||||
pub mod uuid;
|
||||
}
|
||||
|
||||
use pgrx::prelude::*;
|
||||
|
||||
use rand::distributions::{Alphanumeric, DistString};
|
||||
|
||||
use any_ascii::any_ascii;
|
||||
use inflector::cases::{
|
||||
camelcase, kebabcase, pascalcase, screamingsnakecase, snakecase, titlecase,
|
||||
};
|
||||
use inflector::string::{pluralize, singularize};
|
||||
use pulldown_cmark::{html, Options, Parser};
|
||||
use str_slug::StrSlug;
|
||||
use uuid::Uuid;
|
||||
pub use modules::after::*;
|
||||
pub use modules::ascii::*;
|
||||
pub use modules::before::*;
|
||||
pub use modules::case::*;
|
||||
pub use modules::contains::*;
|
||||
pub use modules::length::*;
|
||||
pub use modules::markdown::*;
|
||||
pub use modules::random::*;
|
||||
pub use modules::split::*;
|
||||
pub use modules::start::*;
|
||||
pub use modules::substr::*;
|
||||
pub use modules::uuid::*;
|
||||
|
||||
pgrx::pg_module_magic!();
|
||||
|
||||
#[pg_extern]
|
||||
fn str_random(length: i32) -> String {
|
||||
Alphanumeric.sample_string(&mut rand::thread_rng(), length as usize)
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
fn str_length(input: &str) -> i32 {
|
||||
input.len() as i32
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
fn str_after<'a>(input: &'a str, search: &str) -> &'a str {
|
||||
let matches: Vec<_> = input.match_indices(search).collect();
|
||||
match matches.first() {
|
||||
None => input,
|
||||
Some(x) => &input[x.1.len()..],
|
||||
}
|
||||
}
|
||||
// #[pg_extern]
|
||||
// fn str_after_last<'a>(input: &'a str, search: &str) -> &'a str {
|
||||
// }
|
||||
|
||||
// fn str_before<'a>(input: &'a str, search: &str) -> &'a str {
|
||||
// }
|
||||
// fn str_beforeLast<'a>(input: &'a str, search: &str) -> &'a str {
|
||||
// }
|
||||
// fn str_between<'a>(input: &'a str, search: &str) -> &'a str {
|
||||
// }
|
||||
|
||||
#[pg_extern]
|
||||
fn str_uuid() -> String {
|
||||
Uuid::new_v4().to_string()
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
fn str_ascii(input: &str) -> String {
|
||||
any_ascii(input)
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
fn str_is_ascii(input: &str) -> bool {
|
||||
input.is_ascii()
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
fn str_contains(input: &str, search: &str) -> bool {
|
||||
input.contains(search)
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
fn str_contains_all(input: &str, search: Vec<&str>) -> bool {
|
||||
search.iter().all(|s| input.contains(s))
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
fn str_lower(input: &str) -> String {
|
||||
input.to_lowercase()
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
fn str_upper(input: &str) -> String {
|
||||
input.to_uppercase()
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
fn str_slug(input: &str, sep: char) -> String {
|
||||
let mut slug = StrSlug::new();
|
||||
slug.separator = sep;
|
||||
slug.slug(input)
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
fn str_singular(input: &str) -> String {
|
||||
singularize::to_singular(input)
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
fn str_plural(input: &str) -> String {
|
||||
pluralize::to_plural(input)
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
fn str_title(input: &str) -> String {
|
||||
titlecase::to_title_case(input)
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
fn str_camel(input: &str) -> String {
|
||||
camelcase::to_camel_case(input)
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
fn str_kebab(input: &str) -> String {
|
||||
kebabcase::to_kebab_case(input)
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
fn str_snake(input: &str) -> String {
|
||||
snakecase::to_snake_case(input)
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
fn str_studly(input: &str) -> String {
|
||||
pascalcase::to_pascal_case(input)
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
fn str_scream(input: &str) -> String {
|
||||
screamingsnakecase::to_screaming_snake_case(input)
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
fn str_markdown(input: &str) -> String {
|
||||
// Set up options and parser. Strikethroughs are not part of the CommonMark standard
|
||||
// and we therefore must enable it explicitly.
|
||||
let mut options = Options::empty();
|
||||
options.insert(Options::ENABLE_STRIKETHROUGH);
|
||||
let parser = Parser::new_ext(input, options);
|
||||
|
||||
// Write to String buffer.
|
||||
let mut html_output: String = String::with_capacity(input.len() * 3 / 2);
|
||||
html::push_html(&mut html_output, parser);
|
||||
html_output
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
fn str_substr(input: &str, start: i32, end: i32) -> &str {
|
||||
&input[start as usize..end as usize]
|
||||
}
|
||||
#[pg_extern]
|
||||
fn str_replace(input: &'static str, old: &'static str, new: &'static str) -> String {
|
||||
fn str_replace<'a>(input: &'a str, old: &'a str, new: &'a str) -> String {
|
||||
input.replace(old, new)
|
||||
}
|
||||
|
||||
@@ -153,18 +43,8 @@ fn str_append(mut input: String, extra: &str) -> String {
|
||||
input
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
fn str_split(input: &'static str, pattern: &str) -> Vec<&'static str> {
|
||||
input.split_terminator(pattern).into_iter().collect()
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
fn str_split_set<'a>(input: &'a str, pattern: &'a str) -> SetOfIterator<'a, &'a str> {
|
||||
SetOfIterator::new(input.split_terminator(pattern).into_iter())
|
||||
}
|
||||
|
||||
#[cfg(any(test, feature = "pg_test"))]
|
||||
#[pg_schema]
|
||||
#[pgrx::pg_schema]
|
||||
mod tests {
|
||||
|
||||
// #[pg_test]
|
||||
|
||||
20
src/modules/after.rs
Normal file
20
src/modules/after.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
use pgrx::prelude::*;
|
||||
|
||||
#[pg_extern]
|
||||
pub fn str_after<'a>(input: &'a str, search: &str) -> &'a str {
|
||||
let matches: Vec<_> = input.match_indices(search).collect();
|
||||
match matches.first() {
|
||||
None => input,
|
||||
Some(x) => &input[x.1.len()..],
|
||||
}
|
||||
}
|
||||
// #[pg_extern]
|
||||
// fn str_after_last<'a>(input: &'a str, search: &str) -> &'a str {
|
||||
// }
|
||||
|
||||
#[cfg(any(test, feature = "pg_test"))]
|
||||
mod tests {
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
use pgrx::prelude::*;
|
||||
}
|
||||
19
src/modules/ascii.rs
Normal file
19
src/modules/ascii.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
use any_ascii::any_ascii;
|
||||
use pgrx::prelude::*;
|
||||
|
||||
#[pg_extern]
|
||||
pub fn str_ascii(input: &str) -> String {
|
||||
any_ascii(input)
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
pub fn str_is_ascii(input: &str) -> bool {
|
||||
input.is_ascii()
|
||||
}
|
||||
|
||||
#[cfg(any(test, feature = "pg_test"))]
|
||||
mod tests {
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
use pgrx::prelude::*;
|
||||
}
|
||||
4
src/modules/before.rs
Normal file
4
src/modules/before.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
// fn str_before<'a>(input: &'a str, search: &str) -> &'a str {
|
||||
// }
|
||||
// fn str_beforeLast<'a>(input: &'a str, search: &str) -> &'a str {
|
||||
// }
|
||||
71
src/modules/case.rs
Normal file
71
src/modules/case.rs
Normal file
@@ -0,0 +1,71 @@
|
||||
use pgrx::prelude::*;
|
||||
|
||||
use inflector::cases::{
|
||||
camelcase, kebabcase, pascalcase, screamingsnakecase, snakecase, titlecase,
|
||||
};
|
||||
use inflector::string::{pluralize, singularize};
|
||||
use str_slug::StrSlug;
|
||||
|
||||
#[pg_extern]
|
||||
pub fn str_lower(input: &str) -> String {
|
||||
input.to_lowercase()
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
pub fn str_upper(input: &str) -> String {
|
||||
input.to_uppercase()
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
pub fn str_slug(input: &str, sep: char) -> String {
|
||||
let mut slug = StrSlug::new();
|
||||
slug.separator = sep;
|
||||
slug.slug(input)
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
pub fn str_singular(input: &str) -> String {
|
||||
singularize::to_singular(input)
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
pub fn str_plural(input: &str) -> String {
|
||||
pluralize::to_plural(input)
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
pub fn str_title(input: &str) -> String {
|
||||
titlecase::to_title_case(input)
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
pub fn str_camel(input: &str) -> String {
|
||||
camelcase::to_camel_case(input)
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
pub fn str_kebab(input: &str) -> String {
|
||||
kebabcase::to_kebab_case(input)
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
pub fn str_snake(input: &str) -> String {
|
||||
snakecase::to_snake_case(input)
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
pub fn str_studly(input: &str) -> String {
|
||||
pascalcase::to_pascal_case(input)
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
pub fn str_scream(input: &str) -> String {
|
||||
screamingsnakecase::to_screaming_snake_case(input)
|
||||
}
|
||||
|
||||
#[cfg(any(test, feature = "pg_test"))]
|
||||
mod tests {
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
use pgrx::prelude::*;
|
||||
}
|
||||
18
src/modules/contains.rs
Normal file
18
src/modules/contains.rs
Normal file
@@ -0,0 +1,18 @@
|
||||
use pgrx::prelude::*;
|
||||
|
||||
#[pg_extern]
|
||||
pub fn str_contains(input: &str, search: &str) -> bool {
|
||||
input.contains(search)
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
pub fn str_contains_all(input: &str, search: Vec<&str>) -> bool {
|
||||
search.iter().all(|s| input.contains(s))
|
||||
}
|
||||
|
||||
#[cfg(any(test, feature = "pg_test"))]
|
||||
mod tests {
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
use pgrx::prelude::*;
|
||||
}
|
||||
13
src/modules/length.rs
Normal file
13
src/modules/length.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
use pgrx::prelude::*;
|
||||
|
||||
#[pg_extern]
|
||||
fn str_length(input: &str) -> i32 {
|
||||
input.len() as i32
|
||||
}
|
||||
|
||||
#[cfg(any(test, feature = "pg_test"))]
|
||||
mod tests {
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
use pgrx::prelude::*;
|
||||
}
|
||||
24
src/modules/markdown.rs
Normal file
24
src/modules/markdown.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
use pgrx::prelude::*;
|
||||
|
||||
use pulldown_cmark::{html, Options, Parser};
|
||||
|
||||
#[pg_extern]
|
||||
pub fn str_markdown(input: &str) -> String {
|
||||
// Set up options and parser. Strikethroughs are not part of the CommonMark standard
|
||||
// and we therefore must enable it explicitly.
|
||||
let mut options = Options::empty();
|
||||
options.insert(Options::ENABLE_STRIKETHROUGH);
|
||||
let parser = Parser::new_ext(input, options);
|
||||
|
||||
// Write to String buffer.
|
||||
let mut html_output: String = String::with_capacity(input.len() * 3 / 2);
|
||||
html::push_html(&mut html_output, parser);
|
||||
html_output
|
||||
}
|
||||
|
||||
#[cfg(any(test, feature = "pg_test"))]
|
||||
mod tests {
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
use pgrx::prelude::*;
|
||||
}
|
||||
14
src/modules/random.rs
Normal file
14
src/modules/random.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
use pgrx::prelude::*;
|
||||
use rand::distributions::{Alphanumeric, DistString};
|
||||
|
||||
#[pg_extern]
|
||||
pub fn str_random(length: i32) -> String {
|
||||
Alphanumeric.sample_string(&mut rand::thread_rng(), length as usize)
|
||||
}
|
||||
|
||||
#[cfg(any(test, feature = "pg_test"))]
|
||||
mod tests {
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
use pgrx::prelude::*;
|
||||
}
|
||||
18
src/modules/split.rs
Normal file
18
src/modules/split.rs
Normal file
@@ -0,0 +1,18 @@
|
||||
use pgrx::prelude::*;
|
||||
|
||||
#[pg_extern]
|
||||
pub fn str_split<'a>(input: &'a str, pattern: &str) -> Vec<&'a str> {
|
||||
input.split_terminator(pattern).into_iter().collect()
|
||||
}
|
||||
|
||||
#[pg_extern]
|
||||
pub fn str_split_set<'a>(input: &'a str, pattern: &'a str) -> SetOfIterator<'a, &'a str> {
|
||||
SetOfIterator::new(input.split_terminator(pattern).into_iter())
|
||||
}
|
||||
|
||||
#[cfg(any(test, feature = "pg_test"))]
|
||||
mod tests {
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
use pgrx::prelude::*;
|
||||
}
|
||||
29
src/modules/start.rs
Normal file
29
src/modules/start.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use pgrx::prelude::*;
|
||||
use regex::Regex;
|
||||
|
||||
#[pg_extern]
|
||||
pub fn str_start(value: &str, prefix: &str) -> String {
|
||||
let quoted = regex::escape(prefix);
|
||||
let re = Regex::new(&format!("^(?:{})+", quoted)).unwrap();
|
||||
|
||||
format!("{}{}", prefix, re.replace(value, ""))
|
||||
}
|
||||
|
||||
#[cfg(any(test, feature = "pg_test"))]
|
||||
#[pgrx::pg_schema]
|
||||
mod tests {
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
use pgrx::prelude::*;
|
||||
|
||||
#[pg_test]
|
||||
fn test_no_slash_prefix() {
|
||||
let result = Spi::get_one::<String>("SELECT public.str_start('path/to/file', '/')");
|
||||
assert_eq!(result, Ok(Some("/path/to/file".to_string())));
|
||||
}
|
||||
#[pg_test]
|
||||
fn test_slash_prefix() {
|
||||
let result = Spi::get_one::<String>("SELECT public.str_start('/path/to/file', '/')");
|
||||
assert_eq!(result, Ok(Some("/path/to/file".to_string())));
|
||||
}
|
||||
}
|
||||
13
src/modules/substr.rs
Normal file
13
src/modules/substr.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
use pgrx::prelude::*;
|
||||
|
||||
#[pg_extern]
|
||||
pub fn str_substr(input: &str, start: i32, end: i32) -> &str {
|
||||
&input[start as usize..end as usize]
|
||||
}
|
||||
|
||||
#[cfg(any(test, feature = "pg_test"))]
|
||||
mod tests {
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
use pgrx::prelude::*;
|
||||
}
|
||||
14
src/modules/uuid.rs
Normal file
14
src/modules/uuid.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
use pgrx::prelude::*;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[pg_extern]
|
||||
pub fn str_uuid() -> String {
|
||||
Uuid::new_v4().to_string()
|
||||
}
|
||||
|
||||
#[cfg(any(test, feature = "pg_test"))]
|
||||
mod tests {
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
use pgrx::prelude::*;
|
||||
}
|
||||
Reference in New Issue
Block a user