dynamodb_expression/path/
name.rs

1use core::fmt;
2
3/// Represents a DynamoDB [attribute name][1]. This will most commonly be used
4/// for [top-level attributes][2].
5///
6/// This must only be used an attribute name without an index or additional
7/// field.
8///
9/// When used in an [`Expression`], attribute `Name`s are automatically handled
10/// as [expression attribute names][3], allowing for names that would not
11/// otherwise be permitted by DynamoDB. For example, `foo` would become
12/// something similar to `#0` in the expression, and the name would be in the
13/// `expression_attribute_names`.
14///
15/// ```
16/// use dynamodb_expression::path::Name;
17///
18/// // A variety of strings can be turned into a `Name`.
19/// let name: Name = "foo".into();
20/// let name: Name = String::from("foo").into();
21/// let name: Name = (&String::from("foo")).into();
22/// let name: Name = (&"foo").into();
23/// ```
24///
25/// [1]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.Attributes.html
26/// [2]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.Attributes.html#Expressions.Attributes.TopLevelAttributes
27/// [3]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ExpressionAttributeNames.html
28/// [`Expression`]: crate::expression::Expression
29/// [`Path`]: crate::path::Path
30#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
31pub struct Name {
32    pub(crate) name: String,
33}
34
35impl Name {
36    pub fn new<T>(name: T) -> Self
37    where
38        T: Into<String>,
39    {
40        Self { name: name.into() }
41    }
42}
43
44impl fmt::Display for Name {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        self.name.fmt(f)
47    }
48}
49
50impl From<String> for Name {
51    fn from(name: String) -> Self {
52        Self { name }
53    }
54}
55
56impl From<&String> for Name {
57    fn from(name: &String) -> Self {
58        Self::from(name.to_owned())
59    }
60}
61
62impl From<&str> for Name {
63    fn from(name: &str) -> Self {
64        Self::from(name.to_owned())
65    }
66}
67
68impl From<&&str> for Name {
69    fn from(name: &&str) -> Self {
70        Self::from(name.to_owned())
71    }
72}
73
74impl From<Name> for String {
75    fn from(name: Name) -> Self {
76        name.name
77    }
78}