toasty_core/stmt/
value_list.rs

1//! List-related methods and trait implementations for [`Value`].
2
3use crate::stmt::Value;
4
5impl Value {
6    /// Creates a [`Value::List`] from a vector of values.
7    ///
8    /// # Examples
9    ///
10    /// ```
11    /// # use toasty_core::stmt::Value;
12    /// let list = Value::list_from_vec(vec![Value::from(1_i64), Value::from(2_i64)]);
13    /// assert!(list.is_list());
14    /// ```
15    pub fn list_from_vec(items: Vec<Self>) -> Self {
16        Self::List(items)
17    }
18
19    /// Returns `true` if this value is a [`Value::List`].
20    pub fn is_list(&self) -> bool {
21        matches!(self, Self::List(_))
22    }
23
24    /// Consumes this value and returns the inner `Vec<Value>`, panicking
25    /// if this is not a [`Value::List`].
26    ///
27    /// # Panics
28    ///
29    /// Panics if the value is not a `List` variant.
30    #[track_caller]
31    pub fn into_list_unwrap(self) -> Vec<Value> {
32        match self {
33            Value::List(list) => list,
34            _ => panic!("expected Value::List; actual={self:#?}"),
35        }
36    }
37}
38
39impl From<Vec<Value>> for Value {
40    fn from(value: Vec<Value>) -> Self {
41        Value::List(value)
42    }
43}
44
45impl<T, const N: usize> PartialEq<[T; N]> for Value
46where
47    T: PartialEq<Value>,
48{
49    fn eq(&self, other: &[T; N]) -> bool {
50        match self {
51            Value::List(items) => items.iter().enumerate().all(|(i, item)| other[i].eq(item)),
52            _ => false,
53        }
54    }
55}
56
57impl<T, const N: usize> PartialEq<Value> for [T; N]
58where
59    T: PartialEq<Value>,
60{
61    fn eq(&self, other: &Value) -> bool {
62        other.eq(self)
63    }
64}