2022-01-13 17:37:46 +00:00
|
|
|
use crate::err::Error;
|
2022-07-04 09:41:18 +00:00
|
|
|
use crate::fnc::util::string;
|
2022-01-13 17:37:46 +00:00
|
|
|
use crate::sql::value::Value;
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn concat(args: Vec<Value>) -> Result<Value, Error> {
|
2022-05-05 04:30:32 +00:00
|
|
|
Ok(args.into_iter().map(|x| x.as_string()).collect::<Vec<_>>().concat().into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn ends_with((val, chr): (String, String)) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
Ok(val.ends_with(&chr).into())
|
|
|
|
}
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn join(args: Vec<Value>) -> Result<Value, Error> {
|
2022-08-29 01:47:33 +00:00
|
|
|
let mut args = args.into_iter().map(Value::as_string);
|
2022-09-22 23:54:53 +00:00
|
|
|
let chr = args.next().ok_or_else(|| Error::InvalidArguments {
|
2022-09-21 00:57:33 +00:00
|
|
|
name: String::from("string::join"),
|
|
|
|
message: String::from("Expected at least one argument"),
|
|
|
|
})?;
|
2022-08-29 01:47:33 +00:00
|
|
|
// FIXME: Use intersperse to avoid intermediate allocation once stable
|
|
|
|
// https://github.com/rust-lang/rust/issues/79524
|
|
|
|
let val = args.collect::<Vec<_>>().join(&chr);
|
2022-01-13 17:37:46 +00:00
|
|
|
Ok(val.into())
|
|
|
|
}
|
|
|
|
|
2023-02-18 21:07:37 +00:00
|
|
|
pub fn len((string,): (String,)) -> Result<Value, Error> {
|
2022-09-21 00:57:33 +00:00
|
|
|
let num = string.chars().count() as i64;
|
2022-01-13 17:37:46 +00:00
|
|
|
Ok(num.into())
|
|
|
|
}
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn lowercase((string,): (String,)) -> Result<Value, Error> {
|
|
|
|
Ok(string.to_lowercase().into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn repeat((val, num): (String, usize)) -> Result<Value, Error> {
|
2022-09-02 15:19:01 +00:00
|
|
|
const LIMIT: usize = 2usize.pow(20);
|
|
|
|
if val.len().saturating_mul(num) > LIMIT {
|
|
|
|
Err(Error::InvalidArguments {
|
|
|
|
name: String::from("string::repeat"),
|
2023-02-03 11:47:07 +00:00
|
|
|
message: format!("Output must not exceed {LIMIT} bytes."),
|
2022-09-02 15:19:01 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Ok(val.repeat(num).into())
|
|
|
|
}
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn replace((val, old, new): (String, String, String)) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
Ok(val.replace(&old, &new).into())
|
|
|
|
}
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn reverse((string,): (String,)) -> Result<Value, Error> {
|
|
|
|
Ok(string.chars().rev().collect::<String>().into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-11-27 11:05:33 +00:00
|
|
|
pub fn slice((val, beg, lim): (String, Option<isize>, Option<isize>)) -> Result<Value, Error> {
|
|
|
|
let val = match beg {
|
2022-12-03 23:30:38 +00:00
|
|
|
Some(v) if v < 0 => {
|
|
|
|
val.chars().skip(val.len().saturating_sub(v.unsigned_abs())).collect::<String>()
|
|
|
|
}
|
2022-11-27 11:05:33 +00:00
|
|
|
Some(v) => val.chars().skip(v as usize).collect::<String>(),
|
|
|
|
None => val,
|
|
|
|
};
|
|
|
|
let val = match lim {
|
2022-12-03 23:30:38 +00:00
|
|
|
Some(v) if v < 0 => {
|
|
|
|
val.chars().take(val.len().saturating_sub(v.unsigned_abs())).collect::<String>()
|
|
|
|
}
|
2022-11-27 11:05:33 +00:00
|
|
|
Some(v) => val.chars().take(v as usize).collect::<String>(),
|
|
|
|
None => val,
|
|
|
|
};
|
|
|
|
Ok(val.into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn slug((string,): (String,)) -> Result<Value, Error> {
|
2022-12-20 10:30:40 +00:00
|
|
|
Ok(string::slug(string).into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn split((val, chr): (String, String)) -> Result<Value, Error> {
|
|
|
|
Ok(val.split(&chr).collect::<Vec<&str>>().into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn starts_with((val, chr): (String, String)) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
Ok(val.starts_with(&chr).into())
|
|
|
|
}
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn trim((string,): (String,)) -> Result<Value, Error> {
|
|
|
|
Ok(string.trim().into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn uppercase((string,): (String,)) -> Result<Value, Error> {
|
|
|
|
Ok(string.to_uppercase().into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 00:57:33 +00:00
|
|
|
pub fn words((string,): (String,)) -> Result<Value, Error> {
|
|
|
|
Ok(string.split_whitespace().collect::<Vec<&str>>().into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|