use crate::{AppContext, ModuleId, PluginInfo};
pub fn imports(ctx: &AppContext, importer: ModuleId, imported: ModuleId) -> bool {
let graph = &ctx.inner.graph;
let (Some(from), Some(target)) = (graph.module_of(importer), graph.module_of(imported)) else {
return false;
};
if from == target {
return false;
}
let mut visited = vec![false; graph.modules.len()];
visited[from] = true;
let mut pending = vec![from];
while let Some(module) = pending.pop() {
for &next in &graph.modules[module].imports {
if next == target {
return true;
}
if !visited[next] {
visited[next] = true;
pending.push(next);
}
}
}
false
}
pub fn is_community_plugin(ctx: &AppContext, module: ModuleId) -> bool {
ctx.plugin(module)
.is_some_and(|info| !ctx.inner.graph.first_party.contains(info.name))
}
pub fn acts_for_community_plugin(ctx: &AppContext, module: ModuleId) -> bool {
acts_for(ctx, module).is_some_and(|info| !ctx.inner.graph.first_party.contains(info.name))
}
pub fn acts_for(ctx: &AppContext, module: ModuleId) -> Option<&PluginInfo> {
let graph = &ctx.inner.graph;
let first = graph
.acts_for
.get(graph.module_of(module)?)
.copied()
.flatten()?;
graph.modules[first].plugin.as_ref()
}
pub fn acts_for_reason(ctx: &AppContext, module: ModuleId) -> String {
let graph = &ctx.inner.graph;
let Some(index) = graph.module_of(module) else {
return String::new();
};
let Some(first) = graph.acts_for.get(index).copied().flatten() else {
return String::new();
};
let Some(info) = graph.modules[first].plugin.as_ref() else {
return String::new();
};
graph.acts_for_reason(index, info)
}
pub fn is_app_module(ctx: &AppContext, module: ModuleId) -> bool {
let graph = &ctx.inner.graph;
graph
.module_of(module)
.is_some_and(|index| graph.acts_for.get(index).copied().flatten().is_none())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{CommandSpec, Module, ModuleBuilder, Navree};
mod navree_plugin_khata {
use crate::{CommandSpec, Module, ModuleBuilder, PluginInfo};
pub const KHATA: PluginInfo =
PluginInfo::new("navree-plugin-khata", module_path!(), "khata", "1.0.0");
pub struct KhataModule(pub fn(&mut ModuleBuilder));
impl Module for KhataModule {
fn register(&self, m: &mut ModuleBuilder) {
m.plugin(KHATA);
(self.0)(m);
}
}
pub struct KhataHelper(pub Option<&'static str>);
impl Module for KhataHelper {
fn register(&self, m: &mut ModuleBuilder) {
if let Some(name) = self.0 {
m.command(CommandSpec::new(name, "Helper", |_, _| async { Ok(()) }));
}
}
}
}
mod navree_plugin_khata_core {
use std::marker::PhantomData;
use crate::{CommandSpec, Module, ModuleBuilder};
pub struct KhataGeneric<T>(pub Option<&'static str>, pub PhantomData<T>);
impl<T: Send + Sync + 'static> Module for KhataGeneric<T> {
fn register(&self, m: &mut ModuleBuilder) {
if let Some(name) = self.0 {
m.command(CommandSpec::new(name, "Core", |_, _| async { Ok(()) }));
}
}
}
}
struct AppModule;
impl Module for AppModule {
fn register(&self, _: &mut ModuleBuilder) {}
}
struct Unwired;
#[tokio::test]
async fn the_reason_a_module_acts_for_a_plugin_is_the_clause_the_kernels_refusals_give() {
use std::marker::PhantomData;
use navree_plugin_khata::{KHATA, KhataHelper, KhataModule};
use navree_plugin_khata_core::KhataGeneric;
let app = Navree::app()
.module(KhataModule(|m| {
m.import(KhataHelper(None))
.import(KhataGeneric::<KhataModule>(None, PhantomData));
}))
.module(AppModule)
.boot()
.await
.expect("boots");
let ctx = app.ctx();
let naming = ModuleId::of::<KhataModule>();
let helper = ModuleId::of::<KhataHelper>();
let second = ModuleId::of::<KhataGeneric<KhataModule>>();
let app_mod = ModuleId::of::<AppModule>();
let missing = ModuleId::of::<Unwired>();
assert_eq!(acts_for_reason(ctx, naming), "");
assert_eq!(
acts_for_reason(ctx, helper),
format!(" (a module of the plugin's crate `{}`)", KHATA.crate_name)
);
assert_eq!(
acts_for_reason(ctx, second),
" (only the plugin's modules import it)"
);
assert_eq!(acts_for_reason(ctx, app_mod), "");
assert_eq!(acts_for_reason(ctx, missing), "");
assert!(acts_for_community_plugin(ctx, naming));
assert!(acts_for_community_plugin(ctx, helper));
assert!(acts_for_community_plugin(ctx, second));
assert!(!is_community_plugin(ctx, helper));
assert!(!is_community_plugin(ctx, second));
assert!(!acts_for_community_plugin(ctx, app_mod));
assert!(!acts_for_community_plugin(ctx, missing));
let direct = Navree::app()
.module(KhataModule(|m| {
m.command(CommandSpec::new("check", "Plugin", |_, _| async { Ok(()) }));
}))
.validate()
.expect_err("refused")
.to_string();
assert!(
direct.contains(&format!(
"in `KhataModule`{},",
acts_for_reason(ctx, naming)
)),
"{direct}"
);
let in_crate = Navree::app()
.module(KhataModule(|m| {
m.import(KhataHelper(Some("check")));
}))
.validate()
.expect_err("refused")
.to_string();
assert!(
in_crate.contains(&format!(
"in `KhataHelper`{},",
acts_for_reason(ctx, helper)
)),
"{in_crate}"
);
let by_import = Navree::app()
.module(KhataModule(|m| {
m.import(KhataGeneric::<KhataModule>(Some("check"), PhantomData));
}))
.validate()
.expect_err("refused")
.to_string();
assert!(
by_import.contains(&format!(
"in `{}`{},",
second.name(),
acts_for_reason(ctx, second)
)),
"{by_import}"
);
}
}