rl_lang/interpreter/stdlib/result/
result_unwrap.rs1use crate::{
2 interpreter::{evaluator::Evaluator, stdlib::common::check_arity, values::Value},
3 utils::{errors::Error, span::Span},
4};
5
6pub fn std_unwrap(eval: &mut Evaluator, args: Vec<Value>, span: Span) -> Result<Value, Error> {
7 check_arity(&args, 1, "result_unwrap", span)?;
8 match &args[0] {
9 Value::Ok(inner) => Ok(*inner.clone()),
10 Value::Err(v) => Err(eval.err(format!("result_unwrap: called on Err({})", v), span)),
11 other => Err(eval.err(
12 format!("result_unwrap: expected result, got {}", other.type_name()),
13 span,
14 )),
15 }
16}
17
18pub fn std_unwrap_err(eval: &mut Evaluator, args: Vec<Value>, span: Span) -> Result<Value, Error> {
19 check_arity(&args, 1, "result_unwrap_err", span)?;
20 match &args[0] {
21 Value::Err(inner) => Ok(*inner.clone()),
22 Value::Ok(v) => Err(eval.err(format!("result_unwrap_err: called on ok({})", v), span)),
23 other => Err(eval.err(
24 format!(
25 "result_unwrap_err: expected result, got {}",
26 other.type_name()
27 ),
28 span,
29 )),
30 }
31}
32
33pub fn std_unwrap_or(eval: &mut Evaluator, args: Vec<Value>, span: Span) -> Result<Value, Error> {
34 check_arity(&args, 2, "result_unwrap_or", span)?;
35 match &args[0] {
36 Value::Ok(inner) => Ok(*inner.clone()),
37 Value::Err(_) => Ok(args[1].clone()),
38 other => Err(eval.err(
39 format!(
40 "result_unwrap_or: expected result, got {}",
41 other.type_name()
42 ),
43 span,
44 )),
45 }
46}