use core::{fmt::Debug, ops::RangeBounds};
use anyhash::Hasher;
use derive_more::{Display, Error};
use ufotofu::prelude::*;
use crate::prelude::*;
pub trait Store<const MCL: usize, const MCC: usize, const MPL: usize, N, S, PD, AT>: Clone {
type InternalError;
async fn create_entry<Data, P, H>(
&mut self,
data: &Data,
payload_producer: P,
payload_length: u64,
ingredients: &AT::Ingredients,
) -> Result<
Option<AuthorisedEntry<MCL, MCC, MPL, N, S, PD, AT>>,
CreateEntryError<Self::InternalError, P::Error, AT::CreationError>,
>
where
Data: ?Sized + Namespaced<N> + Coordinatelike<MCL, MCC, MPL, S>,
P: BulkProducer<Item = u8, Final = ()>,
H: Default + Hasher<PD>,
AT: AuthorisationToken<MCL, MCC, MPL, N, S, PD> + Debug,
N: Debug,
S: Debug,
PD: Debug;
async fn create_entry_nondestructive<Data, P, H>(
&mut self,
data: &Data,
payload_producer: P,
payload_length: u64,
ingredients: &AT::Ingredients,
) -> Result<
NondestructiveInsert<MCL, MCC, MPL, N, S, PD, AT>,
CreateEntryError<Self::InternalError, P::Error, AT::CreationError>,
>
where
Data: ?Sized + Namespaced<N> + Coordinatelike<MCL, MCC, MPL, S>,
P: BulkProducer<Item = u8, Final = ()>,
H: Default + Hasher<PD>,
AT: AuthorisationToken<MCL, MCC, MPL, N, S, PD> + Debug,
N: Debug,
S: Debug,
PD: Debug;
async fn insert_entry(
&mut self,
entry: AuthorisedEntry<MCL, MCC, MPL, N, S, PD, AT>,
) -> Result<bool, Self::InternalError>;
async fn forget_entry<K>(
&mut self,
namespace_id: &N,
key: &K,
expected_digest: Option<PD>,
) -> Result<bool, Self::InternalError>
where
K: Keylike<MCL, MCC, MPL, S>,
PD: PartialEq;
async fn forget_area(
&mut self,
namespace_id: &N,
area: &Area<MCL, MCC, MPL, S>,
) -> Result<(), Self::InternalError>;
async fn forget_namespace(&mut self, namespace_id: &N) -> Result<(), Self::InternalError>;
async fn get_entry<K, Slice>(
&mut self,
namespace_id: &N,
key: &K,
expected_digest: Option<PD>,
payload_slice: &Slice,
) -> Result<
Option<(
AuthorisedEntry<MCL, MCC, MPL, N, S, PD, AT>,
impl BulkProducer<Item = u8, Final = (), Error = PayloadProducerError<Self::InternalError>>,
)>,
Self::InternalError,
>
where
K: Keylike<MCL, MCC, MPL, S>,
Slice: RangeBounds<u64>;
async fn get_area(
&mut self,
namespace_id: N,
area: Area<MCL, MCC, MPL, S>,
) -> impl Producer<
Item = AuthorisedEntry<MCL, MCC, MPL, N, S, PD, AT>,
Final = (),
Error = Self::InternalError,
>;
async fn flush(&mut self) -> Result<(), Self::InternalError>;
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]
pub enum NondestructiveInsert<const MCL: usize, const MCC: usize, const MPL: usize, N, S, PD, AT> {
Success(AuthorisedEntry<MCL, MCC, MPL, N, S, PD, AT>),
Prevented,
Outdated,
}
#[derive(Debug, Display, Error, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
pub enum PayloadProducerError<StoreError> {
#[display("store error")]
StoreError(StoreError),
#[display("payload was removed")]
PayloadWasRemoved,
}
#[derive(Debug, Display, Error, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
pub enum CreateEntryError<StoreError, ProducerError, AuthorisationTokenError> {
#[display("store error")]
StoreError(StoreError),
#[display("incorrect payload length")]
IncorrectPayloadLength,
#[display("producer error")]
ProducerError(ProducerError),
#[display("authorisation token error")]
AuthorisationTokenError(AuthorisationTokenError),
}