Skip to main content

rl_lang/interpreter/stdlib/result/
result_map.rs

1use crate::{
2    interpreter::{evaluator::Evaluator, stdlib::common::check_arity, values::Value},
3    utils::{errors::Error, span::Span},
4};
5
6pub fn std_result_map(eval: &mut Evaluator, args: Vec<Value>, span: Span) -> Result<Value, Error> {
7    check_arity(&args, 2, "result_map", span)?;
8    match args[0].clone() {
9        Value::Ok(inner) => {
10            let mapped = eval.call_value(args[1].clone(), vec![*inner], span)?;
11            Ok(Value::Ok(Box::new(mapped)))
12        }
13        // pass error as is
14        Value::Err(_) => Ok(args[0].clone()),
15        other => Err(eval.err(
16            format!("result_map: expected result, got {}", other.type_name()),
17            span,
18        )),
19    }
20}
21
22pub fn std_result_map_err(
23    eval: &mut Evaluator,
24    args: Vec<Value>,
25    span: Span,
26) -> Result<Value, Error> {
27    check_arity(&args, 2, "result_map_err", span)?;
28    match args[0].clone() {
29        Value::Err(inner) => {
30            let mapped = eval.call_value(args[1].clone(), vec![*inner], span)?;
31            Ok(Value::Err(Box::new(mapped)))
32        }
33        // pass ok as is
34        Value::Ok(_) => Ok(args[0].clone()),
35        other => Err(eval.err(
36            format!("result_map_err: expected result, got {}", other.type_name()),
37            span,
38        )),
39    }
40}