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
use core::fmt::{self, Write};
use super::Value;
/// A DynamoDB value, or a reference to one stored in the collected expression values.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) enum ValueOrRef {
Value(Value),
Ref(Ref),
}
impl fmt::Display for ValueOrRef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ValueOrRef::Value(value) => value.fmt(f),
ValueOrRef::Ref(value) => value.fmt(f),
}
}
}
impl<T> From<T> for ValueOrRef
where
T: Into<Value>,
{
fn from(value: T) -> Self {
Self::Value(value.into())
}
}
impl From<Ref> for ValueOrRef {
fn from(value: Ref) -> Self {
Self::Ref(value)
}
}
/// A reference to a DynamoDB value stored in expression attribute values.
/// Automatically prefixed with `:`.
///
/// ```
/// use dynamodb_expression::value::Ref;
/// # use pretty_assertions::assert_eq;
///
/// let value = Ref::new("expression_value");
/// assert_eq!(":expression_value", value.to_string())
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Ref(String);
impl Ref {
pub fn new<T>(value_ref: T) -> Self
where
T: Into<String>,
{
Self(value_ref.into())
}
}
impl From<String> for Ref {
fn from(value: String) -> Self {
Self(value)
}
}
impl From<&String> for Ref {
fn from(value: &String) -> Self {
Self(value.to_owned())
}
}
impl From<&str> for Ref {
fn from(value: &str) -> Self {
Self(value.to_owned())
}
}
impl From<&&str> for Ref {
fn from(value: &&str) -> Self {
Self((*value).to_owned())
}
}
impl fmt::Display for Ref {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_char(':')?;
self.0.fmt(f)
}
}
impl From<Ref> for String {
fn from(mut value: Ref) -> Self {
value.0.insert(0, ':');
value.0
}
}
/// Represents a value that is either a string, or a reference to a value
/// already in the expression attribute values.
///
/// ```
/// use dynamodb_expression::value::{StringOrRef, Ref};
///
/// let value: StringOrRef = "a string value".into();
/// let value: StringOrRef = Ref::new("expression_value").into();
/// ```
///
/// For example, the [`BeginsWith`] operator can take a string or a reference to
/// an extended attribute value. Here's how [`StringOrRef`] works with that.
///
/// ```
/// use dynamodb_expression::{condition::BeginsWith, value::Ref, Path};
/// # use pretty_assertions::assert_eq;
///
/// let begins_with = BeginsWith::new(Path::new_name("foo"), "T");
/// assert_eq!(r#"begins_with(foo, "T")"#, begins_with.to_string());
///
/// let begins_with = BeginsWith::new(Path::new_name("foo"), Ref::new("prefix"));
/// assert_eq!(r#"begins_with(foo, :prefix)"#, begins_with.to_string());
/// ```
///
/// See also: [`Ref`]
///
/// [`BeginsWith`]: crate::condition::BeginsWith
pub enum StringOrRef {
String(String),
Ref(Ref),
}
impl From<String> for StringOrRef {
fn from(value: String) -> Self {
Self::String(value)
}
}
impl From<&String> for StringOrRef {
fn from(value: &String) -> Self {
Self::String(value.to_owned())
}
}
impl From<&str> for StringOrRef {
fn from(value: &str) -> Self {
Self::String(value.to_owned())
}
}
impl From<&&str> for StringOrRef {
fn from(value: &&str) -> Self {
Self::String((*value).to_owned())
}
}
impl From<Ref> for StringOrRef {
fn from(value: Ref) -> Self {
Self::Ref(value)
}
}
impl From<StringOrRef> for ValueOrRef {
fn from(value: StringOrRef) -> Self {
match value {
StringOrRef::String(value) => value.into(),
StringOrRef::Ref(value) => value.into(),
}
}
}
#[cfg(test)]
mod test {
use pretty_assertions::assert_str_eq;
use crate::Scalar;
use super::{Ref, ValueOrRef};
#[test]
fn display_value() {
let vr = ValueOrRef::from(Scalar::new_string("foo"));
assert_str_eq!("\"foo\"", vr.to_string());
}
#[test]
fn display_ref() {
let vr = ValueOrRef::Ref(Ref("foo".into()));
assert_str_eq!(":foo", vr.to_string());
}
}