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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//! Types related to [DynamoDB update expressions][1]. For more, see [`Update`].
//!
//! [1]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html

mod add;
mod delete;
mod remove;
mod set;
mod set_remove;

use core::fmt;

pub use self::{
    add::{Add, AddValue},
    delete::Delete,
    remove::Remove,
    set::{
        if_not_exists, list_append, math, Assign, IfNotExists, ListAppend, Math, Set, SetAction,
    },
    set_remove::SetRemove,
};

/// Represents a [DynamoDB update expression][1].
///
/// See also: [`Expression`], [`Set`], [`Remove`], [`SetRemove`], [`Add`],
/// [`Delete`],
///
/// # Examples
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use dynamodb_expression::{update::Update, Expression, Path};
/// # use pretty_assertions::assert_eq;
///
/// let update = Update::from("foo".parse::<Path>()?.math().add(7));
/// assert_eq!("SET foo = foo + 7", update.to_string());
///
/// let update = Update::from("foo".parse::<Path>()?.if_not_exists().set("a value"));
/// assert_eq!(
///     r#"SET foo = if_not_exists(foo, "a value")"#,
///     update.to_string()
/// );
///
/// let update = Update::from("foo".parse::<Path>()?.remove());
/// assert_eq!(r#"REMOVE foo"#, update.to_string());
/// #
/// # // TODO: Examples for `Add` and `Delete`.
///
/// // To use an `Update`, build an `Expression`.
/// let expression = Expression::builder().with_update(update).build();
/// # _ = expression;
/// #
/// # Ok(())
/// # }
/// ```
///
/// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html
/// [`Expression`]: crate::Expression
#[must_use = "Use in a DynamoDB expression with `Expression::builder().with_update(update)`"]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Update {
    SetRemove(SetRemove),
    Add(Add),
    Delete(Delete),
}

impl Update {
    /// A new update expression for a [`Set`] statement.
    pub fn new_set<T>(set: T) -> Self
    where
        T: Into<Set>,
    {
        set.into().into()
    }

    /// A new update expression for a [`Remove`] statement.
    pub fn new_remove<T>(remove: T) -> Self
    where
        T: Into<Remove>,
    {
        remove.into().into()
    }

    /// A new update expression for a [`SetRemove`] statement.
    pub fn new_set_remove<T>(set_remove: T) -> Self
    where
        T: Into<SetRemove>,
    {
        set_remove.into().into()
    }

    /// A new update expression for an [`Add`] statement.
    pub fn new_add<T>(add: T) -> Self
    where
        T: Into<Add>,
    {
        add.into().into()
    }

    /// A new update expression for a [`Delete`] statement.
    pub fn new_delete<T>(delete: T) -> Self
    where
        T: Into<Delete>,
    {
        delete.into().into()
    }
}

impl fmt::Display for Update {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Update::SetRemove(update) => update.fmt(f),
            Update::Add(update) => update.fmt(f),
            Update::Delete(update) => update.fmt(f),
        }
    }
}

impl From<Set> for Update {
    fn from(value: Set) -> Self {
        Self::SetRemove(value.into())
    }
}

impl From<SetAction> for Update {
    fn from(value: SetAction) -> Self {
        Self::SetRemove(value.into())
    }
}

impl From<Assign> for Update {
    fn from(value: Assign) -> Self {
        Self::SetRemove(value.into())
    }
}

impl From<Math> for Update {
    fn from(value: Math) -> Self {
        Self::SetRemove(value.into())
    }
}

impl From<ListAppend> for Update {
    fn from(value: ListAppend) -> Self {
        Self::SetRemove(value.into())
    }
}

impl From<IfNotExists> for Update {
    fn from(value: IfNotExists) -> Self {
        Self::SetRemove(value.into())
    }
}

impl From<Remove> for Update {
    fn from(value: Remove) -> Self {
        Self::SetRemove(value.into())
    }
}

impl From<SetRemove> for Update {
    fn from(value: SetRemove) -> Self {
        Self::SetRemove(value)
    }
}

impl From<Add> for Update {
    fn from(value: Add) -> Self {
        Self::Add(value)
    }
}

impl From<Delete> for Update {
    fn from(value: Delete) -> Self {
        Self::Delete(value)
    }
}

#[cfg(test)]
mod examples {
    #[test]
    fn example() -> Result<(), Box<dyn std::error::Error>> {
        use crate::{update::Update, Path};
        use pretty_assertions::assert_eq;

        let update = Update::from("foo".parse::<Path>()?.math().add(7));
        assert_eq!("SET foo = foo + 7", update.to_string());

        let update = Update::from("foo".parse::<Path>()?.if_not_exists().set("a value"));
        assert_eq!(
            r#"SET foo = if_not_exists(foo, "a value")"#,
            update.to_string()
        );

        let update = Update::from("foo".parse::<Path>()?.remove());
        assert_eq!(r#"REMOVE foo"#, update.to_string());

        // TODO: Examples for `Add` and `Delete`.

        Ok(())
    }
}