1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#[doc(hidden)]
#[macro_export]
macro_rules! private_define_context {
    {
        $caller:tt
        input = [{
            $name:ident {
                $($body:tt)*
            }
        }]
    } => {
        $crate::tt_call::tt_call! {
            macro = [{ $crate::private_define_context }]
            rest = [{ $($body)* }]
            ~~> $crate::private_define_context! {
                $caller
                name = [{ $name }]
            }
        }
    };
    {
        $caller:tt
        name = [{ $name:ident }]
        $(auto_field = [{ $auto_field:ident, $auto_t:ty, $factory:ty, ($($f_args:ident,)*) }])*
        $(field = [{ $field:ident, $t:ty }])*
    } => {
        $crate::tt_call::tt_return! {
            $caller
            result = [{
                #[derive(Clone, Debug)]
                struct $name {
                    $($auto_field: $auto_t,)*
                    $($field: $t,)*
                }

                impl $name {
                    fn new($($field: $t,)*) -> Result<Self, anyhow::Error> {
                        $(
                            let $auto_field = <$factory as $crate::Factory<_>>::build(($($f_args.clone(),)*))?;
                        )*
                        Ok(Self {
                            $($auto_field,)*
                            $($field,)*
                        })
                    }
                }

                $(
                    impl $crate::Provide<$auto_t> for $name {
                        fn provide(&self) -> $auto_t {
                            self.$auto_field.clone()
                        }
                    }
                    impl $crate::ProvideWith<$auto_t> for $name {
                        fn provide_with<E, F: FnOnce($auto_t) -> Result<$auto_t, E>>(&self, f: F) -> Result<Self, E> {
                            let mut result = self.clone();
                            result.$auto_field = f(result.$auto_field)?;
                            Ok(result)
                        }
                    }
                )*

                $(
                    impl $crate::Provide<$t> for $name {
                        fn provide(&self) -> $t {
                            self.$field.clone()
                        }
                    }
                    impl $crate::ProvideWith<$t> for $name {
                        fn provide_with<E, F: FnOnce($t) -> Result<$t, E>>(&self, f: F) -> Result<Self, E> {
                            let mut result = self.clone();
                            result.$field = f(result.$field)?;
                            Ok(result)
                        }
                    }
                )*
            }]
        }
    };
    {
        $caller:tt
        $(auto_field = [{ $($auto_field:tt)* }])*
        $(field = [{ $($field:tt)* }])*
        rest = [{ $field_name:ident: $t:ty [ ($($f_args:ident),*) $factory:ty ], $($rest:tt)* }]
    } => {
        $crate::private_define_context! {
            $caller
            $(auto_field = [{ $($auto_field)* }])*
            auto_field = [{ $field_name, $t, $factory, ($($f_args,)*) }]
            $(field = [{ $($field)* }])*
            rest = [{ $($rest)* }]
        }
    };
    {
        $caller:tt
        $(auto_field = [{ $($auto_field:tt)* }])*
        $(field = [{ $($field:tt)* }])*
        rest = [{ $field_name:ident: $t:ty [ ($($f_args:ident),*) $factory:ty ] }]
    } => {
        $crate::private_define_context! {
            $caller
            $(auto_field = [{ $($auto_field)* }])*
            auto_field = [{ $field_name, $t, $factory, ($($f_args,)*) }]
            $(field = [{ $($field)* }])*
            rest = [{ }]
        }
    };
    {
        $caller:tt
        $(auto_field = [{ $($auto_field:tt)* }])*
        $(field = [{ $($field:tt)* }])*
        rest = [{ $field_name:ident: $t:ty [ $factory:ty ], $($rest:tt)* }]
    } => {
        $crate::private_define_context! {
            $caller
            $(auto_field = [{ $($auto_field)* }])*
            auto_field = [{ $field_name, $t, $factory, () }]
            $(field = [{ $($field)* }])*
            rest = [{ $($rest)* }]
        }
    };
    {
        $caller:tt
        $(auto_field = [{ $($auto_field:tt)* }])*
        $(field = [{ $($field:tt)* }])*
        rest = [{ $field_name:ident: $t:ty [ $factory:ty ] }]
    } => {
        $crate::private_define_context! {
            $caller
            $(auto_field = [{ $($auto_field)* }])*
            auto_field = [{ $field_name, $t, $factory, () }]
            $(field = [{ $($field)* }])*
            rest = [{ }]
        }
    };
    {
        $caller:tt
        $(auto_field = [{ $($auto_field:tt)* }])*
        $(field = [{ $($field:tt)* }])*
        rest = [{ $field_name:ident: $t:ty, $($rest:tt)* }]
    } => {
        $crate::private_define_context! {
            $caller
            $(auto_field = [{ $($auto_field)* }])*
            $(field = [{ $($field)* }])*
            field = [{ $field_name, $t }]
            rest = [{ $($rest)* }]
        }
    };
    {
        $caller:tt
        $(auto_field = [{ $($auto_field:tt)* }])*
        $(field = [{ $($field:tt)* }])*
        rest = [{ $field_name:ident: $t:ty }]
    } => {
        $crate::private_define_context! {
            $caller
            $(auto_field = [{ $($auto_field)* }])*
            $(field = [{ $($field)* }])*
            field = [{ $field_name, $t }]
            rest = [{ }]
        }
    };
    {
        $caller:tt
        $(auto_field = [{ $($auto_field:tt)* }])*
        $(field = [{ $($field:tt)* }])*
        rest = [{ }]
    } => {
        $crate::tt_call::tt_return! {
            $caller
            $(auto_field = [{ $($auto_field)* }])*
            $(field = [{ $($field)* }])*
        }
    };
}

/// Define a new context. Typically used at the top level of an
/// application to contain the full set of requried dependencies.
///
/// Contexts follow a struct-like syntax, although the names of
/// fields are for the most part unimportant.
///
/// Contexts automatically implement all applicable interfaces.
/// An interface is applicable if all of the dependencies
/// required by that interface are present in the context.
///
/// Dependencies are identified by *type*, not by the field name.
/// Contexts may not contain two fields of the same type. Instead
/// use new-type wrappers to distinguish similar dependencies.
///
/// Types used in a context must implement `Clone + Debug`, and
/// `Clone` should be a cheap operation. For this reason it is usual
/// to wrap dependencies in an `Rc` or `Arc`.
///
/// A constructor function will be automatically implemented
/// for contexts, with one parameter for each dependency, to be
/// provided in the same order as when the context is defined.
///
/// ## Example
///
/// ```
/// use std::sync::Arc;
///
/// #[derive(Debug)]
/// struct Foo;
/// #[derive(Debug)]
/// struct Bar;
///
/// aerosol::define_context!(
///     TestContext {
///         foo: Arc<Foo>,
///         bar: Arc<Bar>,
///     }
/// );
///
/// fn main() {
///     TestContext::new(
///         Arc::new(Foo),
///         Arc::new(Bar),
///     );
/// }
/// ```
///
/// It is also possible to define a factory type to enable
/// dependencies to be automatically created.
///
/// When a factory is specified for a dependency, it will be
/// omitted from the parameter list required by the context's
/// constructor. Instead, the constructor will call the `build`
/// method on the specified factory.
///
/// To conditionally use a factory, or use different factories
/// for the same dependency, define separate contexts, or
/// call the factory manually and pass the result to the
/// context's constructor in the normal way.
///
/// ## Example
///
/// ```
/// use std::sync::Arc;
///
/// #[derive(Debug)]
/// struct Foo;
/// #[derive(Debug)]
/// struct Bar;
///
/// struct FooFactory;
/// impl aerosol::Factory for FooFactory {
///     type Object = Arc<Foo>;
///     fn build(_: ()) -> Result<Arc<Foo>, anyhow::Error> { Ok(Arc::new(Foo)) }
/// }
///
/// aerosol::define_context!(
///     TestContext {
///         foo: Arc<Foo> [FooFactory],
///         bar: Arc<Bar>,
///     }
/// );
///
/// fn main() {
///     TestContext::new(
///         Arc::new(Bar),
///     );
/// }
/// ```
///
///
#[macro_export]
macro_rules! define_context {
    ($($input:tt)*) => (
        $crate::tt_call::tt_call! {
            macro = [{ $crate::private_define_context }]
            input = [{ $($input)* }]
        }
    );
}