use async_trait::async_trait;
use crate::violation::Violation;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WriteKind {
Create,
Update,
Delete,
Restore,
HardDelete,
}
#[derive(Debug)]
pub struct WriteCtx<'a, E> {
pub kind: WriteKind,
pub before: Option<&'a E>,
pub after: Option<&'a E>,
}
#[derive(Debug, Default, Clone, PartialEq)]
pub struct GuardOutcome {
pub refuse: Vec<Violation>,
pub shadow: Vec<Violation>,
}
impl GuardOutcome {
pub fn allow() -> Self {
Self::default()
}
pub fn merge(mut self, other: GuardOutcome) -> Self {
self.refuse.extend(other.refuse);
self.shadow.extend(other.shadow);
self
}
}
#[async_trait]
pub trait WriteGuard<E>: Send + Sync {
async fn check(&self, ctx: &WriteCtx<'_, E>) -> GuardOutcome;
}
#[derive(Debug, Default, Clone, Copy)]
pub struct AllowAll;
#[async_trait]
impl<E: Sync> WriteGuard<E> for AllowAll {
async fn check(&self, _ctx: &WriteCtx<'_, E>) -> GuardOutcome {
GuardOutcome::allow()
}
}