navree-kernel 1.4.0

Navree kernel: module graph, type-keyed DI container, lifecycle and command runner.
Documentation
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
//! The [`Module`] and [`Injectable`] traits, and [`ConfiguredModule`].

use std::marker::PhantomData;

use crate::builder::PassedModule;
use crate::{BoxFuture, KernelError, ModuleBuilder, ModuleId, ProviderKey, Resolver, Scope};

/// A unit of an app: it registers providers, exports, imports, contributions,
/// lifecycle hooks and commands through a [`ModuleBuilder`].
///
/// `register` is called once per [`ModuleId`] per boot. A configured instance
/// (`for_root(...)`, [`is_configured`](Module::is_configured) returns `true`)
/// wins over plain imports of the same module type.
pub trait Module: Send + Sync + 'static {
    /// The module identity. `for_root` wrappers override this to return the id
    /// of the module type they configure.
    fn id(&self) -> ModuleId {
        ModuleId::of::<Self>()
    }

    /// The readable module name used in errors and reports.
    fn name(&self) -> &'static str {
        self.id().name()
    }

    /// `for_root(...)` values return true; at most one configured instance per id.
    fn is_configured(&self) -> bool {
        false
    }

    /// Registers everything the module contributes.
    fn register(&self, m: &mut ModuleBuilder);

    /// The concrete type name of `self` when erased behind `dyn Module`.
    /// Sealed by an unnameable parameter so crates outside `navree-kernel`
    /// cannot override it. Not public API.
    #[doc(hidden)]
    #[allow(private_interfaces)]
    fn __module_type(&self, _: sealed::Witness<'_>) -> sealed::ModuleType {
        sealed::ModuleType {
            type_name: std::any::type_name::<Self>(),
        }
    }
}

mod sealed {
    use std::marker::PhantomData;

    #[derive(Clone, Copy)]
    pub struct Witness<'a>(PhantomData<fn(&'a ()) -> &'a ()>);

    impl Witness<'_> {
        pub(super) fn new() -> Self {
            Self(PhantomData)
        }
    }

    #[derive(Clone, Copy)]
    pub struct ModuleType {
        pub(super) type_name: &'static str,
    }
}

impl Module for Box<dyn Module> {
    fn id(&self) -> ModuleId {
        (**self).id()
    }

    fn name(&self) -> &'static str {
        (**self).name()
    }

    fn is_configured(&self) -> bool {
        (**self).is_configured()
    }

    fn register(&self, m: &mut ModuleBuilder) {
        (**self).register(m);
    }

    #[allow(private_interfaces)]
    fn __module_type(&self, witness: sealed::Witness<'_>) -> sealed::ModuleType {
        (**self).__module_type(witness)
    }
}

impl Module for std::pin::Pin<Box<dyn Module>> {
    fn id(&self) -> ModuleId {
        self.as_ref().get_ref().id()
    }

    fn name(&self) -> &'static str {
        self.as_ref().get_ref().name()
    }

    fn is_configured(&self) -> bool {
        self.as_ref().get_ref().is_configured()
    }

    fn register(&self, m: &mut ModuleBuilder) {
        self.as_ref().get_ref().register(m);
    }

    #[allow(private_interfaces)]
    fn __module_type(&self, witness: sealed::Witness<'_>) -> sealed::ModuleType {
        self.as_ref().get_ref().__module_type(witness)
    }
}

/// Unwraps erased `dyn Module` wrappers (`Box`, `Pin`) and returns the inner
/// value's `std::any::type_name`.
pub(crate) fn resolved_type_name(module: &dyn Module) -> &'static str {
    module.__module_type(sealed::Witness::new()).type_name
}

/// The configured form of module `M`: what `M::for_root(options)` and
/// `M::from_config()` return, so a module does not repeat the wrapper that
/// carries its id, says it is configured and hands its options to `register`.
///
/// `register` is the module's own function. It gets `Some(options)` from
/// [`for_root`](ConfiguredModule::for_root), and `None` from
/// [`from_config`](ConfiguredModule::from_config), which means "read the
/// section of the app's configuration" (a plain `M` registers with `None`
/// too).
///
/// A module of a plugin that is not one of the framework's own packages may
/// configure only modules whose type names the plugin's crate; it imports any
/// other module by type, and the app configures it.
///
/// ```
/// use navree_kernel::{ConfiguredModule, Module, ModuleBuilder, Navree};
///
/// #[derive(Clone)]
/// struct GreetingOptions(&'static str);
///
/// struct GreetingModule;
///
/// impl GreetingModule {
///     pub fn for_root(options: GreetingOptions) -> impl Module {
///         ConfiguredModule::<GreetingModule, _>::for_root(options, register)
///     }
/// }
///
/// impl Module for GreetingModule {
///     fn register(&self, m: &mut ModuleBuilder) {
///         register(m, None);
///     }
/// }
///
/// fn register(m: &mut ModuleBuilder, options: Option<GreetingOptions>) {
///     let greeting = options.map_or("hello", |options| options.0);
///     m.value(greeting.to_owned()).exports::<String>();
/// }
///
/// // The configured instance has the plain module's id.
/// let configured = GreetingModule::for_root(GreetingOptions("namaste"));
/// assert_eq!(configured.id(), GreetingModule.id());
/// assert!(configured.is_configured());
/// let report = Navree::app().module(configured).validate().expect("valid");
/// // The app's root module and GreetingModule.
/// assert_eq!(report.modules.len(), 2);
/// ```
pub struct ConfiguredModule<M, O> {
    options: Option<O>,
    register: fn(&mut ModuleBuilder, Option<O>),
    module: PhantomData<fn() -> M>,
}

impl<M, O> ConfiguredModule<M, O> {
    /// `M` configured with `options` (configure a module once, in the app
    /// root).
    pub fn for_root(options: O, register: fn(&mut ModuleBuilder, Option<O>)) -> Self {
        Self {
            options: Some(options),
            register,
            module: PhantomData,
        }
    }

    /// `M` configured from the app's configuration: `register` gets `None`.
    pub fn from_config(register: fn(&mut ModuleBuilder, Option<O>)) -> Self {
        Self {
            options: None,
            register,
            module: PhantomData,
        }
    }
}

impl<M: 'static, O: Clone + Send + Sync + 'static> Module for ConfiguredModule<M, O> {
    fn id(&self) -> ModuleId {
        ModuleId::of::<M>()
    }

    fn is_configured(&self) -> bool {
        true
    }

    fn register(&self, m: &mut ModuleBuilder) {
        if self.options.is_some() {
            m.contribute(CodeConfigured {
                module: ModuleId::of::<M>().name(),
            });
        }
        (self.register)(m, self.options.clone());
    }
}

/// Expands, in the crate that calls it, to a configured-module wrapper type of
/// that crate with one private field holding [`ConfiguredModule`], two
/// crate-private constructors (`for_root(options)` and `from_config()`) bound
/// to the crate's own `register` function, and [`Module`] forwarding `id`,
/// `is_configured` and `register`.
///
/// A framework package that names itself a plugin uses this in place of
/// returning [`ConfiguredModule`] directly, so its configured forms have a type
/// of the package's own crate and no other crate can build the wrapper with
/// another `register` function. Not public API; Navree's own crates use it.
///
/// ```
/// use navree_kernel::{Module, ModuleBuilder, Navree, configured_module};
///
/// #[derive(Clone)]
/// struct GreetingOptions(&'static str);
///
/// struct GreetingModule;
///
/// impl GreetingModule {
///     pub fn for_root(options: GreetingOptions) -> impl Module {
///         ConfiguredGreetingModule::for_root(options)
///     }
///
///     pub fn from_config() -> impl Module {
///         ConfiguredGreetingModule::from_config()
///     }
/// }
///
/// impl Module for GreetingModule {
///     fn register(&self, m: &mut ModuleBuilder) {
///         register(m, None);
///     }
/// }
///
/// configured_module!(ConfiguredGreetingModule, GreetingModule, GreetingOptions, register);
///
/// fn register(m: &mut ModuleBuilder, options: Option<GreetingOptions>) {
///     let greeting = options.map_or("sat sri akal", |options| options.0);
///     m.value(greeting.to_owned()).exports::<String>();
/// }
///
/// let configured = GreetingModule::for_root(GreetingOptions("namaste"));
/// assert_eq!(configured.id(), GreetingModule.id());
/// assert!(configured.is_configured());
/// Navree::app().module(configured).validate().expect("valid");
/// ```
///
/// Outside the crate that wrote a configured form, neither its constructors
/// nor its private field can build one:
///
/// ```compile_fail
/// use navree_kernel::__private::ConfiguredPlainModule;
///
/// let _ = ConfiguredPlainModule::from_config();
/// ```
///
/// ```compile_fail
/// use navree_kernel::ConfiguredModule;
/// use navree_kernel::__private::{ConfiguredPlainModule, PlainModule};
///
/// let _ = ConfiguredPlainModule(ConfiguredModule::<PlainModule, ()>::from_config(|_, _| {}));
/// ```
#[doc(hidden)]
#[macro_export]
macro_rules! configured_module {
    (
        $(#[$meta:meta])*
        $vis:vis $name:ident,
        $module:ty,
        $options:ty,
        $register:expr $(,)?
    ) => {
        #[allow(missing_docs)]
        $(#[$meta])*
        $vis struct $name($crate::ConfiguredModule<$module, $options>);

        impl $name {
            #[allow(dead_code)]
            pub(crate) fn for_root(options: $options) -> Self {
                Self($crate::ConfiguredModule::for_root(options, $register))
            }

            #[allow(dead_code)]
            pub(crate) fn from_config() -> Self {
                Self($crate::ConfiguredModule::from_config($register))
            }
        }

        impl $crate::Module for $name {
            fn id(&self) -> $crate::ModuleId {
                $crate::Module::id(&self.0)
            }

            fn is_configured(&self) -> bool {
                $crate::Module::is_configured(&self.0)
            }

            fn register(&self, m: &mut $crate::ModuleBuilder) {
                $crate::Module::register(&self.0, m);
            }
        }
    };
}

mod sample {
    /// A module of the kernel that registers nothing, for the doctests of
    /// [`configured_module!`](crate::configured_module) and the plugin tests.
    /// Not public API.
    ///
    /// Outside `navree-kernel`, `Module::__module_type` cannot be overridden:
    ///
    /// ```compile_fail
    /// use navree_kernel::{Module, ModuleBuilder};
    ///
    /// struct OutsideModule;
    ///
    /// impl Module for OutsideModule {
    ///     fn register(&self, _: &mut ModuleBuilder) {}
    ///
    ///     fn __module_type(&self, _: _) -> _ {
    ///         unreachable!()
    ///     }
    /// }
    /// ```
    pub struct PlainModule;

    impl PlainModule {
        /// Configured form of [`PlainModule`], for plugin tests.
        pub fn from_config() -> ConfiguredPlainModule {
            ConfiguredPlainModule::from_config()
        }
    }

    impl crate::Module for PlainModule {
        fn register(&self, _: &mut crate::ModuleBuilder) {}
    }

    fn register(_: &mut crate::ModuleBuilder, _: Option<()>) {}

    crate::configured_module!(pub ConfiguredPlainModule, PlainModule, (), register);
}

pub use sample::{ConfiguredPlainModule, PlainModule};

/// Contributed by a module that was given its options in code
/// (`for_root(options)`), so tools can tell that the section of the
/// configuration it would otherwise read is not read. navree-config warns
/// when the configuration has such a section.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct CodeConfigured {
    module: &'static str,
}

impl CodeConfigured {
    /// The short name of the module (`CacheModule`).
    pub fn module(&self) -> &'static str {
        self.module
    }
}

/// A type the container can construct (what `#[injectable]` implements).
pub trait Injectable: Sized + Send + Sync + 'static {
    /// The provider scope; singletons by default.
    fn scope() -> Scope {
        Scope::Singleton
    }

    /// The keys `construct` resolves, used by the boot-time graph check.
    /// Keys resolved through [`Lazy`](crate::Lazy) are left out.
    fn dependencies() -> Vec<ProviderKey> {
        Vec::new()
    }

    /// Builds the value, resolving dependencies through `r`.
    fn construct(r: Resolver) -> BoxFuture<'static, Result<Self, KernelError>>;
}

/// Values accepted by [`ModuleBuilder::imports`]: a module, a tuple of up to
/// 16 modules, or a `Vec<Box<dyn Module>>`.
pub trait IntoModules {
    /// Converts into boxed modules, in order.
    fn into_modules(self) -> Vec<Box<dyn Module>>;

    /// Converts into modules, in order, each with the type of the value it was
    /// given as: a module given already boxed has the type `Box<dyn Module>`.
    /// The kernel reads a module's own type from it, and only a typed value
    /// makes a `PassedModule`. Not public API; the kernel calls it.
    #[doc(hidden)]
    fn into_passed_modules(self) -> Vec<PassedModule>
    where
        Self: Sized,
    {
        self.into_modules()
            .into_iter()
            .map(PassedModule::boxed)
            .collect()
    }
}

impl<M: Module> IntoModules for M {
    fn into_modules(self) -> Vec<Box<dyn Module>> {
        vec![Box::new(self)]
    }

    fn into_passed_modules(self) -> Vec<PassedModule> {
        vec![PassedModule::of(self)]
    }
}

impl IntoModules for Vec<Box<dyn Module>> {
    fn into_modules(self) -> Vec<Box<dyn Module>> {
        self
    }
}

macro_rules! tuple_into_modules {
    ($($name:ident),+) => {
        impl<$($name: Module),+> IntoModules for ($($name,)+) {
            #[allow(non_snake_case)]
            fn into_modules(self) -> Vec<Box<dyn Module>> {
                let ($($name,)+) = self;
                vec![$(Box::new($name) as Box<dyn Module>),+]
            }

            #[allow(non_snake_case)]
            fn into_passed_modules(self) -> Vec<PassedModule> {
                let ($($name,)+) = self;
                vec![$(PassedModule::of($name)),+]
            }
        }
    };
}

tuple_into_modules!(A);
tuple_into_modules!(A, B);
tuple_into_modules!(A, B, C);
tuple_into_modules!(A, B, C, D);
tuple_into_modules!(A, B, C, D, E);
tuple_into_modules!(A, B, C, D, E, F);
tuple_into_modules!(A, B, C, D, E, F, G);
tuple_into_modules!(A, B, C, D, E, F, G, H);
tuple_into_modules!(A, B, C, D, E, F, G, H, I);
tuple_into_modules!(A, B, C, D, E, F, G, H, I, J);
tuple_into_modules!(A, B, C, D, E, F, G, H, I, J, K);
tuple_into_modules!(A, B, C, D, E, F, G, H, I, J, K, L);
tuple_into_modules!(A, B, C, D, E, F, G, H, I, J, K, L, M);
tuple_into_modules!(A, B, C, D, E, F, G, H, I, J, K, L, M, N);
tuple_into_modules!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O);
tuple_into_modules!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P);

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Navree;
    use crate::graph::Graph;
    use crate::plugin::{FIRST_PARTY, PluginInfo, is_first_party};

    /// Stands for a framework package's module and its configured form written in that crate.
    mod navree_flags_probe {
        use crate::{Module, ModuleBuilder};

        #[derive(Clone)]
        pub struct FlagsOptions(pub &'static str);

        pub struct FlagsModule;

        impl FlagsModule {
            pub fn for_root(options: FlagsOptions) -> impl Module {
                ConfiguredFlagsModule::for_root(options)
            }

            pub fn from_config() -> impl Module {
                ConfiguredFlagsModule::from_config()
            }
        }

        impl Module for FlagsModule {
            fn register(&self, m: &mut ModuleBuilder) {
                register(m, None);
            }
        }

        crate::configured_module!(ConfiguredFlagsModule, FlagsModule, FlagsOptions, register);

        fn register(m: &mut ModuleBuilder, options: Option<FlagsOptions>) {
            let city = options.map_or("from_config", |options| options.0);
            m.contribute(city);
        }
    }

    #[test]
    fn a_configured_form_written_in_a_crate_is_a_type_of_that_crate_with_the_modules_id() {
        use navree_flags_probe::{FlagsModule, FlagsOptions};

        let plain_id = ModuleId::of::<FlagsModule>();
        let krate = concat!(module_path!(), "::navree_flags_probe");
        let prefix = format!("{krate}::");

        let for_root = FlagsModule::for_root(FlagsOptions("Ludhiana"));
        assert_eq!(for_root.id(), plain_id);
        assert_eq!(for_root.name(), "FlagsModule");
        assert!(for_root.is_configured());
        let passed_root = PassedModule::of(for_root);
        assert!(
            passed_root.type_name.starts_with(prefix.as_str()),
            "{}",
            passed_root.type_name
        );
        assert!(
            !passed_root.type_name.contains('<'),
            "{}",
            passed_root.type_name
        );

        let graph = Graph::build_passed(&[passed_root], &[]).expect("valid");
        let m = graph.module_of(plain_id).expect("in the graph");
        let code_configured: Vec<&str> = graph.modules[m]
            .contributions
            .iter()
            .filter_map(|c| c.value.downcast_ref::<CodeConfigured>())
            .map(|c| c.module())
            .collect();
        assert_eq!(code_configured, ["FlagsModule"]);
        let cities: Vec<&str> = graph.modules[m]
            .contributions
            .iter()
            .filter_map(|c| c.value.downcast_ref::<&'static str>().copied())
            .collect();
        assert_eq!(cities, ["Ludhiana"]);

        let from_config = FlagsModule::from_config();
        assert_eq!(from_config.id(), plain_id);
        assert!(from_config.is_configured());
        let passed_config = PassedModule::of(from_config);
        assert!(
            passed_config.type_name.starts_with(prefix.as_str()),
            "{}",
            passed_config.type_name
        );
        let listed = [("navree-flags", krate)];
        let info = PluginInfo::new("navree-flags", krate, "flags", "1.4.0");
        assert!(is_first_party(
            &info,
            &[passed_config.type_name],
            &listed,
            "1.4.0"
        ));

        let graph = Graph::build_passed(&[passed_config], &[]).expect("valid");
        let m = graph.module_of(plain_id).expect("in the graph");
        let has_code_configured = graph.modules[m]
            .contributions
            .iter()
            .any(|c| c.value.downcast_ref::<CodeConfigured>().is_some());
        assert!(!has_code_configured);
        let cities: Vec<&str> = graph.modules[m]
            .contributions
            .iter()
            .filter_map(|c| c.value.downcast_ref::<&'static str>().copied())
            .collect();
        assert_eq!(cities, ["from_config"]);
    }

    #[test]
    fn the_kernels_own_configured_wrapper_never_makes_a_plugin_first_party() {
        use navree_flags_probe::{FlagsModule, FlagsOptions};

        let version = env!("CARGO_PKG_VERSION");
        let krate = concat!(module_path!(), "::navree_flags_probe");
        let listed = [("navree-flags", krate)];
        let info = PluginInfo::new("navree-flags", krate, "flags", version);

        let for_root = PassedModule::of(ConfiguredModule::<FlagsModule, FlagsOptions>::for_root(
            FlagsOptions("Ludhiana"),
            |_, _| {},
        ));
        let from_config = PassedModule::of(
            ConfiguredModule::<FlagsModule, FlagsOptions>::from_config(|_, _| {}),
        );
        for wrapped in [&for_root, &from_config] {
            assert!(
                !is_first_party(&info, &[wrapped.type_name], &listed, version),
                "{}",
                wrapped.type_name
            );
        }

        // Even when the module and options inside `ConfiguredModule` are types of the listed
        // first-party crate, the wrapper's own outer type is the kernel's: not first-party, and
        // refused in the reserved namespace.
        let flags = PluginInfo::new("navree-flags", "navree_flags", "flags", version);
        let kernel_wrapped = "navree_kernel::module::ConfiguredModule<navree_flags::module::FlagsModule, navree_flags::options::FlagsOptions>";
        assert!(!is_first_party(
            &flags,
            &[kernel_wrapped],
            FIRST_PARTY,
            version
        ));

        let mut graph =
            Graph::build_passed(&[PassedModule::of(PlainModule)], &[]).expect("a plain module");
        let m = graph
            .module_of(ModuleId::of::<PlainModule>())
            .expect("in the graph");
        graph.modules[m].type_name = kernel_wrapped;
        graph.modules[m].plugin = Some(flags);
        let mut errors = Vec::new();
        graph.check_plugins(&mut errors);
        assert!(
            matches!(
                errors.as_slice(),
                [KernelError::NamespaceReserved {
                    plugin: "navree-flags",
                    namespace: "flags",
                }]
            ),
            "{errors:?}"
        );
        assert!(!graph.first_party.contains("navree-flags"));

        let claimed = ConfiguredModule::<FlagsModule, ()>::from_config(|m, _| {
            m.plugin(PluginInfo::new(
                "navree-flags",
                "navree_kernel",
                "flags",
                env!("CARGO_PKG_VERSION"),
            ));
        });
        let refused = Navree::app()
            .module(claimed)
            .validate()
            .expect_err("the kernel's own wrapper is never first-party");
        assert!(
            matches!(
                refused.0.as_slice(),
                [KernelError::NamespaceReserved {
                    plugin: "navree-flags",
                    namespace: "flags",
                }]
            ),
            "{refused:?}"
        );
    }
}