teo_runtime/value/convert/into/
tuple.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use teo_result::Error;
use crate::value::Value;

impl<T0, T1> TryFrom<Value> for (T0, T1) where T0: TryFrom<Value, Error = Error>, T1: TryFrom<Value, Error = Error> {

    type Error = Error;

    fn try_from(ref value: Value) -> Result<Self, Self::Error> {
        match value {
            Value::Tuple(values) => {
                if values.len() != 2 {
                    Err(Error::new(format!("Cannot convert {} into Tuple", value.type_hint())))?
                }
                Ok((
                    values.get(0).unwrap().clone().try_into()?,
                    values.get(1).unwrap().clone().try_into()?,
                ))
            }
            _ => Err(Error::new(format!("Cannot convert {} into Tuple", value.type_hint()))),
        }
    }
}

impl<T0, T1, T2> TryFrom<Value> for (T0, T1, T2) where T0: TryFrom<Value, Error = Error>, T1: TryFrom<Value, Error = Error>, T2: TryFrom<Value, Error = Error> {

    type Error = Error;

    fn try_from(ref value: Value) -> Result<Self, Self::Error> {
        match value {
            Value::Tuple(values) => Ok((
                values.get(0).map(Clone::clone).ok_or(Err(Error::new(format!("Cannot convert {} into Tuple", value.type_hint())))?).unwrap().try_into()?,
                values.get(1).map(Clone::clone).ok_or(Err(Error::new(format!("Cannot convert {} into Tuple", value.type_hint())))?).unwrap().try_into()?,
                values.get(2).map(Clone::clone).ok_or(Err(Error::new(format!("Cannot convert {} into Tuple", value.type_hint())))?).unwrap().try_into()?,
            )),
            _ => Err(Error::new(format!("Cannot convert {} into Tuple", value.type_hint()))),
        }
    }
}