use core::fmt;
use std::marker::PhantomData;
use crate::{AttributeDefinition, CompositeKeySchema, Condition, IntoAttributeValue, KeySchema};
mod sealed_traits {
pub trait KeyConditionStateSeal {}
}
crate::utils::impl_sealed_marker_types!(
KeyConditionState,
sealed_traits::KeyConditionStateSeal;
#[doc(hidden)]
PkOnly,
#[doc(hidden)]
WithSk
);
#[derive(Debug, Clone)]
#[must_use = "key condition does nothing until applied to a request"]
pub struct KeyCondition<'a, KS: KeySchema, S: KeyConditionState = PkOnly>(
Condition<'a>,
PhantomData<(KS, S)>,
);
impl<'a, KS: KeySchema> KeyCondition<'a, KS> {
pub fn pk(value: impl IntoAttributeValue) -> Self {
KeyCondition(Condition::eq(KS::PartitionKey::NAME, value), PhantomData)
}
}
impl<'a, KS: CompositeKeySchema> KeyCondition<'a, KS> {
pub fn sk_eq(self, value: impl IntoAttributeValue) -> KeyCondition<'a, KS, WithSk> {
KeyCondition(
self.0 & Condition::eq(KS::SortKey::NAME, value),
PhantomData,
)
}
pub fn sk_lt(self, value: impl IntoAttributeValue) -> KeyCondition<'a, KS, WithSk> {
KeyCondition(
self.0 & Condition::lt(KS::SortKey::NAME, value),
PhantomData,
)
}
pub fn sk_le(self, value: impl IntoAttributeValue) -> KeyCondition<'a, KS, WithSk> {
KeyCondition(
self.0 & Condition::le(KS::SortKey::NAME, value),
PhantomData,
)
}
pub fn sk_gt(self, value: impl IntoAttributeValue) -> KeyCondition<'a, KS, WithSk> {
KeyCondition(
self.0 & Condition::gt(KS::SortKey::NAME, value),
PhantomData,
)
}
pub fn sk_ge(self, value: impl IntoAttributeValue) -> KeyCondition<'a, KS, WithSk> {
KeyCondition(
self.0 & Condition::ge(KS::SortKey::NAME, value),
PhantomData,
)
}
pub fn sk_between(
self,
low: impl IntoAttributeValue,
high: impl IntoAttributeValue,
) -> KeyCondition<'a, KS, WithSk> {
KeyCondition(
self.0 & Condition::between(KS::SortKey::NAME, low, high),
PhantomData,
)
}
pub fn sk_begins_with(self, prefix: impl IntoAttributeValue) -> KeyCondition<'a, KS, WithSk> {
KeyCondition(
self.0 & Condition::begins_with(KS::SortKey::NAME, prefix),
PhantomData,
)
}
}
impl<KS: KeySchema, S: KeyConditionState> fmt::Display for KeyCondition<'_, KS, S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
use super::{ApplyKeyCondition, KeyConditionableBuilder};
impl<B: KeyConditionableBuilder, KS: KeySchema, S: KeyConditionState> ApplyKeyCondition<B>
for KeyCondition<'_, KS, S>
{
fn apply_key_condition(self, builder: B) -> B {
self.0.apply_key_condition(builder)
}
}