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

/// Represents a DynamoDB [attribute name][1]. This will most commonly be used
/// for [top-level attributes][2].
///
/// This must only be used an attribute name without an index or additional
/// field.
///
/// When used in an [`Expression`], attribute `Name`s are automatically handled
/// as [expression attribute names][3], allowing for names that would not
/// otherwise be permitted by DynamoDB. For example, `foo` would become
/// something similar to `#0` in the expression, and the name would be in the
/// `expression_attribute_names`.
///
/// ```
/// use dynamodb_expression::path::Name;
///
/// // A variety of strings can be turned into a `Name`.
/// let name: Name = "foo".into();
/// let name: Name = String::from("foo").into();
/// let name: Name = (&String::from("foo")).into();
/// let name: Name = (&"foo").into();
/// ```
///
/// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.Attributes.html
/// [2]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.Attributes.html#Expressions.Attributes.TopLevelAttributes
/// [3]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ExpressionAttributeNames.html
/// [`Expression`]: crate::expression::Expression
/// [`Path`]: crate::path::Path
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Name {
    pub(crate) name: String,
}

impl Name {
    pub fn new<T>(name: T) -> Self
    where
        T: Into<String>,
    {
        Self { name: name.into() }
    }
}

impl fmt::Display for Name {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.name)
    }
}

impl From<String> for Name {
    fn from(name: String) -> Self {
        Self { name }
    }
}

impl From<&String> for Name {
    fn from(name: &String) -> Self {
        Self::from(name.to_owned())
    }
}

impl From<&str> for Name {
    fn from(name: &str) -> Self {
        Self::from(name.to_owned())
    }
}

impl From<&&str> for Name {
    fn from(name: &&str) -> Self {
        Self::from(name.to_owned())
    }
}

impl From<Name> for String {
    fn from(name: Name) -> Self {
        name.name
    }
}