use super::mutation::{validate_delete_mutation, validate_patch_mutation, validate_row_mutation};
use super::{TableMutation, TableStoreError};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TableAdapterCapabilities {
pub relational_rows: bool,
pub sparse_patches: bool,
pub deletes: bool,
}
impl Default for TableAdapterCapabilities {
fn default() -> Self {
Self {
relational_rows: true,
sparse_patches: true,
deletes: true,
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct TableCommitOutcome;
impl TableCommitOutcome {
pub fn applied() -> Self {
Self
}
pub fn was_applied(&self) -> bool {
true
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct TableWritePlan {
pub mutations: Vec<TableMutation>,
}
impl TableWritePlan {
pub fn new(mutations: Vec<TableMutation>) -> Self {
Self { mutations }
}
pub fn is_empty(&self) -> bool {
self.mutations.is_empty()
}
pub fn validate(&self) -> Result<(), TableStoreError> {
self.validate_for(&TableAdapterCapabilities::default())
}
pub fn validate_for(
&self,
capabilities: &TableAdapterCapabilities,
) -> Result<(), TableStoreError> {
for mutation in &self.mutations {
match mutation {
TableMutation::UpsertRow(mutation) => {
if !capabilities.relational_rows {
return Err(TableStoreError::Metadata(
"read-model adapter does not support relational row writes".into(),
));
}
validate_row_mutation(mutation)?;
}
TableMutation::PatchRow(mutation) => {
if !capabilities.relational_rows || !capabilities.sparse_patches {
return Err(TableStoreError::Metadata(
"read-model adapter does not support sparse row patches".into(),
));
}
validate_patch_mutation(mutation)?;
}
TableMutation::DeleteRow(mutation) => {
if !capabilities.relational_rows || !capabilities.deletes {
return Err(TableStoreError::Metadata(
"read-model adapter does not support row deletes".into(),
));
}
validate_delete_mutation(mutation)?;
}
}
}
Ok(())
}
}