libhaystack/haystack/val/
list.rs

1// Copyright (C) 2020 - 2022, J2 Innovations
2
3//! Haystack List
4
5use crate::haystack::val::Value;
6use std::vec::Vec;
7
8/// A Haystack List of `Value`s
9///
10/// # Example
11/// Create `List`
12/// ```
13/// use libhaystack::val::*;
14///
15/// let list_value = Value::from(vec![Value::from(true), Value::from("A string")]);
16/// assert!(list_value.is_list());
17/// assert_eq!(List::try_from(&list_value).unwrap().len(), 2);
18///```
19///
20pub type List = Vec<Value>;
21
22/// Converts from `List` to a `List` `Value`
23impl From<List> for Value {
24    fn from(value: List) -> Self {
25        Value::List(value)
26    }
27}
28
29/// Tries to convert from `Value` to a `List`
30impl TryFrom<&Value> for List {
31    type Error = &'static str;
32    fn try_from(value: &Value) -> Result<Self, Self::Error> {
33        match value {
34            Value::List(v) => Ok(v.clone()),
35            _ => Err("Value is not an `List`"),
36        }
37    }
38}