Skip to main content

sva_cli/
output.rs

1// Concern: builds this CLI's subcommand JSON objects | Non-concern: the envelope they are answered in, and the query data (sva-core) | IO: (Output, fields) -> a JSON string
2
3use sva_core::json::{NONE, escape, list, num, pair_list, strings};
4
5/// `target: null` is a whole-directory lint, the one mode running the entry-point check.
6pub fn lint_data(dir: &str, target: Option<&str>, nodes: usize) -> String {
7    let target = target.map_or_else(|| NONE.to_string(), |t| format!("\"{}\"", escape(t)));
8    format!(
9        "{{\n  \"dir\": \"{}\",\n  \"target\": {target},\n  \"nodes\": {nodes}\n  }}",
10        escape(dir)
11    )
12}
13
14/// Every key a trace declares is written, `sva_core::NONE` where the node has none.
15pub fn trace_data(t: &crate::Traceable) -> String {
16    let d = &t.traced;
17    let text = |held: Option<&String>| {
18        held.map_or_else(|| NONE.to_string(), |held| format!("\"{}\"", escape(held)))
19    };
20    format!(
21        "{{\n  \"node\": \"{}\",\n  \"expr\": \"{}\",\n  \"ty\": \"{}\",\n  \
22         \"discrete\": {},\n  \"bpm\": {},\n  \"meter\": {},\n  \"entry\": {},\n  \
23         \"file\": {},\n  \"loop\": {},\n  \"down\": {},\n  \"up\": {}\n}}",
24        escape(&d.node),
25        escape(&d.expr),
26        escape(&d.ty),
27        text(d.discrete.as_ref()),
28        t.bpm.map_or_else(|| NONE.to_string(), num),
29        text(t.meter.as_ref()),
30        strings(&d.entry),
31        text(d.file.as_ref()),
32        d.cycle.as_ref().map_or_else(
33            || NONE.to_string(),
34            |members| format!("{{ \"order\": {} }}", strings(members))
35        ),
36        strings(&d.down),
37        list(&d.up, |u| {
38            format!(
39                "\n    {{ \"node\": \"{}\", \"expr\": \"{}\", \"ty\": \"{}\" }}",
40                escape(&u.node),
41                escape(&u.expr),
42                escape(&u.ty)
43            )
44        })
45    )
46}
47
48pub fn new_data(s: &crate::Scaffolded) -> String {
49    format!(
50        "{{ \"root\": \"{}\", \"files\": {}, \"next\": {} }}",
51        escape(&s.root.display().to_string()),
52        strings(&s.files),
53        pair_list(&crate::NEXT, "command", "why")
54    )
55}
56
57pub fn help_data(page: &str) -> String {
58    format!("{{ \"help\": \"{}\" }}", escape(page))
59}
60
61pub fn version_data(name: &str, version: &str) -> String {
62    format!(
63        "{{ \"name\": \"{}\", \"version\": \"{}\" }}",
64        escape(name),
65        escape(version)
66    )
67}