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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use super::{Expr, Value};
/// A list of expressions.
///
/// Represents an ordered collection of expressions that evaluate to a list of
/// values.
///
/// # Examples
///
/// ```text
/// list(a, b, c) // a list containing expressions a, b, and c
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct ExprList {
/// The expressions in the list.
pub items: Vec<Expr>,
}
impl Expr {
/// Creates a list expression from an iterator of items convertible to [`Expr`].
pub fn list<T>(items: impl IntoIterator<Item = T>) -> Self
where
T: Into<Self>,
{
ExprList {
items: items.into_iter().map(Into::into).collect(),
}
.into()
}
/// Creates a list expression from a pre-built vector of expressions.
pub fn list_from_vec(items: Vec<Self>) -> Self {
ExprList { items }.into()
}
/// Returns `true` if this expression is a list (either `Expr::List` or
/// `Expr::Value(Value::List(...))`).
pub fn is_list(&self) -> bool {
matches!(self, Self::List(_) | Self::Value(Value::List(_)))
}
/// Returns `true` if this expression is an empty list.
pub fn is_list_empty(&self) -> bool {
match self {
Self::List(list) => list.items.is_empty(),
Self::Value(Value::List(list)) => list.is_empty(),
_ => false,
}
}
/// Returns a reference to the inner [`ExprList`].
///
/// # Panics
///
/// Panics if `self` is not `Expr::List`.
#[track_caller]
pub fn as_list_unwrap(&self) -> &ExprList {
match self {
Self::List(list) => list,
_ => panic!("expected Expr::List(..) but was {self:#?}"),
}
}
/// Returns a mutable reference to the inner [`ExprList`].
///
/// # Panics
///
/// Panics if `self` is not `Expr::List`.
#[track_caller]
pub fn as_list_mut_unwrap(&mut self) -> &mut ExprList {
match self {
Self::List(list) => list,
_ => panic!("expected Expr::List(..) but was {self:#?}"),
}
}
/// Consumes the expression, returning `Some(ExprList)` if it is a list,
/// or `None` otherwise.
pub fn into_list(self) -> Option<ExprList> {
match self {
Self::List(list) => Some(list),
_ => None,
}
}
}
impl ExprList {
/// Returns `true` if the list contains no expressions.
pub fn is_empty(&self) -> bool {
self.items.is_empty()
}
/// Returns the number of expressions in the list.
pub fn len(&self) -> usize {
self.items.len()
}
}
impl From<ExprList> for Expr {
fn from(value: ExprList) -> Self {
Self::List(value)
}
}
impl From<Vec<Self>> for Expr {
fn from(value: Vec<Self>) -> Self {
Self::list_from_vec(value)
}
}