toasty_core/stmt/
expr_list.rs

1use super::{Expr, Value};
2
3/// A list of expressions.
4///
5/// Represents an ordered collection of expressions that evaluate to a list of
6/// values.
7///
8/// # Examples
9///
10/// ```text
11/// list(a, b, c)  // a list containing expressions a, b, and c
12/// ```
13#[derive(Debug, Clone, PartialEq)]
14pub struct ExprList {
15    /// The expressions in the list.
16    pub items: Vec<Expr>,
17}
18
19impl Expr {
20    pub fn list<T>(items: impl IntoIterator<Item = T>) -> Self
21    where
22        T: Into<Self>,
23    {
24        ExprList {
25            items: items.into_iter().map(Into::into).collect(),
26        }
27        .into()
28    }
29
30    pub fn list_from_vec(items: Vec<Self>) -> Self {
31        ExprList { items }.into()
32    }
33
34    pub fn is_list(&self) -> bool {
35        matches!(self, Self::List(_) | Self::Value(Value::List(_)))
36    }
37
38    pub fn is_list_empty(&self) -> bool {
39        match self {
40            Self::List(list) => list.items.is_empty(),
41            Self::Value(Value::List(list)) => list.is_empty(),
42            _ => false,
43        }
44    }
45
46    #[track_caller]
47    pub fn expect_list(&self) -> &ExprList {
48        match self {
49            Self::List(list) => list,
50            _ => panic!("expected Expr::List(..) but was {self:#?}"),
51        }
52    }
53
54    #[track_caller]
55    pub fn expect_list_mut(&mut self) -> &mut ExprList {
56        match self {
57            Self::List(list) => list,
58            _ => panic!("expected Expr::List(..) but was {self:#?}"),
59        }
60    }
61
62    #[track_caller]
63    pub fn unwrap_list(self) -> ExprList {
64        match self {
65            Self::List(list) => list,
66            _ => panic!("expected Expr::List(..) but was {self:#?}"),
67        }
68    }
69}
70
71impl ExprList {
72    pub fn is_empty(&self) -> bool {
73        self.items.is_empty()
74    }
75
76    pub fn len(&self) -> usize {
77        self.items.len()
78    }
79}
80
81impl From<ExprList> for Expr {
82    fn from(value: ExprList) -> Self {
83        Self::List(value)
84    }
85}
86
87impl From<Vec<Self>> for Expr {
88    fn from(value: Vec<Self>) -> Self {
89        Self::list_from_vec(value)
90    }
91}