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