wasmtime_interface_types/
value.rs1use std::convert::TryFrom;
2use std::fmt;
3
4#[derive(Debug, Clone)]
6#[allow(missing_docs)]
7pub enum Value {
8 String(String),
9 I32(i32),
10 U32(u32),
11 I64(i64),
12 U64(u64),
13 F32(f32),
14 F64(f64),
15}
16
17macro_rules! from {
18 ($($a:ident => $b:ident,)*) => ($(
19 impl From<$a> for Value {
20 fn from(val: $a) -> Value {
21 Value::$b(val)
22 }
23 }
24
25 impl TryFrom<Value> for $a {
26 type Error = anyhow::Error;
27
28 fn try_from(val: Value) -> Result<$a, Self::Error> {
29 match val {
30 Value::$b(v) => Ok(v),
31 v => anyhow::bail!("cannot convert {:?} to {}", v, stringify!($a)),
32 }
33 }
34 }
35 )*)
36}
37
38from! {
39 String => String,
40 i32 => I32,
41 u32 => U32,
42 i64 => I64,
43 u64 => U64,
44 f32 => F32,
45 f64 => F64,
46}
47
48impl<'a> From<&'a str> for Value {
49 fn from(x: &'a str) -> Value {
50 x.to_string().into()
51 }
52}
53
54impl fmt::Display for Value {
55 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
56 match self {
57 Value::String(s) => s.fmt(f),
58 Value::I32(s) => s.fmt(f),
59 Value::U32(s) => s.fmt(f),
60 Value::I64(s) => s.fmt(f),
61 Value::U64(s) => s.fmt(f),
62 Value::F32(s) => s.fmt(f),
63 Value::F64(s) => s.fmt(f),
64 }
65 }
66}