use crate::PolicyDomain;
use async_trait::async_trait;
use std::fmt;
use std::future::Future;
use std::num::NonZeroUsize;
#[async_trait]
pub trait LookupSource<D: PolicyDomain>: Send + Sync {
type Id: Send + Sync + Clone;
type Error: std::error::Error + Send + Sync + 'static;
async fn lookup_page(
&self,
subject: &D::Subject,
action: &D::Action,
context: &D::Context,
cursor: Option<&[u8]>,
limit: NonZeroUsize,
) -> Result<LookupPage<Self::Id>, Self::Error>;
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LookupPage<Id> {
pub ids: Vec<Id>,
pub next_cursor: Option<Vec<u8>>,
}
#[async_trait]
pub trait Hydrator<Id>: Send + Sync {
type Resource: Send;
type Error: std::error::Error + Send + Sync + 'static;
async fn hydrate(&self, ids: &[Id]) -> Result<Vec<Option<Self::Resource>>, Self::Error>;
}
#[async_trait]
impl<F, Fut, Id, R, E> Hydrator<Id> for F
where
F: Fn(&[Id]) -> Fut + Send + Sync,
Fut: Future<Output = Result<Vec<Option<R>>, E>> + Send,
Id: Send + Sync,
R: Send,
E: std::error::Error + Send + Sync + 'static,
{
type Resource = R;
type Error = E;
async fn hydrate(&self, ids: &[Id]) -> Result<Vec<Option<Self::Resource>>, Self::Error> {
self(ids).await
}
}
#[non_exhaustive]
pub enum LookupAuthorizedError<LookupErr, HydrateErr, Resource = ()> {
Evaluation(crate::FilterError<Resource>),
Lookup(LookupErr),
Hydrate(HydrateErr),
LookupPageTooLarge {
limit: usize,
actual: usize,
},
HydratorContractViolation {
expected: usize,
actual: usize,
},
LookupCursorStuck,
}
impl<L: fmt::Debug, H: fmt::Debug, R> fmt::Debug for LookupAuthorizedError<L, H, R> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Evaluation(error) => f.debug_tuple("Evaluation").field(error).finish(),
Self::Lookup(error) => f.debug_tuple("Lookup").field(error).finish(),
Self::Hydrate(error) => f.debug_tuple("Hydrate").field(error).finish(),
Self::LookupPageTooLarge { limit, actual } => f
.debug_struct("LookupPageTooLarge")
.field("limit", limit)
.field("actual", actual)
.finish(),
Self::HydratorContractViolation { expected, actual } => f
.debug_struct("HydratorContractViolation")
.field("expected", expected)
.field("actual", actual)
.finish(),
Self::LookupCursorStuck => f.write_str("LookupCursorStuck"),
}
}
}
impl<L, H, R> fmt::Display for LookupAuthorizedError<L, H, R>
where
L: fmt::Display,
H: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Evaluation(err) => fmt::Display::fmt(err, f),
Self::Lookup(err) => write!(f, "lookup source error: {err}"),
Self::Hydrate(err) => write!(f, "hydrator error: {err}"),
Self::LookupPageTooLarge { limit, actual } => write!(
f,
"lookup source returned {actual} ids exceeding the requested limit of {limit}"
),
Self::HydratorContractViolation { expected, actual } => write!(
f,
"hydrator returned {actual} entries for {expected} ids; \
the result must match the input length"
),
Self::LookupCursorStuck => f.write_str(
"lookup source returned the same cursor twice in a row; \
cursors must strictly advance or be None to signal exhaustion",
),
}
}
}
impl<L, H, R> std::error::Error for LookupAuthorizedError<L, H, R>
where
L: std::error::Error + 'static,
H: std::error::Error + 'static,
{
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Evaluation(_) => None,
Self::Lookup(err) => Some(err),
Self::Hydrate(err) => Some(err),
Self::HydratorContractViolation { .. }
| Self::LookupPageTooLarge { .. }
| Self::LookupCursorStuck => None,
}
}
}
#[derive(Debug)]
pub struct LookupAuthorizedPage<R> {
pub resources: Vec<R>,
pub next_cursor: Option<Vec<u8>>,
}