Skip to main content

sim_lib_lang_lua/
value.rs

1use sim_kernel::Value;
2
3/// Result of evaluating a Lua core form.
4#[derive(Clone, Debug)]
5pub enum LuaResult {
6    /// Ordinary expression values.
7    Values(Vec<Value>),
8    /// Values carried by a Lua `return` form.
9    Return(Vec<Value>),
10    /// Non-local exit carried by a Lua `break` form.
11    Break,
12}
13
14impl LuaResult {
15    /// Build an ordinary single-value result.
16    pub fn one(value: Value) -> Self {
17        Self::Values(vec![value])
18    }
19
20    /// Build ordinary expression values.
21    pub fn values(values: Vec<Value>) -> Self {
22        Self::Values(values)
23    }
24
25    /// Build returned values.
26    pub fn return_values(values: Vec<Value>) -> Self {
27        Self::Return(values)
28    }
29
30    /// Build a `break` result.
31    pub fn break_signal() -> Self {
32        Self::Break
33    }
34
35    /// Borrow the contained values.
36    pub fn values_ref(&self) -> &[Value] {
37        match self {
38            Self::Values(values) | Self::Return(values) => values,
39            Self::Break => &[],
40        }
41    }
42
43    /// Return whether this result came from a Lua `return` form.
44    pub fn is_return(&self) -> bool {
45        matches!(self, Self::Return(_))
46    }
47
48    /// Return whether this result came from a Lua `break` form.
49    pub fn is_break(&self) -> bool {
50        matches!(self, Self::Break)
51    }
52
53    /// Consume the result and return its values.
54    pub fn into_values(self) -> Vec<Value> {
55        match self {
56            Self::Values(values) | Self::Return(values) => values,
57            Self::Break => Vec::new(),
58        }
59    }
60}