use std::fmt;
use std::str::FromStr;
use serde::{Serialize, Serializer};
use super::types::AttributeValue;
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct DynamoId(String);
impl DynamoId {
pub fn new(value: impl Into<String>) -> Self {
Self(value.into())
}
pub fn from_attr(value: &AttributeValue) -> Option<Self> {
match value {
AttributeValue::S(s) => Some(Self(s.clone())),
AttributeValue::N(n) => Some(Self(n.clone())),
_ => None,
}
}
pub fn to_attr(&self) -> AttributeValue {
AttributeValue::S(self.0.clone())
}
pub fn as_str(&self) -> &str {
&self.0
}
pub fn into_string(self) -> String {
self.0
}
}
impl fmt::Display for DynamoId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl FromStr for DynamoId {
type Err = std::convert::Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(s.to_string()))
}
}
impl Serialize for DynamoId {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&self.0)
}
}
impl From<String> for DynamoId {
fn from(s: String) -> Self {
Self(s)
}
}
impl From<&str> for DynamoId {
fn from(s: &str) -> Self {
Self(s.to_string())
}
}