rib/interpreter/
interpreter_result.rs1use crate::interpreter::interpreter_stack_value::RibInterpreterStackValue;
16use crate::{GetLiteralValue, LiteralValue};
17use golem_wasm_ast::analysis::analysed_type::tuple;
18use golem_wasm_ast::analysis::AnalysedType;
19use golem_wasm_rpc::{Value, ValueAndType};
20use std::fmt::{Display, Formatter};
21
22#[derive(Debug, Clone, PartialEq)]
23pub enum RibResult {
24 Unit,
25 Val(ValueAndType),
26}
27
28impl Display for RibResult {
29 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
30 let wasm_wave = match self {
31 RibResult::Unit => ValueAndType::new(Value::Tuple(vec![]), tuple(vec![])).to_string(),
32 RibResult::Val(value_and_type) => value_and_type.to_string(),
33 };
34
35 write!(f, "{}", wasm_wave)
36 }
37}
38
39impl RibResult {
40 pub fn from_rib_interpreter_stack_value(
41 stack_value: &RibInterpreterStackValue,
42 ) -> Option<RibResult> {
43 match stack_value {
44 RibInterpreterStackValue::Unit => Some(RibResult::Unit),
45 RibInterpreterStackValue::Val(value_and_type) => {
46 Some(RibResult::Val(value_and_type.clone()))
47 }
48 RibInterpreterStackValue::Iterator(_) => None,
49 RibInterpreterStackValue::Sink(_, _) => None,
50 }
51 }
52
53 pub fn get_bool(&self) -> Option<bool> {
54 match self {
55 RibResult::Val(ValueAndType {
56 value: Value::Bool(bool),
57 ..
58 }) => Some(*bool),
59 RibResult::Val(_) => None,
60 RibResult::Unit => None,
61 }
62 }
63 pub fn get_val(&self) -> Option<ValueAndType> {
64 match self {
65 RibResult::Val(val) => Some(val.clone()),
66 RibResult::Unit => None,
67 }
68 }
69
70 pub fn get_literal(&self) -> Option<LiteralValue> {
71 self.get_val().and_then(|x| x.get_literal())
72 }
73
74 pub fn get_record(&self) -> Option<Vec<(String, ValueAndType)>> {
75 self.get_val().and_then(|x| match x {
76 ValueAndType {
77 value: Value::Record(field_values),
78 typ: AnalysedType::Record(typ),
79 } => Some(
80 field_values
81 .into_iter()
82 .zip(typ.fields)
83 .map(|(value, typ)| (typ.name, ValueAndType::new(value, typ.typ)))
84 .collect(),
85 ),
86 _ => None,
87 })
88 }
89}