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!(U8, u8);
69impl_to_interstice_value!(U32, u32);
70impl_to_interstice_value!(U64, u64);
71impl_to_interstice_value!(I32, i32);
72impl_to_interstice_value!(I64, i64);
73impl_to_interstice_value!(F32, f32);
74impl_to_interstice_value!(F64, f64);
75
76macro_rules! impl_tryfrom_numeric {
77    ($variant:ident, $ty:ty) => {
78        impl TryFrom<IntersticeValue> for $ty {
79            type Error = IntersticeAbiError;
80
81            fn try_from(value: IntersticeValue) -> Result<Self, Self::Error> {
82                match value {
83                    IntersticeValue::$variant(v) => Ok(v),
84                    _ => Err(IntersticeAbiError::ConversionError(format!(
85                        "Expected IntersticeValue::{}",
86                        stringify!($variant)
87                    ))),
88                }
89            }
90        }
91    };
92}
93
94impl_tryfrom_numeric!(U8, u8);
95impl_tryfrom_numeric!(U32, u32);
96impl_tryfrom_numeric!(U64, u64);
97impl_tryfrom_numeric!(I32, i32);
98impl_tryfrom_numeric!(I64, i64);
99impl_tryfrom_numeric!(F32, f32);
100impl_tryfrom_numeric!(F64, f64);