use std::future::{Future, IntoFuture};
use std::pin::Pin;
use super::*;
use aws_sdk_dynamodb::operation::delete_item::builders::DeleteItemFluentBuilder;
#[must_use = "builder does nothing until awaited or executed"]
pub struct DeleteItemRequest<
TD: TableDefinition,
T = (),
O: OutputFormat = Raw,
R: ReturnValue = ReturnNothing,
C: ConditionState = NoCondition,
> {
builder: DeleteItemFluentBuilder,
_marker: PhantomData<(TD, T, O, R, C)>,
}
impl<TD: TableDefinition, T, O: OutputFormat, R: ReturnValue, C: ConditionState>
DeleteItemRequest<TD, T, O, R, C>
{
pub fn into_inner(self) -> DeleteItemFluentBuilder {
self.builder
}
}
impl<TD: TableDefinition> DeleteItemRequest<TD> {
pub fn new(client: aws_sdk_dynamodb::Client, key: Key<TD>) -> Self {
Self::_new(client, key)
}
}
impl<TD: TableDefinition, T, O: OutputFormat, R: ReturnValue, C: ConditionState>
DeleteItemRequest<TD, T, O, R, C>
{
pub(super) fn _new(client: aws_sdk_dynamodb::Client, key: Key<TD>) -> Self {
let table_name = TD::table_name();
tracing::debug!(table_name, ?key, "DeleteItem");
Self {
builder: client
.delete_item()
.table_name(table_name)
.set_key(Some(key.into_inner())),
_marker: PhantomData,
}
}
}
impl<TD: TableDefinition, T, O: OutputFormat, C: ConditionState>
DeleteItemRequest<TD, T, O, ReturnNothing, C>
{
pub fn return_old(self) -> DeleteItemRequest<TD, T, O, Return<Old>, C> {
tracing::debug!("DeleteItem return_old");
DeleteItemRequest {
builder: self.builder,
_marker: PhantomData,
}
}
}
impl<TD: TableDefinition, T, O: OutputFormat, C: ConditionState>
DeleteItemRequest<TD, T, O, Return<Old>, C>
{
pub fn return_none(self) -> DeleteItemRequest<TD, T, O, ReturnNothing, C> {
tracing::debug!("DeleteItem return_none");
DeleteItemRequest {
builder: self.builder,
_marker: PhantomData,
}
}
}
impl<TD: TableDefinition, T, O: OutputFormat, R: ReturnValue>
DeleteItemRequest<TD, T, O, R, NoCondition>
{
pub fn condition(
mut self,
condition: Condition<'_>,
) -> DeleteItemRequest<TD, T, O, R, AlreadyHasCondition> {
tracing::debug!(%condition, "DeleteItem condition");
self.builder = condition.apply(self.builder);
DeleteItemRequest {
builder: self.builder,
_marker: PhantomData,
}
}
}
impl<TD: TableDefinition, T: DynamoDBItem<TD>, O: OutputFormat, R: ReturnValue>
DeleteItemRequest<TD, T, O, R, NoCondition>
{
pub fn exists(self) -> DeleteItemRequest<TD, T, O, R, AlreadyHasCondition> {
self.condition(T::exists())
}
}
impl<TD: TableDefinition, T, R: ReturnValue, C: ConditionState>
DeleteItemRequest<TD, T, Typed, R, C>
{
pub fn raw(self) -> DeleteItemRequest<TD, T, Raw, R, C> {
DeleteItemRequest {
builder: self.builder,
_marker: PhantomData,
}
}
}
impl<TD: TableDefinition, T, O: OutputFormat, C: ConditionState>
DeleteItemRequest<TD, T, O, ReturnNothing, C>
{
#[tracing::instrument(level = "debug", skip(self), name = "delete_execute")]
pub fn execute(self) -> impl Future<Output = Result<()>> + Send + 'static {
let builder = self.builder;
async move {
builder.return_values(SDKReturnValue::None).send().await?;
Ok(())
}
}
}
impl<TD: TableDefinition, T, O: OutputFormat, C: ConditionState> IntoFuture
for DeleteItemRequest<TD, T, O, ReturnNothing, C>
{
type Output = Result<()>;
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(self.execute())
}
}
impl<TD: TableDefinition, T: DynamoDBItem<TD> + DeserializeOwned, C: ConditionState>
DeleteItemRequest<TD, T, Typed, Return<Old>, C>
{
#[tracing::instrument(level = "debug", skip(self), name = "delete_execute_old")]
pub fn execute(self) -> impl Future<Output = Result<Option<T>>> + Send + 'static {
let builder = self.builder;
async move {
builder
.return_values(SDKReturnValue::AllOld)
.send()
.await?
.attributes
.map(Item::from_dynamodb_response)
.map(T::try_from_item)
.transpose()
}
}
}
impl<TD: TableDefinition, T: DynamoDBItem<TD> + DeserializeOwned, C: ConditionState> IntoFuture
for DeleteItemRequest<TD, T, Typed, Return<Old>, C>
{
type Output = Result<Option<T>>;
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(self.execute())
}
}
impl<TD: TableDefinition, T, C: ConditionState> DeleteItemRequest<TD, T, Raw, Return<Old>, C> {
#[tracing::instrument(level = "debug", skip(self), name = "delete_execute_old_raw")]
pub fn execute(self) -> impl Future<Output = Result<Option<Item<TD>>>> + Send + 'static {
let builder = self.builder;
async move {
Ok(builder
.return_values(SDKReturnValue::AllOld)
.send()
.await
.map(|out| out.attributes.map(Item::from_dynamodb_response))?)
}
}
}
impl<TD: TableDefinition, T, C: ConditionState> IntoFuture
for DeleteItemRequest<TD, T, Raw, Return<Old>, C>
{
type Output = Result<Option<Item<TD>>>;
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(self.execute())
}
}