sva-cli 0.4.0

The command-line front end for SVA
Documentation
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
// Concern: parses argv into the subcommands this CLI answers and their options | Non-concern: what a target names (sva-core) | IO: (argv) -> Command or CliError

use std::path::PathBuf;

use sva_core::{Asked, CliError, WindowEdge};

mod reading;

pub(crate) use reading::check_frame;
use reading::{analyze_args, render_args};

pub const USAGE: &str = "usage: sva-cli render [<node|expression>] [query options]\n       \
     sva-cli analyze <file.wav> [--as <representation>[=<destination>]]... [--from <time>] \
     [--to <time>] [--frame <secs>] [--peaks <n>]\n       \
     sva-cli lint [<node|expression>] [--in <dir>] [--format <json|text>]\n       \
     sva-cli trace <node|expression> [--in <dir>]\n       \
     sva-cli builtins\n       \
     sva-cli outline <expression>\n       \
     sva-cli new <name> [--idempotency-key <key>]\n\
     query options: [--in <dir>] [--node <path>] [--from <time>] [--to <time>] \
     [--as <representation>[=<destination>]]... [--frame <secs>] [--depth <n>] [--peaks <n>] \
     [--sample-rate <hz>] [--oversample <n>] [--flop-budget <n>] [--volatile <name>]... \
     [--no-cache] [--brief] [--skim] [--pcm16] [--confirm]\n\
     representations: lines atoms spectrum envelope derivative samples ledger pitch formants \
     stereo bands crest loudness alias bindings arguments flops\n\
     analyses (`analyze` only): onsets trajectory masking gain-reduction\n\
     --against <file.wav> is the second signal `--as masking` is read against\n\
     --sample-rate <hz> is the observation rate, legal with any --as\n\
     --node <path> names the instance a reading is taken of; required with `--as bindings`\n\
     --flop-budget <n> is the operation count the caller means to pay; the profile's own \
     budget refuses past it, and `--as flops` prints the tree that count came from\n\
     --volatile <name> declares a bound parameter the player is moving: every node that reads \
     it is rendered and kept in memory for this run only, never written to the cache, and \
     sounds the same as without the flag; a name the target binds nowhere is refused\n\
     --brief condenses `ledger` to the nodes that clipped\n\
     --skim condenses `ledger`'s fields to node/channel/rms/peak/clipped\n\
     --pcm16 quantizes a `.wav` destination to 16-bit PCM instead of 32-bit float\n\
     destinations: a `.wav` path takes `samples` as audio; any other path takes JSON; none \
     prints JSON to stdout\n\
     --in <dir> names the composition `render`, `lint` and `trace` read; without it they read \
     the current directory. `new` and `analyze` take none. Each <node|expression> names a node \
     path or an expression in the same expression grammar a node file's body holds. A file \
     holds structure besides that body -- `name = <expr>` defaults, a TSV grid, a bare meter \
     such as `4/4` -- and an argument is a body alone, so `4/4` there is a division.\n\
     verb aliases: `validate`=lint, `list`=builtins, `create`=new, `show`=trace. `render` \
     and `analyze` take a reading, which the standard's verb list has no word for, so they \
     keep their own names.";

/// Only the readings that are a pure function of a buffer; the rest need the graph behind it.
pub const ANALYZE_REPRESENTATIONS: [&str; 10] = [
    "samples",
    "spectrum",
    "envelope",
    "derivative",
    "pitch",
    "formants",
    "bands",
    "loudness",
    "crest",
    "stereo",
];

#[derive(Debug, PartialEq)]
pub struct RenderArgs {
    /// A node path or an expression; which one it is, only the graph can say.
    pub target: Option<String>,
    /// The instance a reading is taken of, where it is not the target itself.
    pub node: Option<String>,
    /// The composition directory `--in` named, where the caller named one.
    pub dir: Option<String>,
    pub cache: bool,
    pub sample_rate: Option<u32>,
    pub from: Option<WindowEdge>,
    pub to: Option<WindowEdge>,
    pub asked: Vec<Asked>,
    pub brief: bool,
    pub skim: bool,
    pub pcm16: bool,
    /// The caller said a destination that already holds a file may be replaced.
    pub confirm: bool,
    pub flop_budget: Option<u128>,
    pub volatile: Vec<String>,
}

#[derive(Debug, PartialEq)]
pub struct AnalyzeArgs {
    pub path: PathBuf,
    pub from: Option<WindowEdge>,
    pub to: Option<WindowEdge>,
    pub asked: Vec<Asked>,
    /// The readings `sva-analysis` answers, which no `Representation` names.
    pub analyses: Vec<(String, Option<PathBuf>)>,
    pub against: Option<PathBuf>,
    /// The caller said a destination that already holds a file may be replaced.
    pub confirm: bool,
}

#[derive(Debug, PartialEq)]
pub enum Command {
    Version,
    Help,
    Render(Box<RenderArgs>),
    Analyze(Box<AnalyzeArgs>),
    /// A node path or an expression, same grammar as render's target; `None` lints the whole
    /// directory.
    Lint {
        target: Option<String>,
        dir: Option<String>,
        format: Format,
    },
    Trace {
        target: String,
        dir: Option<String>,
    },
    Builtins,
    /// An expression's own parse tree; it reads no composition.
    Outline {
        text: String,
    },
    New {
        name: String,
        /// Present where the caller says this is a retry, per the `cli` standard's own
        /// requirement that a create verb be made idempotent.
        idempotency_key: Option<String>,
    },
}

/// How the diagnostics an evaluator answers are rendered. One set of objects, two renderings.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Format {
    #[default]
    Json,
    Text,
}

const STANDARD_VERBS: [(&str, &str); 4] = [
    ("validate", "lint"),
    ("list", "builtins"),
    ("create", "new"),
    ("show", "trace"),
];

fn canonical(subcommand: &str) -> &str {
    STANDARD_VERBS
        .iter()
        .find(|(alias, _)| *alias == subcommand)
        .map_or(subcommand, |(_, name)| *name)
}

/// A free function rather than a closure per parser: a closure would hold its loop's
/// iterator borrowed.
pub(crate) fn value<'a>(
    it: &mut impl Iterator<Item = &'a String>,
    name: &str,
) -> Result<String, CliError> {
    it.next()
        .cloned()
        .ok_or_else(|| CliError::Usage(format!("{name} needs a value\n{USAGE}")))
}

/// `--version`/`-V` and `--help` are global flags, recognized anywhere in argv (cli standard).
pub fn parse_args(argv: &[String]) -> Result<Command, CliError> {
    if argv.iter().any(|a| a == "--version" || a == "-V") {
        return Ok(Command::Version);
    }
    if argv.iter().any(|a| a == "--help") {
        return Ok(Command::Help);
    }

    let mut it = argv.iter();
    let subcommand = canonical(
        it.next()
            .ok_or_else(|| CliError::Usage(format!("no subcommand given\n{USAGE}")))?,
    );
    let rest = it.as_slice();
    match subcommand {
        "render" => render_args(rest),
        "analyze" => analyze_args(rest),
        "lint" => lint_args(rest),
        "trace" => trace_args(rest),
        "builtins" => builtins_args(rest),
        "outline" => outline_args(rest),
        "new" => new_args(rest),
        other => Err(CliError::Usage(format!(
            "unknown subcommand `{other}`\n{USAGE}"
        ))),
    }
}

fn builtins_args(rest: &[String]) -> Result<Command, CliError> {
    match rest.first() {
        None => Ok(Command::Builtins),
        Some(extra) => Err(CliError::Usage(format!(
            "builtins takes no arguments, not `{extra}`\n{USAGE}"
        ))),
    }
}

fn outline_args(rest: &[String]) -> Result<Command, CliError> {
    match rest {
        [text] => Ok(Command::Outline { text: text.clone() }),
        _ => Err(CliError::Usage(format!(
            "outline takes one <expression>, quoted as one argument\n{USAGE}"
        ))),
    }
}

fn new_args(rest: &[String]) -> Result<Command, CliError> {
    match rest {
        [name] if !name.starts_with("--") => Ok(Command::New {
            name: name.clone(),
            idempotency_key: None,
        }),
        [name, flag, key] if !name.starts_with("--") && flag == "--idempotency-key" => {
            Ok(Command::New {
                name: name.clone(),
                idempotency_key: Some(key.clone()),
            })
        }
        [] => Err(CliError::Usage(format!("missing <name>\n{USAGE}"))),
        _ => Err(CliError::Usage(format!(
            "new takes one <name> and an optional `--idempotency-key <key>`\n{USAGE}"
        ))),
    }
}

fn lint_args(rest: &[String]) -> Result<Command, CliError> {
    let (dir, rest) = composition_flag(rest)?;
    let mut it = rest.iter().peekable();
    let target = match it.peek() {
        Some(a) if !a.starts_with("--") => it.next().cloned(),
        _ => None,
    };
    let mut format = Format::default();
    while let Some(flag) = it.next() {
        match flag.as_str() {
            "--format" => {
                format = match it.next().map(String::as_str) {
                    Some("json") => Format::Json,
                    Some("text") => Format::Text,
                    other => {
                        return Err(CliError::Usage(format!(
                            "`--format` takes json or text, not `{}`\n{USAGE}",
                            other.unwrap_or("nothing")
                        )));
                    }
                };
            }
            extra => {
                return Err(CliError::Usage(format!(
                    "lint takes only [<node|expression>], `--in <dir>` and `--format \
                     <json|text>`, not `{extra}`\n{USAGE}"
                )));
            }
        }
    }
    Ok(Command::Lint {
        target,
        dir,
        format,
    })
}

/// `--in <dir>` names the composition, wherever in the tail it is written; without it, the
/// process's own directory is the one a subcommand reads.
pub(super) fn composition_flag(rest: &[String]) -> Result<(Option<String>, Vec<String>), CliError> {
    let mut dir = None;
    let mut kept = Vec::with_capacity(rest.len());
    let mut it = rest.iter();
    while let Some(arg) = it.next() {
        match arg.as_str() {
            "--in" => {
                dir = Some(
                    it.next()
                        .cloned()
                        .ok_or_else(|| CliError::Usage(format!("`--in` needs a path\n{USAGE}")))?,
                );
            }
            _ => kept.push(arg.clone()),
        }
    }
    Ok((dir, kept))
}

/// One positional and nothing else: a trace answers structure, which no option narrows —
/// the rate is an observation parameter and a trace takes no observation.
fn trace_args(rest: &[String]) -> Result<Command, CliError> {
    let (dir, rest) = composition_flag(rest)?;
    match rest.as_slice() {
        [target] if !target.starts_with("--") => Ok(Command::Trace {
            target: target.clone(),
            dir,
        }),
        [] => Err(CliError::Usage(format!(
            "missing <node|expression>\n{USAGE}"
        ))),
        _ => Err(CliError::Usage(format!(
            "trace takes one <node|expression>, `--in <dir>` and nothing else\n{USAGE}"
        ))),
    }
}

pub(crate) fn number(raw: &str, flag: &str) -> Result<f64, CliError> {
    match raw.parse::<f64>() {
        Ok(v) if v.is_finite() => Ok(v),
        _ => Err(CliError::Usage(format!(
            "{flag} needs a finite number, got `{raw}`\n{USAGE}"
        ))),
    }
}

pub(crate) fn positive(raw: &str, flag: &str) -> Result<f64, CliError> {
    match number(raw, flag)? {
        v if v > 0.0 => Ok(v),
        _ => Err(CliError::Usage(format!(
            "{flag} needs a positive number, got `{raw}`\n{USAGE}"
        ))),
    }
}

pub(crate) fn operations(raw: &str, flag: &str) -> Result<u128, CliError> {
    raw.parse::<u128>().map_err(|_| not_a_count(raw, flag))
}

pub(crate) fn count(raw: &str, flag: &str) -> Result<usize, CliError> {
    usize::try_from(operations(raw, flag)?).map_err(|_| not_a_count(raw, flag))
}

fn not_a_count(raw: &str, flag: &str) -> CliError {
    CliError::Usage(format!("{flag} needs a whole count, got `{raw}`\n{USAGE}"))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::Path;

    fn argv(parts: &[&str]) -> Vec<String> {
        parts.iter().map(|s| (*s).to_string()).collect()
    }

    fn rendered(parts: &[&str]) -> RenderArgs {
        match parse_args(&argv(parts)) {
            Ok(Command::Render(args)) => *args,
            other => panic!("expected a render, got {other:?}"),
        }
    }

    #[test]
    fn version_and_help_are_recognized_anywhere_in_argv() {
        assert_eq!(parse_args(&argv(&["--version"])).unwrap(), Command::Version);
        assert_eq!(parse_args(&argv(&["-V"])).unwrap(), Command::Version);
        assert_eq!(
            parse_args(&argv(&["lint", "--help"])).unwrap(),
            Command::Help
        );
    }

    #[test]
    fn each_standard_verb_alias_reaches_the_subcommand_it_names() {
        assert_eq!(
            parse_args(&argv(&["validate"])).unwrap(),
            Command::Lint {
                target: None,
                dir: None,
                format: Format::Json,
            }
        );
        assert_eq!(parse_args(&argv(&["list"])).unwrap(), Command::Builtins);
        assert_eq!(
            parse_args(&argv(&["show", "kick"])).unwrap(),
            Command::Trace {
                target: "kick".to_string(),
                dir: None,
            }
        );
        assert_eq!(
            parse_args(&argv(&["create", "song1"])).unwrap(),
            Command::New {
                name: "song1".to_string(),
                idempotency_key: None,
            }
        );
    }

    /// The rate is an observation parameter, so it is legal beside any `--as` and nowhere
    /// near a structural subcommand.
    #[test]
    fn the_sample_rate_is_an_observation_flag_render_takes_with_any_reading() {
        for name in ["lines", "atoms", "samples", "ledger"] {
            let args = rendered(&["render", "--as", name, "--sample-rate", "48000"]);
            assert_eq!(args.sample_rate, Some(48_000));
            assert_eq!(args.asked[0].name, name);
        }
        assert!(matches!(
            parse_args(&argv(&["trace", "kick", "--sample-rate", "48000"])),
            Err(CliError::Usage(_))
        ));
    }

    #[test]
    fn a_representation_that_left_the_language_names_what_replaced_it() {
        for (gone, write) in sva_core::RETIRED {
            let Err(CliError::Usage(message)) = parse_args(&argv(&["render", "--as", gone])) else {
                panic!("`{gone}` must refuse")
            };
            assert!(message.contains(write), "{message}");
        }
    }

    #[test]
    fn a_wav_destination_takes_samples_and_nothing_else() {
        let args = rendered(&["render", "--as", "samples=/tmp/out.wav", "--pcm16"]);
        assert!(args.pcm16);
        assert_eq!(
            args.asked[0].dest.as_deref(),
            Some(Path::new("/tmp/out.wav"))
        );
        assert!(matches!(
            parse_args(&argv(&["render", "--as", "ledger=/tmp/out.wav"])),
            Err(CliError::Usage(_))
        ));
    }

    #[test]
    fn render_needs_a_reading_and_bindings_needs_a_node() {
        assert!(matches!(
            parse_args(&argv(&["render"])),
            Err(CliError::Usage(_))
        ));
        assert!(matches!(
            parse_args(&argv(&["render", "--as", "bindings"])),
            Err(CliError::Usage(_))
        ));
        let args = rendered(&["render", "--as", "bindings", "--node", "kick"]);
        assert_eq!(args.node.as_deref(), Some("kick"));
    }

    #[test]
    fn volatile_repeats_once_per_parameter_and_needs_a_name() {
        let args = rendered(&["render", "--as", "samples", "--volatile", "cutoff"]);
        assert_eq!(args.volatile, ["cutoff"]);
        let args = rendered(&[
            "render",
            "--as",
            "samples",
            "--volatile",
            "cutoff",
            "--volatile",
            "q",
        ]);
        assert_eq!(args.volatile, ["cutoff", "q"]);
        assert!(rendered(&["render", "--as", "samples"]).volatile.is_empty());
        assert!(matches!(
            parse_args(&argv(&["render", "--as", "samples", "--volatile"])),
            Err(CliError::Usage(_))
        ));
    }

    #[test]
    fn render_and_analyze_parse_the_same_window_flags() {
        let args = rendered(&["render", "--as", "samples", "--from", "0.5", "--to", "2"]);
        let window = (Some(WindowEdge::Secs(0.5)), Some(WindowEdge::Secs(2.0)));
        assert_eq!((args.from, args.to), window);
        let Ok(Command::Analyze(args)) = parse_args(&argv(&[
            "analyze",
            "/tmp/a.wav",
            "--as",
            "spectrum",
            "--from",
            "0.5",
            "--to",
            "2",
        ])) else {
            panic!("an analyze")
        };
        assert_eq!((args.from, args.to), window);
    }

    #[test]
    fn analyze_takes_a_wav_and_only_the_readings_a_buffer_answers() {
        assert!(matches!(
            parse_args(&argv(&["analyze", "/tmp/a.mp3", "--as", "spectrum"])),
            Err(CliError::Usage(_))
        ));
        assert!(matches!(
            parse_args(&argv(&["analyze", "/tmp/a.wav", "--as", "ledger"])),
            Err(CliError::Usage(_))
        ));
        assert!(matches!(
            parse_args(&argv(&["analyze", "/tmp/a.wav"])),
            Err(CliError::Usage(_))
        ));
    }

    #[test]
    fn lint_takes_an_optional_target_and_nothing_else() {
        assert_eq!(
            parse_args(&argv(&["lint"])).unwrap(),
            Command::Lint {
                target: None,
                dir: None,
                format: Format::Json
            }
        );
        assert_eq!(
            parse_args(&argv(&["lint", "drums/kick"])).unwrap(),
            Command::Lint {
                target: Some("drums/kick".to_string()),
                dir: None,
                format: Format::Json
            }
        );
        assert!(matches!(
            parse_args(&argv(&["lint", "a", "b"])),
            Err(CliError::Usage(_))
        ));
    }

    #[test]
    fn new_takes_a_name_and_an_optional_idempotency_key() {
        assert_eq!(
            parse_args(&argv(&["new", "song1", "--idempotency-key", "k"])).unwrap(),
            Command::New {
                name: "song1".to_string(),
                idempotency_key: Some("k".to_string()),
            }
        );
        assert!(matches!(
            parse_args(&argv(&["new"])),
            Err(CliError::Usage(_))
        ));
    }

    #[test]
    fn builtins_takes_no_arguments() {
        assert_eq!(parse_args(&argv(&["builtins"])).unwrap(), Command::Builtins);
        assert!(matches!(
            parse_args(&argv(&["builtins", "x"])),
            Err(CliError::Usage(_))
        ));
    }

    #[test]
    fn a_subcommand_this_cli_does_not_answer_refuses_by_name() {
        for gone in ["bench", "nonlinear-solve"] {
            let Err(CliError::Usage(message)) = parse_args(&argv(&[gone])) else {
                panic!("`{gone}` must refuse")
            };
            assert!(message.contains(gone), "{message}");
        }
    }
}