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
use core::fmt;

use crate::{
    path::Path,
    value::{List, ValueOrRef},
};

/// Represents an update expression to [append elements to a list][1].
///
/// # Examples
///
/// ```
/// use dynamodb_expression::{num_value, path::{Name, Path}, update::ListAppend};
/// # use pretty_assertions::assert_eq;
///
/// let list_append = ListAppend::builder(Name::from("foo")).list([7, 8, 9].map(num_value));
/// assert_eq!("foo = list_append(foo, [7, 8, 9])", list_append.to_string());
///
/// let list_append_2 = Path::from(Name::from("foo")).list_append().list([7, 8, 9].map(num_value));
/// assert_eq!(list_append, list_append_2);
/// ```
///
/// If you want to add the new values to the _beginning_ of the list instead,
/// use the [`.before()`] method.
/// ```
/// use dynamodb_expression::{num_value, path::{Name, Path}, update::ListAppend};
/// # use pretty_assertions::assert_eq;
///
/// let list_append = Path::from(Name::from("foo")).list_append().before().list([1, 2, 3].map(num_value));
/// assert_eq!("foo = list_append([1, 2, 3], foo)", list_append.to_string());
/// ```
///
/// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html#Expressions.UpdateExpressions.SET.UpdatingListElements
/// [`.before()`]: Builder::before
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ListAppend {
    /// The field to set the newly combined list to
    // TODO: Name or Path?
    pub(crate) dst: Path,

    /// The field to get the current list from
    // TODO: Name or Path?
    pub(crate) src: Option<Path>,

    /// The value(s) to add to the list
    pub(crate) list: ValueOrRef,

    /// Whether to add the new values to the beginning or end of the source list
    order: Order,
}

impl ListAppend {
    pub fn builder<T>(dst: T) -> Builder
    where
        T: Into<Path>,
    {
        Builder {
            dst: dst.into(),
            src: None,
            order: None,
        }
    }
}

impl fmt::Display for ListAppend {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let Self {
            dst,
            src,
            list,
            order,
        } = self;

        // If no source field is specified, default to using the destination.
        let src = src.as_ref().unwrap_or(dst);

        write!(f, "{dst} = list_append(")?;

        match order {
            Order::Before => write!(f, "{list}, {src})"),
            Order::After => write!(f, "{src}, {list})"),
        }
    }
}

#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash)]
enum Order {
    /// Put the new elements before the existing elements.
    Before,

    /// Add the new elements after the existing elements.
    #[default]
    After,
}

/// Builds an [`ListAppend`] instance. Create an instance of this by using [`ListAppend::builder`].
#[must_use = "Consume the `Builder` using its `.list()` method"]
#[derive(Debug, Clone)]
pub struct Builder {
    dst: Path,
    src: Option<Path>,
    order: Option<Order>,
}

impl Builder {
    /// Sets the source field to read the initial value from.
    ///
    /// Defaults to the destination field.
    pub fn src<T>(mut self, src: T) -> Self
    where
        T: Into<Path>,
    {
        self.src = Some(src.into());

        self
    }

    /// The new values will be appended to the end of the existing values.
    ///
    /// This is the default.
    pub fn after(mut self) -> Self {
        self.order = Some(Order::After);

        self
    }

    /// The new values will be placed before the existing values.
    ///
    /// Defaults to appending new values after existing values.
    pub fn before(mut self) -> Self {
        self.order = Some(Order::Before);

        self
    }

    /// Sets the new value(s) to concatenate with the specified field.
    ///
    /// Builds the [`ListAppend`] instance.
    pub fn list<T>(self, list: T) -> ListAppend
    where
        T: Into<List>,
    {
        let Self {
            dst,
            src,
            order: op,
        } = self;

        ListAppend {
            dst,
            src,
            order: op.unwrap_or_default(),
            list: list.into().into(),
        }
    }
}

#[cfg(test)]
mod test {
    use pretty_assertions::assert_str_eq;

    use crate::path::Name;

    use super::ListAppend;

    #[test]
    fn display() {
        let append = ListAppend::builder(Name::from("foo"))
            .src(Name::from("bar"))
            .after()
            .list(["a", "b"]);
        assert_str_eq!(r#"foo = list_append(bar, ["a", "b"])"#, append.to_string());

        let append = ListAppend::builder(Name::from("foo"))
            .src(Name::from("bar"))
            .list(["a", "b"]);
        assert_str_eq!(r#"foo = list_append(bar, ["a", "b"])"#, append.to_string());

        let append = ListAppend::builder(Name::from("foo"))
            .src(Name::from("bar"))
            .before()
            .list(["a", "b"]);
        assert_str_eq!(r#"foo = list_append(["a", "b"], bar)"#, append.to_string());

        let append = ListAppend::builder(Name::from("foo"))
            .after()
            .list(["a", "b"]);
        assert_str_eq!(r#"foo = list_append(foo, ["a", "b"])"#, append.to_string());

        let append = ListAppend::builder(Name::from("foo")).list(["a", "b"]);
        assert_str_eq!(r#"foo = list_append(foo, ["a", "b"])"#, append.to_string());

        let append = ListAppend::builder(Name::from("foo"))
            .before()
            .list(["a", "b"]);
        assert_str_eq!(r#"foo = list_append(["a", "b"], foo)"#, append.to_string());
    }
}