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
use super::{Expr, Value};
/// A mutable reference to either an [`Expr`] or a [`Value`] within a
/// composite structure.
///
/// This is the mutable counterpart to [`Entry`](super::Entry), used for
/// in-place modification of nested expressions or values.
///
/// # Examples
///
/// ```ignore
/// use toasty_core::stmt::{EntryMut, Expr, Value};
///
/// let mut expr = Expr::from(Value::from(42_i64));
/// let mut entry = EntryMut::from(&mut expr);
/// assert!(matches!(entry, EntryMut::Expr(_)));
/// ```
#[derive(Debug)]
pub enum EntryMut<'a> {
/// A mutable reference to an expression.
Expr(&'a mut Expr),
/// A mutable reference to a value.
Value(&'a mut Value),
}
impl EntryMut<'_> {
/// Returns `true` if this entry holds a concrete value.
pub fn is_value(&self) -> bool {
matches!(self, EntryMut::Value(_) | EntryMut::Expr(Expr::Value(_)))
}
/// Returns `true` if this entry holds a null value.
pub fn is_value_null(&self) -> bool {
matches!(
self,
EntryMut::Value(Value::Null) | EntryMut::Expr(Expr::Value(Value::Null))
)
}
/// Returns `true` if this entry holds a record, either as an
/// `Expr::Record`, an `Expr::Value(Value::Record)`, or a bare
/// `Value::Record`.
pub fn is_record(&self) -> bool {
match self {
EntryMut::Expr(Expr::Record(_)) => true,
EntryMut::Expr(Expr::Value(value)) => value.is_record(),
EntryMut::Value(value) => value.is_record(),
EntryMut::Expr(_) => false,
}
}
/// Returns `true` if this entry is `Expr::Default`.
pub fn is_default(&self) -> bool {
matches!(self, EntryMut::Expr(Expr::Default))
}
/// Takes the contained expression or value, replacing it with a default.
pub fn take(&mut self) -> Expr {
match self {
EntryMut::Expr(expr) => expr.take(),
EntryMut::Value(value) => value.take().into(),
}
}
/// Replaces the contents of this entry with `expr`.
///
/// # Panics
///
/// Panics if this is a `Value` entry and `expr` is not `Expr::Value`.
pub fn insert(&mut self, expr: Expr) {
match self {
EntryMut::Expr(e) => **e = expr,
EntryMut::Value(e) => match expr {
Expr::Value(value) => **e = value,
_ => panic!("cannot store expression in value entry"),
},
}
}
}
impl<'a> From<&'a mut Expr> for EntryMut<'a> {
fn from(value: &'a mut Expr) -> Self {
EntryMut::Expr(value)
}
}
impl<'a> From<&'a mut Value> for EntryMut<'a> {
fn from(value: &'a mut Value) -> Self {
EntryMut::Value(value)
}
}