wasm_wave/wasm/
fmt.rs

1use core::fmt::Display;
2
3use alloc::{string::ToString, vec::Vec};
4
5use crate::{
6    WasmValue,
7    wasm::{WasmFunc, WasmType, WasmTypeKind},
8    writer::Writer,
9};
10
11/// Implements a WAVE-formatted [`Display`] for a [`WasmType`].
12pub struct DisplayType<'a, T: WasmType>(pub &'a T);
13
14impl<T: WasmType> Display for DisplayType<'_, T> {
15    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
16        let ty = self.0;
17        match ty.kind() {
18            WasmTypeKind::List => {
19                write!(f, "list<{}>", DisplayType(&ty.list_element_type().unwrap()))
20            }
21            WasmTypeKind::Record => {
22                f.write_str("record { ")?;
23                for (idx, (name, field_type)) in ty.record_fields().enumerate() {
24                    if idx != 0 {
25                        f.write_str(", ")?;
26                    }
27                    write!(f, "{name}: {}", DisplayType(&field_type))?;
28                }
29                f.write_str(" }")
30            }
31            WasmTypeKind::Tuple => {
32                f.write_str("tuple<")?;
33                for (idx, ty) in ty.tuple_element_types().enumerate() {
34                    if idx != 0 {
35                        f.write_str(", ")?;
36                    }
37                    write!(f, "{}", DisplayType(&ty))?;
38                }
39                f.write_str(">")
40            }
41            WasmTypeKind::Variant => {
42                f.write_str("variant { ")?;
43                for (idx, (name, payload)) in ty.variant_cases().enumerate() {
44                    if idx != 0 {
45                        f.write_str(", ")?;
46                    }
47                    f.write_str(name.as_ref())?;
48                    if let Some(ty) = payload {
49                        write!(f, "({})", DisplayType(&ty))?;
50                    }
51                }
52                f.write_str(" }")
53            }
54            WasmTypeKind::Enum => {
55                f.write_str("enum { ")?;
56                for (idx, name) in ty.enum_cases().enumerate() {
57                    if idx != 0 {
58                        f.write_str(", ")?;
59                    }
60                    f.write_str(name.as_ref())?;
61                }
62                f.write_str(" }")
63            }
64            WasmTypeKind::Option => {
65                write!(
66                    f,
67                    "option<{}>",
68                    DisplayType(&ty.option_some_type().unwrap())
69                )
70            }
71            WasmTypeKind::Result => {
72                f.write_str("result")?;
73                match ty.result_types().unwrap() {
74                    (None, None) => Ok(()),
75                    (None, Some(err)) => write!(f, "<_, {}>", DisplayType(&err)),
76                    (Some(ok), None) => write!(f, "<{}>", DisplayType(&ok)),
77                    (Some(ok), Some(err)) => {
78                        write!(f, "<{}, {}>", DisplayType(&ok), DisplayType(&err))
79                    }
80                }
81            }
82            WasmTypeKind::Flags => {
83                f.write_str("flags { ")?;
84                for (idx, name) in ty.flags_names().enumerate() {
85                    if idx != 0 {
86                        f.write_str(", ")?;
87                    }
88                    f.write_str(name.as_ref())?;
89                }
90                f.write_str(" }")
91            }
92            simple => Display::fmt(&simple, f),
93        }
94    }
95}
96
97/// Implements a WAVE-formatted [`Display`] for a [`WasmValue`].
98pub struct DisplayValue<'a, T: WasmValue>(pub &'a T);
99
100impl<T: WasmValue> Display for DisplayValue<'_, T> {
101    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
102        Writer::new(f)
103            .write_value(self.0)
104            .map_err(|_| core::fmt::Error)
105    }
106}
107
108/// Implements a WAVE-formatted [`Display`] for a [`WasmFunc`].
109pub struct DisplayFunc<T: WasmFunc>(pub T);
110
111impl<T: WasmFunc> Display for DisplayFunc<T> {
112    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
113        f.write_str("func(")?;
114        let mut param_names = self.0.param_names();
115        for (idx, ty) in self.0.params().enumerate() {
116            if idx != 0 {
117                f.write_str(", ")?;
118            }
119            if let Some(name) = param_names.next() {
120                write!(f, "{name}: ")?;
121            }
122            DisplayType(&ty).fmt(f)?
123        }
124        f.write_str(")")?;
125
126        let results = self.0.results().collect::<Vec<_>>();
127        if results.is_empty() {
128            return Ok(());
129        }
130
131        let mut result_names = self.0.result_names();
132        if results.len() == 1 {
133            let ty = DisplayType(&results.into_iter().next().unwrap()).to_string();
134            if let Some(name) = result_names.next() {
135                write!(f, " -> ({name}: {ty})")
136            } else {
137                write!(f, " -> {ty}")
138            }
139        } else {
140            f.write_str(" -> (")?;
141            for (idx, ty) in results.into_iter().enumerate() {
142                if idx != 0 {
143                    f.write_str(", ")?;
144                }
145                if let Some(name) = result_names.next() {
146                    write!(f, "{name}: ")?;
147                }
148                DisplayType(&ty).fmt(f)?;
149            }
150            f.write_str(")")
151        }
152    }
153}
154
155/// Implements a WAVE-formatted [`Display`] for [`WasmValue`] func arguments.
156pub struct DisplayFuncArgs<'a, T: WasmValue>(pub &'a [T]);
157
158impl<T: WasmValue> Display for DisplayFuncArgs<'_, T> {
159    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
160        f.write_str("(")?;
161        for (idx, v) in self.0.iter().enumerate() {
162            if idx != 0 {
163                f.write_str(", ")?;
164            }
165            DisplayValue(v).fmt(f)?;
166        }
167        f.write_str(")")
168    }
169}
170
171/// Implements a WAVE-formatted [`Display`] for [`WasmValue`] func results.
172pub struct DisplayFuncResults<'a, T: WasmValue>(pub &'a [T]);
173
174impl<T: WasmValue> Display for DisplayFuncResults<'_, T> {
175    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
176        if self.0.len() == 1 {
177            DisplayValue(&self.0[0]).fmt(f)
178        } else {
179            DisplayFuncArgs(self.0).fmt(f)
180        }
181    }
182}
183
184#[cfg(test)]
185mod tests {
186    use alloc::string::ToString;
187
188    use crate::value::Type;
189
190    #[test]
191    fn test_type_display() {
192        for (ty, expected) in [
193            (Type::U8, "u8"),
194            (Type::list(Type::U8), "list<u8>"),
195            (
196                Type::record([("a", Type::U8), ("b", Type::STRING)]).unwrap(),
197                "record { a: u8, b: string }",
198            ),
199            (
200                Type::tuple([Type::U8, Type::BOOL]).unwrap(),
201                "tuple<u8, bool>",
202            ),
203            (
204                Type::variant([("off", None), ("on", Some(Type::U8))]).unwrap(),
205                "variant { off, on(u8) }",
206            ),
207            (
208                Type::enum_ty(["east", "west"]).unwrap(),
209                "enum { east, west }",
210            ),
211            (Type::option(Type::U8), "option<u8>"),
212            (Type::result(None, None), "result"),
213            (Type::result(Some(Type::U8), None), "result<u8>"),
214            (Type::result(None, Some(Type::STRING)), "result<_, string>"),
215            (
216                Type::result(Some(Type::U8), Some(Type::STRING)),
217                "result<u8, string>",
218            ),
219            (
220                Type::flags(["read", "write"]).unwrap(),
221                "flags { read, write }",
222            ),
223        ] {
224            assert_eq!(ty.to_string(), expected);
225        }
226    }
227}