use rusoto_dynamodb::{PutItemError, PutItemInput};
use crate::{Attributes, DynamoDb, DynamoError, Table};
pub trait Put<'d, D: 'd + ?Sized>: Table {
type Builder;
fn put(self, client: &'d D) -> Self::Builder;
}
impl<'d, D: 'd + ?Sized, T: Table + Into<Attributes>> Put<'d, D> for T {
type Builder = Expr<'d, D>;
fn put(self, client: &'d D) -> Self::Builder {
let input = PutItemInput {
table_name: T::table_name(),
item: self.into(),
..PutItemInput::default()
};
Expr::new(client, input)
}
}
pub struct Expr<'d, D: 'd + ?Sized> {
client: &'d D,
input: PutItemInput,
}
impl<'d, D: 'd + ?Sized> Expr<'d, D> {
pub const fn new(client: &'d D, input: PutItemInput) -> Self {
Self { client, input }
}
}
impl<'d, D: 'd + ?Sized> Expr<'d, D>
where
D: DynamoDb,
for<'a> &'a D: Send,
{
pub async fn execute(self) -> Result<(), DynamoError<PutItemError>> {
let Self { client, input } = self;
client.put_item(input).await?;
Ok(())
}
}