use std::fmt::{self, Write};
use crate::introspect::{string, strings};
use crate::key::short_type_name;
use crate::{ModuleId, PluginInfo, PublishTag};
#[derive(Clone, Debug, Default)]
#[non_exhaustive]
pub struct PluginsReport {
pub plugins: Vec<PluginReport>,
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct PluginReport {
pub info: PluginInfo,
pub first_party: bool,
pub modules: Vec<PluginModuleReport>,
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct PluginModuleReport {
pub id: ModuleId,
pub commands: Vec<&'static str>,
pub contributions: Vec<(&'static str, usize)>,
pub publishable: Vec<(PublishTag, usize)>,
}
impl PluginsReport {
pub fn to_json(&self) -> String {
let mut out = String::from("{\"navree\":");
string(&mut out, env!("CARGO_PKG_VERSION"));
out.push_str(",\"plugins\":");
self.write_plugins_array(&mut out);
out.push('}');
out
}
pub(crate) fn write_plugins_array(&self, out: &mut String) {
out.push('[');
for (i, plugin) in self.plugins.iter().enumerate() {
if i > 0 {
out.push(',');
}
let info = &plugin.info;
out.push_str("{\"name\":");
string(out, info.name);
out.push_str(",\"namespace\":");
string(out, info.namespace);
out.push_str(",\"version\":");
string(out, info.version);
for (key, value) in [
("title", info.title),
("description", info.description),
("homepage", info.homepage),
("repository", info.repository),
("license", info.license),
] {
let _ = write!(out, ",\"{key}\":");
match value {
Some(value) => string(out, value),
None => out.push_str("null"),
}
}
let _ = write!(out, ",\"first_party\":{},\"modules\":[", plugin.first_party);
for (j, module) in plugin.modules.iter().enumerate() {
if j > 0 {
out.push(',');
}
out.push_str("{\"name\":");
string(out, module.id.name());
out.push_str(",\"type\":");
string(out, module.id.type_name());
out.push_str(",\"commands\":");
strings(out, module.commands.iter().copied());
out.push_str(",\"contributions\":{");
for (k, (name, count)) in module.contributions.iter().enumerate() {
if k > 0 {
out.push(',');
}
string(out, name);
let _ = write!(out, ":{count}");
}
out.push_str("},\"publishable\":{");
for (k, (tag, count)) in module.publishable.iter().enumerate() {
if k > 0 {
out.push(',');
}
string(out, tag.as_str());
let _ = write!(out, ":{count}");
}
out.push_str("}}");
}
out.push_str("]}");
}
out.push(']');
}
}
impl fmt::Display for PluginsReport {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.plugins.is_empty() {
return f.write_str("No plugins: no module of this app calls m.plugin(..).");
}
let mut lines: Vec<String> = Vec::new();
for (i, plugin) in self.plugins.iter().enumerate() {
if i > 0 {
lines.push(String::new());
}
let info = &plugin.info;
let mut head = format!("{} {} {}", info.namespace, info.name, info.version);
if let Some(title) = info.title {
head.push_str(" ");
head.push_str(title);
}
lines.push(head);
for module in &plugin.modules {
lines.push(format!(" {} {}", module.id.name(), module.id.type_name()));
if !module.commands.is_empty() {
lines.push(format!(" commands {}", module.commands.join(", ")));
}
if !module.contributions.is_empty() {
let mut adds: Vec<(&str, usize)> = module
.contributions
.iter()
.map(|&(name, count)| (short_type_name(name), count))
.collect();
adds.sort_by_key(|&(name, _)| name);
let adds: Vec<String> = adds
.iter()
.map(|(name, count)| format!("{count} {name}"))
.collect();
lines.push(format!(" adds {}", adds.join(", ")));
}
if !module.publishable.is_empty() {
let files: Vec<String> = module
.publishable
.iter()
.map(|(tag, count)| format!("{count} {}", tag.as_str()))
.collect();
lines.push(format!(" publishes {}", files.join(", ")));
}
}
}
f.write_str(&lines.join("\n"))
}
}
#[cfg(test)]
mod tests {
use super::*;
struct AnnouncementsModule;
struct WeatherModule;
fn announcements() -> PluginsReport {
let info = PluginInfo::new(
"navree-plugin-announcements",
"navree_plugin_announcements",
"announcements",
"1.0.0",
)
.title("Announcements")
.description("Notices for every member of a workspace, by Simran Kaur's team in Ludhiana.")
.repository("https://code.ludhiana.example/navree-plugin-announcements")
.license("MIT OR Apache-2.0");
let weather = PluginInfo::new(
"navree-plugin-weather",
"navree_plugin_weather",
"weather",
"0.4.0",
);
PluginsReport {
plugins: vec![
PluginReport {
info,
first_party: false,
modules: vec![PluginModuleReport {
id: ModuleId::of::<AnnouncementsModule>(),
commands: vec!["announcements:prune"],
contributions: vec![
("navree_config::registry::SectionRegistration", 1),
("navree_contracts::permissions::Permissions", 1),
("navree_http::controller::ControllerRegistration", 1),
("navree_kernel::plugin::Publishable", 2),
("navree_schema::migration::MigrationSet", 1),
],
publishable: vec![(PublishTag::Views, 3), (PublishTag::Locales, 2)],
}],
},
PluginReport {
info: weather,
first_party: false,
modules: vec![PluginModuleReport {
id: ModuleId::of::<WeatherModule>(),
commands: Vec::new(),
contributions: Vec::new(),
publishable: Vec::new(),
}],
},
],
}
}
#[test]
fn the_plugins_json_has_the_documented_shape() {
let mut report = announcements();
report.plugins.truncate(1);
let json: serde_json::Value = serde_json::from_str(&report.to_json()).expect("JSON");
assert_eq!(
json,
serde_json::json!({
"navree": env!("CARGO_PKG_VERSION"),
"plugins": [{
"name": "navree-plugin-announcements",
"namespace": "announcements",
"version": "1.0.0",
"title": "Announcements",
"description": "Notices for every member of a workspace, by Simran Kaur's team in Ludhiana.",
"homepage": null,
"repository": "https://code.ludhiana.example/navree-plugin-announcements",
"license": "MIT OR Apache-2.0",
"first_party": false,
"modules": [{
"name": "AnnouncementsModule",
"type": std::any::type_name::<AnnouncementsModule>(),
"commands": ["announcements:prune"],
"contributions": {
"navree_config::registry::SectionRegistration": 1,
"navree_contracts::permissions::Permissions": 1,
"navree_http::controller::ControllerRegistration": 1,
"navree_kernel::plugin::Publishable": 2,
"navree_schema::migration::MigrationSet": 1
},
"publishable": {"views": 3, "locales": 2}
}]
}]
})
);
}
#[test]
fn the_plugins_text_lists_commands_contributions_and_files() {
let expected = format!(
"announcements navree-plugin-announcements 1.0.0 Announcements\n\
\x20 AnnouncementsModule {}\n\
\x20 commands announcements:prune\n\
\x20 adds 1 ControllerRegistration, 1 MigrationSet, 1 Permissions, 2 Publishable, 1 SectionRegistration\n\
\x20 publishes 3 views, 2 locales\n\
\n\
weather navree-plugin-weather 0.4.0\n\
\x20 WeatherModule {}",
std::any::type_name::<AnnouncementsModule>(),
std::any::type_name::<WeatherModule>(),
);
assert_eq!(announcements().to_string(), expected);
assert_eq!(
PluginsReport::default().to_string(),
"No plugins: no module of this app calls m.plugin(..)."
);
}
}