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

use crate::{
    path::Path,
    value::{Num, Ref, Set, Value, ValueOrRef},
};

/// Represents an [`ADD` statement][1] in a [DynamoDB update expression][2].
///
/// The [DynamoDB documentation recommends][1] against using `ADD`:
///
/// > In general, we recommend using `SET` rather than `ADD`.
///
/// See also: [`Path::add`], [`Update`], [`Set`]
///
/// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html#Expressions.UpdateExpressions.ADD
/// [2]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html
/// [`Update`]: crate::update::Update
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Add {
    pub(crate) path: Path,
    pub(crate) value: ValueOrRef,
}

impl Add {
    pub fn new<N, V>(path: N, value: V) -> Self
    where
        N: Into<Path>,
        V: Into<AddValue>,
    {
        Self {
            path: path.into(),
            value: match value.into() {
                AddValue::Num(num) => Value::Scalar(num.into()).into(),
                AddValue::Set(set) => set.into(),
                AddValue::Ref(value_ref) => value_ref.into(),
            },
        }
    }
}

impl fmt::Display for Add {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "ADD {} {}", self.path, self.value)
    }
}

/// A value that can be used for the `ADD` operation in a DynamoDB update request.
///
/// See also: [`Path::add`], [`Add`]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AddValue {
    Set(Set),
    Num(Num),
    Ref(Ref),
}

impl fmt::Display for AddValue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Set(value) => value.fmt(f),
            Self::Num(value) => value.fmt(f),
            Self::Ref(value) => value.fmt(f),
        }
    }
}

impl From<Set> for AddValue {
    fn from(value: Set) -> Self {
        Self::Set(value)
    }
}

impl From<Num> for AddValue {
    fn from(value: Num) -> Self {
        Self::Num(value)
    }
}

impl From<Ref> for AddValue {
    fn from(value: Ref) -> Self {
        Self::Ref(value)
    }
}