Skip to main content

kardia_core/
deps.rs

1use crate::context::Context;
2use crate::error::AccessError;
3use crate::plugin::Inject;
4use crate::service::{Service, Svc};
5
6/// A tuple of service types declared and accessed together:
7/// `type Inject = (Database, Cache)` → `ctx.deps::<(Database, Cache)>()?`.
8pub trait Deps {
9    type Output;
10
11    fn keys() -> Vec<&'static str>;
12
13    fn get(ctx: &Context) -> Result<Self::Output, AccessError>;
14}
15
16macro_rules! deps_tuple {
17    ($($T:ident),*) => {
18        impl<$($T: Service),*> Deps for ($($T,)*) {
19            type Output = ($(Svc<$T>,)*);
20
21            fn keys() -> Vec<&'static str> {
22                vec![$($T::NAME),*]
23            }
24
25            #[allow(unused_variables)]
26            fn get(ctx: &Context) -> Result<Self::Output, AccessError> {
27                Ok(($(ctx.get::<$T>()?,)*))
28            }
29        }
30    };
31}
32
33deps_tuple!();
34deps_tuple!(A);
35deps_tuple!(A, B);
36deps_tuple!(A, B, C);
37deps_tuple!(A, B, C, D);
38deps_tuple!(A, B, C, D, E);
39deps_tuple!(A, B, C, D, E, F);
40deps_tuple!(A, B, C, D, E, F, G);
41deps_tuple!(A, B, C, D, E, F, G, H);
42
43impl Inject {
44    /// The declaration of a tuple of service types.
45    pub fn of<D: Deps>() -> Self {
46        D::keys().into()
47    }
48}
49
50impl Context {
51    /// Accesses a tuple of declared services through the committed view.
52    pub fn deps<D: Deps>(&self) -> Result<D::Output, AccessError> {
53        D::get(self)
54    }
55}
56
57/// Positions within a dependency tuple (inferred, never written by hand).
58pub mod index {
59    pub struct I0;
60    pub struct I1;
61    pub struct I2;
62    pub struct I3;
63    pub struct I4;
64    pub struct I5;
65    pub struct I6;
66    pub struct I7;
67}
68
69/// `D` contains `S` (at position `I`).
70pub trait Has<S, I> {}
71
72macro_rules! has {
73    ($params:tt $($S:ident => $I:ident),*) => {
74        $(has!(@one $params $S => $I);)*
75    };
76    (@one [$($T:ident),*] $S:ident => $I:ident) => {
77        impl<$($T),*> Has<$S, index::$I> for ($($T,)*) {}
78    };
79}
80
81has!([A] A => I0);
82has!([A, B] A => I0, B => I1);
83has!([A, B, C] A => I0, B => I1, C => I2);
84has!([A, B, C, D] A => I0, B => I1, C => I2, D => I3);
85has!([A, B, C, D, E] A => I0, B => I1, C => I2, D => I3, E => I4);
86has!([A, B, C, D, E, F] A => I0, B => I1, C => I2, D => I3, E => I4, F => I5);
87has!([A, B, C, D, E, F, G] A => I0, B => I1, C => I2, D => I3, E => I4, F => I5, G => I6);
88has!([A, B, C, D, E, F, G, H] A => I0, B => I1, C => I2, D => I3, E => I4, F => I5, G => I6, H => I7);
89
90/// A plugin context whose typed accesses are checked against its declared
91/// dependencies `D` at compile time: `get::<S>()` only compiles if `S` is in `D`.
92pub struct Injected<D> {
93    ctx: Context,
94    _deps: std::marker::PhantomData<fn() -> D>,
95}
96
97impl<D> Clone for Injected<D> {
98    fn clone(&self) -> Self {
99        Self { ctx: self.ctx.clone(), _deps: std::marker::PhantomData }
100    }
101}
102
103impl<D: Deps> Injected<D> {
104    pub fn new(ctx: Context) -> Self {
105        Self { ctx, _deps: std::marker::PhantomData }
106    }
107
108    /// A declared dependency, read through the committed view:
109    /// `ctx.get::<Database, _>()` (the `_` is its inferred position in `D`).
110    pub fn get<S: Service, I>(&self) -> Result<Svc<S>, AccessError>
111    where
112        D: Has<S, I>,
113    {
114        self.ctx.get::<S>()
115    }
116
117    /// All declared dependencies at once.
118    pub fn deps(&self) -> Result<D::Output, AccessError> {
119        D::get(&self.ctx)
120    }
121
122    /// The underlying (unchecked) context.
123    pub fn context(&self) -> &Context {
124        &self.ctx
125    }
126}
127
128impl<D> std::ops::Deref for Injected<D> {
129    type Target = Context;
130
131    fn deref(&self) -> &Context {
132        &self.ctx
133    }
134}