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
//! The `introspect --json` built-in: a [`GraphReport`] as JSON, for tools
//! that look at an app from outside (Navigator, `navree mcp`).
use std::fmt::Write;
use crate::GraphReport;
use crate::command::BUILT_INS;
impl GraphReport {
/// The report as one JSON object, what `introspect --json` prints:
///
/// ```json
/// {
/// "navree": "1.0.0",
/// "modules": [
/// {"name": "DealsModule", "global": false, "root": false,
/// "imports": ["DatabaseModule"], "exports": ["crm::DealService"],
/// "providers": [{"type": "crm::DealService", "scope": "singleton",
/// "exported": true, "multi": false, "dependencies": ["navree_db::Db"]}],
/// "commands": [{"name": "crm:close", "about": "Close stale deals"}]}
/// ],
/// "commands": [{"name": "check", "about": "...", "module": null}, ...]
/// }
/// ```
///
/// Modules come in dependency order (imports first); the implicit app
/// root, which imports the app's root modules, is last with `"root":
/// true`. `commands` lists the built-ins (`module: null`), then every
/// module's commands. Nothing is constructed to produce it.
pub fn to_json(&self) -> String {
let mut out = String::from("{\"navree\":");
string(&mut out, env!("CARGO_PKG_VERSION"));
out.push_str(",\"modules\":[");
let root = self.modules.len().saturating_sub(1);
for (i, module) in self.modules.iter().enumerate() {
if i > 0 {
out.push(',');
}
out.push_str("{\"name\":");
string(&mut out, module.name);
let _ = write!(
out,
",\"global\":{},\"root\":{},\"imports\":",
module.global,
i == root
);
strings(&mut out, module.imports.iter().copied());
out.push_str(",\"exports\":");
strings(&mut out, module.exports.iter().map(String::as_str));
out.push_str(",\"providers\":[");
for (j, provider) in module.providers.iter().enumerate() {
if j > 0 {
out.push(',');
}
out.push_str("{\"type\":");
string(&mut out, &provider.key);
let _ = write!(
out,
",\"scope\":\"{}\",\"exported\":{},\"multi\":{},\"dependencies\":",
provider.scope,
module.exports.contains(&provider.key),
provider.multi
);
strings(&mut out, provider.dependencies.iter().map(String::as_str));
out.push('}');
}
out.push_str("],\"commands\":[");
for (j, command) in module.commands.iter().enumerate() {
if j > 0 {
out.push(',');
}
command_object(&mut out, command.name, command.about, None);
}
out.push_str("]}");
}
out.push_str("],\"commands\":[");
let built_ins = BUILT_INS.iter().map(|(usage, about)| {
let name = usage.split_whitespace().next().unwrap_or(usage);
(name, *about, None)
});
let registered = self.modules.iter().flat_map(|m| {
m.commands
.iter()
.map(move |c| (c.name, c.about, Some(m.name)))
});
for (i, (name, about, module)) in built_ins.chain(registered).enumerate() {
if i > 0 {
out.push(',');
}
command_object(&mut out, name, about, Some(module));
}
out.push_str("]}");
out
}
}
/// `{"name":..,"about":..}`, plus `"module"` when `module` is given.
fn command_object(out: &mut String, name: &str, about: &str, module: Option<Option<&str>>) {
out.push_str("{\"name\":");
string(out, name);
out.push_str(",\"about\":");
string(out, about);
if let Some(module) = module {
out.push_str(",\"module\":");
match module {
Some(module) => string(out, module),
None => out.push_str("null"),
}
}
out.push('}');
}
/// A JSON array of strings.
fn strings<'a>(out: &mut String, items: impl Iterator<Item = &'a str>) {
out.push('[');
for (i, item) in items.enumerate() {
if i > 0 {
out.push(',');
}
string(out, item);
}
out.push(']');
}
/// A JSON string literal.
fn string(out: &mut String, s: &str) {
out.push('"');
for c in s.chars() {
match c {
'"' => out.push_str("\\\""),
'\\' => out.push_str("\\\\"),
'\n' => out.push_str("\\n"),
'\r' => out.push_str("\\r"),
'\t' => out.push_str("\\t"),
c if u32::from(c) < 0x20 => {
let _ = write!(out, "\\u{:04x}", u32::from(c));
}
c => out.push(c),
}
}
out.push('"');
}