Skip to main content

interstice_abi/interstice_value/convert/
option.rs

1use crate::{IntersticeAbiError, IntersticeValue};
2
3impl<T> Into<IntersticeValue> for Option<T>
4where
5    T: Into<IntersticeValue>,
6{
7    fn into(self) -> IntersticeValue {
8        match self {
9            Some(v) => IntersticeValue::Option(Some(Box::new(v.into()))),
10            None => IntersticeValue::Option(None),
11        }
12    }
13}
14
15impl<T> std::convert::TryFrom<IntersticeValue> for Option<T>
16where
17    T: TryFrom<IntersticeValue, Error = IntersticeAbiError>,
18{
19    type Error = IntersticeAbiError;
20
21    fn try_from(value: IntersticeValue) -> Result<Self, Self::Error> {
22        match value {
23            IntersticeValue::Option(opt) => match opt {
24                Some(inner) => T::try_from(*inner).map(Some),
25                None => Ok(None),
26            },
27            IntersticeValue::Void => Ok(None),
28            other => Err(IntersticeAbiError::ConversionError(format!(
29                "Expected IntersticeValue::Option or IntersticeValue::Void, got {:?}",
30                other
31            ))),
32        }
33    }
34}