Skip to main content

interstice_abi/interstice_value/convert/
primitives.rs

1use crate::{IntersticeAbiError, IntersticeValue};
2
3impl Into<IntersticeValue> for () {
4    fn into(self) -> IntersticeValue {
5        IntersticeValue::Void
6    }
7}
8impl TryInto<()> for IntersticeValue {
9    type Error = IntersticeAbiError;
10
11    fn try_into(self) -> Result<(), Self::Error> {
12        if let IntersticeValue::Void = self {
13            Ok(())
14        } else {
15            Err(IntersticeAbiError::ConversionError(
16                "Expected IntersticeValue::Void".into(),
17            ))
18        }
19    }
20}
21
22impl Into<IntersticeValue> for String {
23    fn into(self) -> IntersticeValue {
24        IntersticeValue::String(self)
25    }
26}
27impl TryFrom<IntersticeValue> for String {
28    type Error = IntersticeAbiError;
29
30    fn try_from(value: IntersticeValue) -> Result<Self, Self::Error> {
31        match value {
32            IntersticeValue::String(s) => Ok(s),
33            _ => Err(IntersticeAbiError::ConversionError(
34                "Expected IntersticeValue::String".into(),
35            )),
36        }
37    }
38}
39
40impl Into<IntersticeValue> for bool {
41    fn into(self) -> IntersticeValue {
42        IntersticeValue::Bool(self)
43    }
44}
45impl TryFrom<IntersticeValue> for bool {
46    type Error = IntersticeAbiError;
47
48    fn try_from(value: IntersticeValue) -> Result<Self, Self::Error> {
49        match value {
50            IntersticeValue::Bool(b) => Ok(b),
51            _ => Err(IntersticeAbiError::ConversionError(
52                "Expected IntersticeValue::Bool".into(),
53            )),
54        }
55    }
56}
57
58macro_rules! impl_to_interstice_value {
59    ($variant:ident, $ty:ty) => {
60        impl Into<IntersticeValue> for $ty {
61            fn into(self) -> IntersticeValue {
62                IntersticeValue::$variant(self)
63            }
64        }
65    };
66}
67
68impl_to_interstice_value!(U32, u32);
69impl_to_interstice_value!(U64, u64);
70impl_to_interstice_value!(I32, i32);
71impl_to_interstice_value!(I64, i64);
72impl_to_interstice_value!(F32, f32);
73impl_to_interstice_value!(F64, f64);
74
75macro_rules! impl_tryfrom_numeric {
76    ($variant:ident, $ty:ty) => {
77        impl TryFrom<IntersticeValue> for $ty {
78            type Error = IntersticeAbiError;
79
80            fn try_from(value: IntersticeValue) -> Result<Self, Self::Error> {
81                match value {
82                    IntersticeValue::$variant(v) => Ok(v),
83                    _ => Err(IntersticeAbiError::ConversionError(format!(
84                        "Expected IntersticeValue::{}",
85                        stringify!($variant)
86                    ))),
87                }
88            }
89        }
90    };
91}
92
93impl_tryfrom_numeric!(U32, u32);
94impl_tryfrom_numeric!(U64, u64);
95impl_tryfrom_numeric!(I32, i32);
96impl_tryfrom_numeric!(I64, i64);
97impl_tryfrom_numeric!(F32, f32);
98impl_tryfrom_numeric!(F64, f64);