use std::marker::PhantomData;
use crate::builder::PassedModule;
use crate::{BoxFuture, KernelError, ModuleBuilder, ModuleId, ProviderKey, Resolver, Scope};
pub trait Module: Send + Sync + 'static {
fn id(&self) -> ModuleId {
ModuleId::of::<Self>()
}
fn name(&self) -> &'static str {
self.id().name()
}
fn is_configured(&self) -> bool {
false
}
fn register(&self, m: &mut ModuleBuilder);
#[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)
}
}
pub(crate) fn resolved_type_name(module: &dyn Module) -> &'static str {
module.__module_type(sealed::Witness::new()).type_name
}
pub struct ConfiguredModule<M, O> {
options: Option<O>,
register: fn(&mut ModuleBuilder, Option<O>),
module: PhantomData<fn() -> M>,
}
impl<M, O> ConfiguredModule<M, O> {
pub fn for_root(options: O, register: fn(&mut ModuleBuilder, Option<O>)) -> Self {
Self {
options: Some(options),
register,
module: PhantomData,
}
}
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());
}
}
#[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 {
pub struct PlainModule;
impl PlainModule {
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};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct CodeConfigured {
module: &'static str,
}
impl CodeConfigured {
pub fn module(&self) -> &'static str {
self.module
}
}
pub trait Injectable: Sized + Send + Sync + 'static {
fn scope() -> Scope {
Scope::Singleton
}
fn dependencies() -> Vec<ProviderKey> {
Vec::new()
}
fn construct(r: Resolver) -> BoxFuture<'static, Result<Self, KernelError>>;
}
pub trait IntoModules {
fn into_modules(self) -> Vec<Box<dyn Module>>;
#[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};
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
);
}
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:?}"
);
}
}