2022-05-14 12:35:08 +00:00
|
|
|
use crate::ctx::Context;
|
2022-01-13 17:37:46 +00:00
|
|
|
use crate::err::Error;
|
|
|
|
use crate::sql::value::Value;
|
|
|
|
use slug::slugify;
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn concat(_: &Context, 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-05-14 12:35:08 +00:00
|
|
|
pub fn ends_with(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-05-05 04:30:32 +00:00
|
|
|
let val = args.remove(0).as_string();
|
|
|
|
let chr = args.remove(0).as_string();
|
2022-01-13 17:37:46 +00:00
|
|
|
Ok(val.ends_with(&chr).into())
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn join(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-05-05 04:30:32 +00:00
|
|
|
let chr = args.remove(0).as_string();
|
|
|
|
let val = args.into_iter().map(|x| x.as_string());
|
2022-01-13 17:37:46 +00:00
|
|
|
let val = val.collect::<Vec<_>>().join(&chr);
|
|
|
|
Ok(val.into())
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn length(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-05-05 04:30:32 +00:00
|
|
|
let val = args.remove(0).as_string();
|
2022-01-13 17:37:46 +00:00
|
|
|
let num = val.chars().count() as i64;
|
|
|
|
Ok(num.into())
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn lowercase(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-05-05 04:30:32 +00:00
|
|
|
Ok(args.remove(0).as_string().to_lowercase().into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn repeat(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-05-05 04:30:32 +00:00
|
|
|
let val = args.remove(0).as_string();
|
2022-01-13 17:37:46 +00:00
|
|
|
let num = args.remove(0).as_int() as usize;
|
|
|
|
Ok(val.repeat(num).into())
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn replace(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-05-05 04:30:32 +00:00
|
|
|
let val = args.remove(0).as_string();
|
|
|
|
let old = args.remove(0).as_string();
|
|
|
|
let new = args.remove(0).as_string();
|
2022-01-13 17:37:46 +00:00
|
|
|
Ok(val.replace(&old, &new).into())
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn reverse(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-05-05 04:30:32 +00:00
|
|
|
Ok(args.remove(0).as_string().chars().rev().collect::<String>().into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn slice(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-05-05 04:30:32 +00:00
|
|
|
let val = args.remove(0).as_string();
|
2022-01-13 17:37:46 +00:00
|
|
|
let beg = args.remove(0).as_int() as usize;
|
|
|
|
let lim = args.remove(0).as_int() as usize;
|
|
|
|
let val = val.chars().skip(beg).take(lim).collect::<String>();
|
|
|
|
Ok(val.into())
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn slug(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-05-05 04:30:32 +00:00
|
|
|
Ok(slugify(&args.remove(0).as_string()).into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn split(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-05-05 04:30:32 +00:00
|
|
|
let val = args.remove(0).as_string();
|
|
|
|
let chr = args.remove(0).as_string();
|
2022-01-13 17:37:46 +00:00
|
|
|
let val = val.split(&chr).collect::<Vec<&str>>();
|
|
|
|
Ok(val.into())
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn starts_with(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-05-05 04:30:32 +00:00
|
|
|
let val = args.remove(0).as_string();
|
|
|
|
let chr = args.remove(0).as_string();
|
2022-01-13 17:37:46 +00:00
|
|
|
Ok(val.starts_with(&chr).into())
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn trim(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-05-05 04:30:32 +00:00
|
|
|
Ok(args.remove(0).as_string().trim().into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn uppercase(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-05-05 04:30:32 +00:00
|
|
|
Ok(args.remove(0).as_string().to_uppercase().into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn words(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-05-05 04:30:32 +00:00
|
|
|
Ok(args.remove(0).as_string().split(' ').collect::<Vec<&str>>().into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|