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
use std::convert::{TryFrom, TryInto};

use failure::Error;

use crate::bolt::value::List;
use crate::error::ValueError;
use crate::Value;

impl<T> TryInto<Vec<T>> for List
where
    T: TryFrom<Value, Error = Error>,
{
    type Error = Error;

    fn try_into(self) -> Result<Vec<T>, Self::Error> {
        let mut vec: Vec<T> = Vec::with_capacity(self.value.len());
        for value in self.value {
            vec.push(T::try_from(value)?);
        }
        Ok(vec)
    }
}

impl<T> TryInto<Vec<T>> for Value
where
    T: TryFrom<Value, Error = Error>,
{
    type Error = Error;

    fn try_into(self) -> Result<Vec<T>, Self::Error> {
        match self {
            Value::List(list) => Ok(list.try_into()?),
            _ => Err(ValueError::InvalidConversion(self).into()),
        }
    }
}