microcad_lang/eval/
format_string.rs

1// Copyright © 2025 The µcad authors <info@ucad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4use crate::eval::*;
5
6impl Eval for FormatExpression {
7    fn eval(&self, context: &mut Context) -> EvalResult<Value> {
8        let value: Value = self.expression.eval(context)?;
9        Ok(Value::String(format!("{value}")))
10    }
11}
12
13impl Eval for FormatString {
14    fn eval(&self, context: &mut Context) -> EvalResult<Value> {
15        let mut result = String::new();
16        for elem in &*self.0 {
17            match elem {
18                FormatStringInner::String(s) => result += &s.value,
19                FormatStringInner::FormatExpression(expr) => match expr.eval(context) {
20                    Ok(Value::String(s)) => result += &s,
21                    Err(e) => return Err(e),
22                    _ => unreachable!("FormatExpression must always evaluate to a string"),
23                },
24            }
25        }
26        Ok(Value::String(result))
27    }
28}