use crate::compiler::prelude::*;
fn join(array: Value, separator: Option<Value>) -> Resolved {
let array = array.try_array()?;
let string_vec = array
.iter()
.map(|s| s.to_str_lossy().ok_or(()))
.collect::<Result<Vec<_>, _>>()
.map_err(|()| "all array items must be strings")?;
let separator = match separator {
None => String::new(),
Some(value) => value.try_bytes_utf8_lossy()?.into_owned(),
};
let joined = string_vec.join(&separator);
Ok(Value::from(joined))
}
#[derive(Clone, Copy, Debug)]
pub struct Join;
impl Function for Join {
fn identifier(&self) -> &'static str {
"join"
}
fn usage(&self) -> &'static str {
"Joins each string in the `value` array into a single string, with items optionally separated from one another by a `separator`."
}
fn category(&self) -> &'static str {
Category::String.as_ref()
}
fn return_kind(&self) -> u16 {
kind::BYTES
}
fn parameters(&self) -> &'static [Parameter] {
const PARAMETERS: &[Parameter] = &[
Parameter::required(
"value",
kind::ARRAY,
"The array of strings to join together.",
)
.with_element_kind(kind::BYTES),
Parameter::optional(
"separator",
kind::BYTES,
"The string separating each original element when joined.",
),
];
PARAMETERS
}
fn compile(
&self,
_state: &state::TypeState,
_ctx: &mut FunctionCompileContext,
arguments: ArgumentList,
) -> Compiled {
let value = arguments.required("value");
let separator = arguments.optional("separator");
Ok(JoinFn { value, separator }.as_expr())
}
fn examples(&self) -> &'static [Example] {
&[
example! {
title: "Join array (no separator)",
source: r#"join(["bring", "us", "together"])"#,
result: Ok("bringustogether"),
},
example! {
title: "Join array (comma separator)",
source: r#"join(["sources", "transforms", "sinks"], separator: ", ")"#,
result: Ok("sources, transforms, sinks"),
},
]
}
}
#[derive(Clone, Debug)]
struct JoinFn {
value: Box<dyn Expression>,
separator: Option<Box<dyn Expression>>,
}
impl FunctionExpression for JoinFn {
fn resolve(&self, ctx: &mut Context) -> Resolved {
let array = self.value.resolve(ctx)?;
let separator = self
.separator
.as_ref()
.map(|s| s.resolve(ctx))
.transpose()?;
join(array, separator)
}
fn type_def(&self, _: &state::TypeState) -> TypeDef {
TypeDef::bytes()
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::value;
test_function![
join => Join;
with_comma_separator {
args: func_args![value: value!(["one", "two", "three"]), separator: ", "],
want: Ok(value!("one, two, three")),
tdef: TypeDef::bytes(),
}
with_space_separator {
args: func_args![value: value!(["one", "two", "three"]), separator: " "],
want: Ok(value!("one two three")),
tdef: TypeDef::bytes(),
}
without_separator {
args: func_args![value: value!(["one", "two", "three"])],
want: Ok(value!("onetwothree")),
tdef: TypeDef::bytes(),
}
non_string_array_item_throws_error {
args: func_args![value: value!(["one", "two", 3])],
want: Err("all array items must be strings"),
tdef: TypeDef::bytes(),
}
];
}