Skip to main content

fastauth_core/
hlist.rs

1//! The type-level plugin list. See `docs/reference/02-internals.md`.
2
3use std::marker::PhantomData;
4use std::sync::Arc;
5
6use crate::adapter::Adapter;
7use crate::model::Fields;
8use crate::plugin::{Plugin, Registry};
9use crate::value::{DecodeError, Row};
10
11/// The empty plugin list.
12#[derive(Clone, Copy, Debug, Default)]
13pub struct Nil;
14
15/// A plugin followed by the rest of the list.
16#[derive(Debug)]
17pub struct Cons<H, T>(pub(crate) Arc<H>, pub(crate) T);
18
19impl<H, T: Clone> Clone for Cons<H, T> {
20    fn clone(&self) -> Self {
21        Cons(self.0.clone(), self.1.clone())
22    }
23}
24
25/// Index: the plugin is the head.
26#[derive(Debug)]
27pub struct Here;
28
29/// Index: the plugin is in the tail.
30#[derive(Debug)]
31pub struct There<I>(PhantomData<I>);
32
33/// `plugins![A, B]` is `Cons<A, Cons<B, Nil>>`: the plugin list in registration order.
34#[macro_export]
35macro_rules! plugins {
36    () => { $crate::Nil };
37    ($head:ty $(, $tail:ty)* $(,)?) => { $crate::Cons<$head, $crate::plugins![$($tail),*]> };
38}
39
40/// Walks the list: registration at build time, decoding on every session load.
41pub trait PluginSet: Send + Sync + 'static {
42    /// Nested tuple of each plugin's `UserFields`, parallel to the list.
43    type UserExt: Clone + Send + Sync + 'static;
44    /// Nested tuple of each plugin's `SessionFields`.
45    type SessionExt: Clone + Send + Sync + 'static;
46
47    #[doc(hidden)]
48    fn register<D: Adapter>(&self, registry: &mut Registry<D>);
49    #[doc(hidden)]
50    fn decode_user(row: &Row) -> Result<Self::UserExt, DecodeError>;
51    #[doc(hidden)]
52    fn decode_session(row: &Row) -> Result<Self::SessionExt, DecodeError>;
53}
54
55impl PluginSet for Nil {
56    type UserExt = ();
57    type SessionExt = ();
58
59    fn register<D: Adapter>(&self, _: &mut Registry<D>) {}
60    fn decode_user(_: &Row) -> Result<(), DecodeError> {
61        Ok(())
62    }
63    fn decode_session(_: &Row) -> Result<(), DecodeError> {
64        Ok(())
65    }
66}
67
68impl<H: Plugin, T: PluginSet> PluginSet for Cons<H, T> {
69    type UserExt = (H::UserFields, T::UserExt);
70    type SessionExt = (H::SessionFields, T::SessionExt);
71
72    fn register<D: Adapter>(&self, registry: &mut Registry<D>) {
73        registry.register_plugin(&self.0);
74        self.1.register(registry);
75    }
76    fn decode_user(row: &Row) -> Result<Self::UserExt, DecodeError> {
77        Ok((H::UserFields::decode(row)?, T::decode_user(row)?))
78    }
79    fn decode_session(row: &Row) -> Result<Self::SessionExt, DecodeError> {
80        Ok((H::SessionFields::decode(row)?, T::decode_session(row)?))
81    }
82}
83
84#[diagnostic::on_unimplemented(
85    message = "plugin `{P}` is not registered on this auth instance",
86    label = "`{P}` is not in this plugin list",
87    note = "register it with `.plugin(...)` on the builder, before any plugin that requires it, and list it in your `plugins![..]` alias"
88)]
89pub trait Has<P: Plugin, I>: PluginSet {
90    fn get(&self) -> &Arc<P>;
91    fn user_ext(ext: &Self::UserExt) -> &P::UserFields;
92    fn session_ext(ext: &Self::SessionExt) -> &P::SessionFields;
93}
94
95impl<P: Plugin, T: PluginSet> Has<P, Here> for Cons<P, T> {
96    fn get(&self) -> &Arc<P> {
97        &self.0
98    }
99    fn user_ext(ext: &Self::UserExt) -> &P::UserFields {
100        &ext.0
101    }
102    fn session_ext(ext: &Self::SessionExt) -> &P::SessionFields {
103        &ext.0
104    }
105}
106
107impl<P: Plugin, H: Plugin, T: Has<P, I>, I> Has<P, There<I>> for Cons<H, T> {
108    fn get(&self) -> &Arc<P> {
109        self.1.get()
110    }
111    fn user_ext(ext: &Self::UserExt) -> &P::UserFields {
112        T::user_ext(&ext.1)
113    }
114    fn session_ext(ext: &Self::SessionExt) -> &P::SessionFields {
115        T::session_ext(&ext.1)
116    }
117}
118
119/// A tuple of plugins that must all be in list `L`. Checked by `.plugin(..)`.
120pub trait Satisfied<L, I> {}
121
122impl<L> Satisfied<L, ()> for () {}
123
124macro_rules! satisfied {
125    ($(($p:ident, $i:ident)),+) => {
126        impl<L, $($p: Plugin, $i),+> Satisfied<L, ($($i,)+)> for ($($p,)+) where $(L: Has<$p, $i>),+ {}
127    };
128}
129
130satisfied!((A, IA));
131satisfied!((A, IA), (B, IB));
132satisfied!((A, IA), (B, IB), (C, IC));
133satisfied!((A, IA), (B, IB), (C, IC), (E, IE));
134satisfied!((A, IA), (B, IB), (C, IC), (E, IE), (F, IF));
135satisfied!((A, IA), (B, IB), (C, IC), (E, IE), (F, IF), (G, IG));
136
137/// Appends at the end, so the list reads in registration order like `plugins![..]`.
138pub trait Append<T> {
139    type Out;
140    fn append(self, item: Arc<T>) -> Self::Out;
141}
142
143impl<T> Append<T> for Nil {
144    type Out = Cons<T, Nil>;
145    fn append(self, item: Arc<T>) -> Self::Out {
146        Cons(item, Nil)
147    }
148}
149
150impl<H, Tail: Append<T>, T> Append<T> for Cons<H, Tail> {
151    type Out = Cons<H, Tail::Out>;
152    fn append(self, item: Arc<T>) -> Self::Out {
153        Cons(self.0, self.1.append(item))
154    }
155}