Skip to main content

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    /// Returns `true` if this value is an empty [`Value::List`].
25    pub fn is_list_empty(&self) -> bool {
26        matches!(self, Self::List(items) if items.is_empty())
27    }
28
29    /// Consumes this value and returns the inner `Vec<Value>`, panicking
30    /// if this is not a [`Value::List`].
31    ///
32    /// # Panics
33    ///
34    /// Panics if the value is not a `List` variant.
35    #[track_caller]
36    pub fn into_list_unwrap(self) -> Vec<Value> {
37        match self {
38            Value::List(list) => list,
39            _ => panic!("expected Value::List; actual={self:#?}"),
40        }
41    }
42}
43
44impl From<Vec<Value>> for Value {
45    fn from(value: Vec<Value>) -> Self {
46        Value::List(value)
47    }
48}
49
50impl<T, const N: usize> PartialEq<[T; N]> for Value
51where
52    T: PartialEq<Value>,
53{
54    fn eq(&self, other: &[T; N]) -> bool {
55        match self {
56            Value::List(items) => items.iter().enumerate().all(|(i, item)| other[i].eq(item)),
57            _ => false,
58        }
59    }
60}
61
62impl<T, const N: usize> PartialEq<Value> for [T; N]
63where
64    T: PartialEq<Value>,
65{
66    fn eq(&self, other: &Value) -> bool {
67        other.eq(self)
68    }
69}