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 lint of every file under its own rules, which decides no render: its
6/// interval is `null`.
7pub fn lint_data(
8    dir: &str,
9    target: Option<&str>,
10    nodes: usize,
11    interval: Option<(f64, f64)>,
12) -> String {
13    let target = target.map_or_else(|| NONE.to_string(), |t| format!("\"{}\"", escape(t)));
14    let interval = interval.map_or(NONE.to_string(), |(start, end)| {
15        format!(
16            "{{ \"start_secs\": {}, \"end_secs\": {} }}",
17            num(start),
18            num(end)
19        )
20    });
21    format!(
22        "{{\n  \"dir\": \"{}\",\n  \"target\": {target},\n  \"nodes\": {nodes},\n  \
23         \"interval\": {interval}\n  }}",
24        escape(dir)
25    )
26}
27
28/// Every key a trace declares is written, `sva_core::NONE` where the node has none.
29pub fn trace_data(t: &crate::Traceable) -> String {
30    let d = &t.traced;
31    let text = |held: Option<&String>| {
32        held.map_or_else(|| NONE.to_string(), |held| format!("\"{}\"", escape(held)))
33    };
34    format!(
35        "{{\n  \"node\": \"{}\",\n  \"expr\": \"{}\",\n  \"ty\": \"{}\",\n  \
36         \"discrete\": {},\n  \"bpm\": {},\n  \"meter\": {},\n  \"entry\": {},\n  \
37         \"file\": {},\n  \"loop\": {},\n  \"down\": {},\n  \"up\": {}\n}}",
38        escape(&d.node),
39        escape(&d.expr),
40        escape(&d.ty),
41        text(d.discrete.as_ref()),
42        t.bpm.map_or_else(|| NONE.to_string(), num),
43        text(t.meter.as_ref()),
44        strings(&d.entry),
45        text(d.file.as_ref()),
46        d.cycle.as_ref().map_or_else(
47            || NONE.to_string(),
48            |members| format!("{{ \"order\": {} }}", strings(members))
49        ),
50        strings(&d.down),
51        list(&d.up, |u| {
52            format!(
53                "\n    {{ \"node\": \"{}\", \"expr\": \"{}\", \"ty\": \"{}\" }}",
54                escape(&u.node),
55                escape(&u.expr),
56                escape(&u.ty)
57            )
58        })
59    )
60}
61
62pub fn new_data(s: &crate::Scaffolded) -> String {
63    format!(
64        "{{ \"root\": \"{}\", \"files\": {}, \"next\": {} }}",
65        escape(&s.root.display().to_string()),
66        strings(&s.files),
67        pair_list(&crate::NEXT, "command", "why")
68    )
69}
70
71pub fn help_data(page: &str) -> String {
72    format!("{{ \"help\": \"{}\" }}", escape(page))
73}
74
75pub fn version_data(name: &str, version: &str) -> String {
76    format!(
77        "{{ \"name\": \"{}\", \"version\": \"{}\" }}",
78        escape(name),
79        escape(version)
80    )
81}