soroban_env_common/
result.rs

1use crate::{Env, Error, TryFromVal, TryIntoVal, Val};
2
3impl<E: Env, T, R> TryFromVal<E, Val> for Result<T, R>
4where
5    T: TryFromVal<E, Val>,
6    R: TryFrom<Error>,
7    <R as TryFrom<Error>>::Error: Into<Error>,
8{
9    type Error = crate::Error;
10
11    #[inline(always)]
12    fn try_from_val(env: &E, val: &Val) -> Result<Self, Self::Error> {
13        let val = *val;
14        if let Ok(error) = Error::try_from_val(env, &val) {
15            match R::try_from(error) {
16                Ok(err) => Ok(Err(err)),
17                Err(err) => Err(err.into()),
18            }
19        } else {
20            let converted = T::try_from_val(env, &val).map_err(Into::into)?;
21            Ok(Ok(converted))
22        }
23    }
24}
25
26impl<E: Env, T, R> TryFromVal<E, Result<T, R>> for Val
27where
28    Val: TryFromVal<E, T>,
29    for<'a> &'a R: TryInto<Error>,
30    for<'a> <&'a R as TryInto<Error>>::Error: Into<Error>,
31{
32    type Error = crate::Error;
33
34    #[inline(always)]
35    fn try_from_val(env: &E, v: &Result<T, R>) -> Result<Self, Self::Error> {
36        match v {
37            Ok(t) => t.try_into_val(env).map_err(Into::into),
38            Err(r) => {
39                let error: Error = r.try_into().map_err(Into::into)?;
40                Ok(error.into())
41            }
42        }
43    }
44}