use std::marker::PhantomData;
use std::sync::Arc;
use crate::adapter::Adapter;
use crate::model::Fields;
use crate::plugin::{Plugin, Registry};
use crate::value::{DecodeError, Row};
#[derive(Clone, Copy, Debug, Default)]
pub struct Nil;
#[derive(Debug)]
pub struct Cons<H, T>(pub(crate) Arc<H>, pub(crate) T);
impl<H, T: Clone> Clone for Cons<H, T> {
fn clone(&self) -> Self {
Cons(self.0.clone(), self.1.clone())
}
}
#[derive(Debug)]
pub struct Here;
#[derive(Debug)]
pub struct There<I>(PhantomData<I>);
#[macro_export]
macro_rules! plugins {
() => { $crate::Nil };
($head:ty $(, $tail:ty)* $(,)?) => { $crate::Cons<$head, $crate::plugins![$($tail),*]> };
}
pub trait PluginSet: Send + Sync + 'static {
type UserExt: Clone + Send + Sync + 'static;
type SessionExt: Clone + Send + Sync + 'static;
#[doc(hidden)]
fn register<D: Adapter>(&self, registry: &mut Registry<D>);
#[doc(hidden)]
fn decode_user(row: &Row) -> Result<Self::UserExt, DecodeError>;
#[doc(hidden)]
fn decode_session(row: &Row) -> Result<Self::SessionExt, DecodeError>;
}
impl PluginSet for Nil {
type UserExt = ();
type SessionExt = ();
fn register<D: Adapter>(&self, _: &mut Registry<D>) {}
fn decode_user(_: &Row) -> Result<(), DecodeError> {
Ok(())
}
fn decode_session(_: &Row) -> Result<(), DecodeError> {
Ok(())
}
}
impl<H: Plugin, T: PluginSet> PluginSet for Cons<H, T> {
type UserExt = (H::UserFields, T::UserExt);
type SessionExt = (H::SessionFields, T::SessionExt);
fn register<D: Adapter>(&self, registry: &mut Registry<D>) {
registry.register_plugin(&self.0);
self.1.register(registry);
}
fn decode_user(row: &Row) -> Result<Self::UserExt, DecodeError> {
Ok((H::UserFields::decode(row)?, T::decode_user(row)?))
}
fn decode_session(row: &Row) -> Result<Self::SessionExt, DecodeError> {
Ok((H::SessionFields::decode(row)?, T::decode_session(row)?))
}
}
#[diagnostic::on_unimplemented(
message = "plugin `{P}` is not registered on this auth instance",
label = "`{P}` is not in this plugin list",
note = "register it with `.plugin(...)` on the builder, before any plugin that requires it, and list it in your `plugins![..]` alias"
)]
pub trait Has<P: Plugin, I>: PluginSet {
fn get(&self) -> &Arc<P>;
fn user_ext(ext: &Self::UserExt) -> &P::UserFields;
fn session_ext(ext: &Self::SessionExt) -> &P::SessionFields;
}
impl<P: Plugin, T: PluginSet> Has<P, Here> for Cons<P, T> {
fn get(&self) -> &Arc<P> {
&self.0
}
fn user_ext(ext: &Self::UserExt) -> &P::UserFields {
&ext.0
}
fn session_ext(ext: &Self::SessionExt) -> &P::SessionFields {
&ext.0
}
}
impl<P: Plugin, H: Plugin, T: Has<P, I>, I> Has<P, There<I>> for Cons<H, T> {
fn get(&self) -> &Arc<P> {
self.1.get()
}
fn user_ext(ext: &Self::UserExt) -> &P::UserFields {
T::user_ext(&ext.1)
}
fn session_ext(ext: &Self::SessionExt) -> &P::SessionFields {
T::session_ext(&ext.1)
}
}
pub trait Satisfied<L, I> {}
impl<L> Satisfied<L, ()> for () {}
macro_rules! satisfied {
($(($p:ident, $i:ident)),+) => {
impl<L, $($p: Plugin, $i),+> Satisfied<L, ($($i,)+)> for ($($p,)+) where $(L: Has<$p, $i>),+ {}
};
}
satisfied!((A, IA));
satisfied!((A, IA), (B, IB));
satisfied!((A, IA), (B, IB), (C, IC));
satisfied!((A, IA), (B, IB), (C, IC), (E, IE));
satisfied!((A, IA), (B, IB), (C, IC), (E, IE), (F, IF));
satisfied!((A, IA), (B, IB), (C, IC), (E, IE), (F, IF), (G, IG));
pub trait Append<T> {
type Out;
fn append(self, item: Arc<T>) -> Self::Out;
}
impl<T> Append<T> for Nil {
type Out = Cons<T, Nil>;
fn append(self, item: Arc<T>) -> Self::Out {
Cons(item, Nil)
}
}
impl<H, Tail: Append<T>, T> Append<T> for Cons<H, Tail> {
type Out = Cons<H, Tail::Out>;
fn append(self, item: Arc<T>) -> Self::Out {
Cons(self.0, self.1.append(item))
}
}