Skip to main content

rl_lang/interpreter/stdlib/string/
join.rs

1use crate::{
2    interpreter::{evaluator::Evaluator, values::Value},
3    utils::{errors::Error, span::Span},
4};
5
6pub fn std_join(
7    eval: &mut Evaluator,
8    strings_array: Value,
9    delim: String,
10    span: Span,
11) -> Result<Value, Error> {
12    match strings_array {
13        Value::Values { items: array, .. } => {
14            let mut strings: Vec<String> = vec![];
15            for v in array {
16                match v {
17                    Value::Integer(i) => strings.push(format!("{}", i)),
18                    Value::Float(f) => strings.push(format!("{}", f)),
19                    Value::Bool(b) => strings.push(format!("{}", b)),
20                    Value::String(s) => strings.push(s),
21                    Value::Char(c) => strings.push(c.to_string()),
22                    Value::Null => strings.push("null".to_string()),
23                    Value::Function { .. } => {
24                        return Err(eval.err(
25                            "functions/lambdas/enclosures are not supported via join()".to_string(),
26                            span,
27                        ));
28                    }
29                    _ => {}
30                }
31            }
32            Ok(Value::String(strings.join(&delim)))
33        }
34        _ => Err(eval.err(
35            "join() expects an array as first argument".to_string(),
36            span,
37        )),
38    }
39}