Skip to main content

dimvexa_cli/
lib.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2//! The command-line front end of dimvexa.
3//!
4//! ```text
5//! dimvexa convert VALUE FROM TO [--exact] [--json] [--config NAME_OR_PATH]... [--density NAME_OR_VALUE]
6//! dimvexa VALUE FROM TO [--exact] [--json]        shorthand
7//! dimvexa units [--groups | GROUP | UNIT] [--json]  list units by quantity, one group, or one unit
8//! dimvexa configs [NAME_OR_PATH] [--json]          what the shipped configs (or a file) contain
9//! ```
10//!
11//! This crate contains **no conversion logic**: it parses arguments, calls the public
12//! library API and formats the result, so it cannot disagree with the library. It always
13//! uses the exact path; the decimal it prints is the exact result rounded once to the
14//! nearest `f64` (0 ulp), never the fast path's result. The library's fast path is within
15//! 1 ulp (linear) or 2 ulp (affine) of that value (`docs/precision.md`).
16//!
17//! Exit codes: `0` success, `1` conversion refused or invalid input, `2` usage error.
18//! With `--json`, errors are printed to stdout as JSON (schema `dimvexa.convert/2`, see
19//! `docs/schemas/`; `units --json` uses `dimvexa.units/2`, `configs --json` `dimvexa.configs/1`) and the exit code stays non-zero. The error `code` is one of the
20//! library's stable codes ([`dimvexa::Error::ALL_CODES`]) or `usage`.
21#![forbid(unsafe_code)]
22#![doc(
23    html_logo_url = "https://raw.githubusercontent.com/welworx/dimvexa/main/docs/brand/icon-256.png",
24    html_favicon_url = "https://raw.githubusercontent.com/welworx/dimvexa/main/docs/brand/icon-32.png"
25)]
26
27use std::fmt::Write as _;
28use std::io::Write;
29
30use dimvexa::{Conversion, Dim, Error, Ratio, Registry, Unit, UnitExpr, registry};
31use dimvexa_config::{Config, ConfigUnit, Constant, Mapping};
32
33mod update;
34
35const USAGE: &str = "\
36Usage:
37  dimvexa convert VALUE FROM TO [--exact] [--json] [--config NAME_OR_PATH]... [--density NAME_OR_VALUE]
38  dimvexa VALUE FROM TO [--exact] [--json] [--config NAME_OR_PATH]... [--density NAME_OR_VALUE]
39  dimvexa units [--groups | GROUP | UNIT] [--json] [--config NAME_OR_PATH]...
40  dimvexa configs [NAME_OR_PATH] [--json]
41  dimvexa update [--check] [-y]
42
43Converts VALUE from unit FROM to unit TO.
44
45Commands:
46  units            list the supported units grouped by quantity, and the ambiguous symbols
47                   that are refused
48  units --groups   list only the groups (length, mass, volume, ...) with their sizes
49  units GROUP      list one group, for example: dimvexa units volume
50  units UNIT       show the details of one unit, for example: dimvexa units psi
51  configs          list the configs that ship with dimvexa (us, gb, ...)
52  configs NAME     show what one config contains: names, units and densities with their
53                   conditions and sources; a path shows your own file
54  update           replace this binary with the latest GitHub release (needs curl and tar;
55                   checks the published SHA-256 first; see docs/adr/015-update-command.md)
56  update --check   only report whether a newer release exists; exit 0 up to date, 3 newer
57                   release available, 1 error
58  update -y        do not ask \"Update to vX.Y.Z? [y/N]\" first (also --yes)
59
60Options:
61  --exact      print the exact rational result (num/den) instead of a decimal
62  --json       machine-readable output (schema dimvexa.convert/2; dimvexa.units/2 for units,
63               dimvexa.configs/1 for configs)
64  --color WHEN auto (default: a terminal, unless NO_COLOR is set), always (also
65               CLICOLOR_FORCE=1) or never; JSON is never coloured
66  --pretty     --json, indented for reading (same fields, same order)
67  --config NAME_OR_PATH  (beta) apply a config (repeatable, later ones win; see
68               docs/adr/014-one-config-mechanism.md). A shipped one is named (us, gb,
69               metric-trade, year-julian, year-civil, water); a file has a path (./mine.toml,
70               or anything with a / or ending in .toml). Without --config the names stay strict
71  --density NAME_OR_VALUE  use a density for mass <-> volume: a name from a loaded config, for
72               example --config water --density water_25C, or your own number, for example
73               --density 998.2067kg/m3. The assumption is printed with the result
74  --version    print version and data table version
75  -h, --help   print this help
76
77Examples:
78  dimvexa convert 10 m/s km/h
79  dimvexa convert 1 psi Pa --exact
80  dimvexa convert 100 degC degF --json
81  dimvexa convert 100 degC degF --pretty
82  dimvexa convert 1 ton kg --config us
83  dimvexa convert 1 mt kg --config metric-trade
84  dimvexa convert 1 L kg --config water --density water_25C
85  dimvexa convert 1 L kg --density 998.2067kg/m3
86  dimvexa convert 1 bbl_oil L --config oil.toml
87
88Precision: the CLI always uses the exact path, so the number printed is the exact result
89rounded once to the nearest f64 (0 ulp). The library's fast f64 path is within 1 ulp (linear
90units) or 2 ulp (temperature scales such as degC and degF) of it; see docs/precision.md.
91
92Exit codes: 0 success, 1 conversion refused or invalid input, 2 usage error; `update --check`
93also exits 3 when a newer release exists.
94";
95
96/// Shown after the project facts; the long form is `docs/licensing.md`.
97const DISCLAIMER: &str = "\
98Provided as is, without warranty. Unit factors and sources can contain errors: verify any
99result you depend on against the primary source, and do not use it as the only safeguard in
100safety-critical, medical, legal, financial or custody-transfer settings. Independent project,
101not affiliated with other unit libraries. Terms: LICENSE-MIT and LICENSE-APACHE.";
102
103/// The value of `--color`.
104#[derive(Clone, Copy, Debug, PartialEq, Eq)]
105enum ColorWhen {
106    Auto,
107    Always,
108    Never,
109}
110
111/// Removes `--color WHEN` / `--color=WHEN` from `args`; the last one wins.
112fn take_color_option(args: &mut Vec<String>) -> Result<Option<ColorWhen>, String> {
113    let mut found = None;
114    let mut rest = Vec::with_capacity(args.len());
115    let mut it = std::mem::take(args).into_iter();
116    while let Some(a) = it.next() {
117        let value = if a == "--color" {
118            it.next().filter(|v| !v.starts_with("--"))
119        } else if let Some(v) = a.strip_prefix("--color=") {
120            Some(v.to_string())
121        } else {
122            rest.push(a);
123            continue;
124        };
125        found = Some(match value.as_deref() {
126            Some("auto") => ColorWhen::Auto,
127            Some("always") => ColorWhen::Always,
128            Some("never") => ColorWhen::Never,
129            _ => {
130                *args = rest;
131                return Err("'--color' needs auto, always or never (--color=always)".to_string());
132            }
133        });
134    }
135    *args = rest;
136    Ok(found)
137}
138
139/// Name, version, tagline and where the project lives, from the package metadata.
140fn about() -> String {
141    format!(
142        "License:    {}\nAuthors:    {}\nRepository: {}\n\n{DISCLAIMER}\n",
143        env!("CARGO_PKG_LICENSE"),
144        env!("CARGO_PKG_AUTHORS"),
145        env!("CARGO_PKG_REPOSITORY")
146    )
147}
148
149/// The text of `--help`: a title line, the usage, and the project facts.
150fn help_text() -> String {
151    format!(
152        "dimvexa {} - Exact factors. Loud failures. Zero guessing.\n\n{USAGE}\n{}",
153        env!("CARGO_PKG_VERSION"),
154        about()
155    )
156}
157
158/// Whether each output stream may carry ANSI colour. Off unless the caller turns it on
159/// (the binary does so for a terminal, honouring `NO_COLOR`).
160#[derive(Clone, Copy, Debug, Default)]
161pub struct Color {
162    /// Colour the result on stdout.
163    pub out: bool,
164    /// Colour `error`, `hint` and `note` lines on stderr.
165    pub err: bool,
166}
167
168const BOLD_GREEN: &str = "\x1b[1;32m";
169const CYAN: &str = "\x1b[36m";
170const BOLD_RED: &str = "\x1b[1;31m";
171const YELLOW: &str = "\x1b[33m";
172const RESET: &str = "\x1b[0m";
173
174/// Colours the leading label of `error[..]:`, `hint:` and `note:` lines.
175fn colorize_diagnostics(text: &str) -> String {
176    let mut out = String::with_capacity(text.len());
177    for line in text.split_inclusive('\n') {
178        let split = if line.starts_with("error[") {
179            line.find("]:").map(|i| (i.saturating_add(1), BOLD_RED))
180        } else if line.starts_with("hint:") {
181            Some((5, CYAN))
182        } else if line.starts_with("note:") {
183            Some((5, YELLOW))
184        } else {
185            None
186        };
187        match split.and_then(|(n, c)| Some((line.get(..n)?, line.get(n..)?, c))) {
188            Some((label, rest, c)) => {
189                let _ = write!(out, "{c}{label}{RESET}{rest}");
190            }
191            None => out.push_str(line),
192        }
193    }
194    out
195}
196
197/// A JSON value for re-indenting: scalars are kept as their source text.
198enum Node {
199    Scalar(String),
200    Array(Vec<Self>),
201    Object(Vec<(String, Self)>),
202}
203
204/// Reads one value of the compact JSON that this crate writes. `None` if it is not JSON.
205fn read_node(chars: &[char], i: &mut usize) -> Option<Node> {
206    fn string(chars: &[char], i: &mut usize) -> Option<String> {
207        let start = *i;
208        *i = i.checked_add(1)?;
209        while let Some(&c) = chars.get(*i) {
210            *i = i.checked_add(1)?;
211            match c {
212                '\\' => *i = i.checked_add(1)?,
213                '"' => return Some(chars.get(start..*i)?.iter().collect()),
214                _ => {}
215            }
216        }
217        None
218    }
219    match *chars.get(*i)? {
220        '"' => string(chars, i).map(Node::Scalar),
221        open @ ('[' | '{') => {
222            *i = i.checked_add(1)?;
223            let close = if open == '[' { ']' } else { '}' };
224            let (mut items, mut fields) = (Vec::new(), Vec::new());
225            if chars.get(*i) == Some(&close) {
226                *i = i.checked_add(1)?;
227            } else {
228                loop {
229                    if open == '{' {
230                        let key = string(chars, i)?;
231                        if chars.get(*i) != Some(&':') {
232                            return None;
233                        }
234                        *i = i.checked_add(1)?;
235                        fields.push((key, read_node(chars, i)?));
236                    } else {
237                        items.push(read_node(chars, i)?);
238                    }
239                    let c = *chars.get(*i)?;
240                    *i = i.checked_add(1)?;
241                    if c == close {
242                        break;
243                    }
244                    if c != ',' {
245                        return None;
246                    }
247                }
248            }
249            Some(if open == '[' {
250                Node::Array(items)
251            } else {
252                Node::Object(fields)
253            })
254        }
255        _ => {
256            let start = *i;
257            while chars.get(*i).is_some_and(|c| !matches!(c, ',' | ']' | '}')) {
258                *i = i.checked_add(1)?;
259            }
260            Some(Node::Scalar(chars.get(start..*i)?.iter().collect()))
261        }
262    }
263}
264
265/// An array of scalars stays on one line while it is short.
266fn fits_on_a_line(items: &[Node]) -> bool {
267    let mut width = 0usize;
268    for n in items {
269        match n {
270            Node::Scalar(s) => width = width.saturating_add(s.chars().count()).saturating_add(2),
271            _ => return false,
272        }
273    }
274    width <= 60
275}
276
277/// Writes `node` indented by two spaces per level. Short arrays of scalars stay on one line.
278fn write_node(node: &Node, depth: usize, out: &mut String) {
279    let pad = |n: usize| "  ".repeat(n);
280    match node {
281        Node::Scalar(s) => out.push_str(s),
282        Node::Array(items) if fits_on_a_line(items) => {
283            out.push('[');
284            for (k, n) in items.iter().enumerate() {
285                if k > 0 {
286                    out.push_str(", ");
287                }
288                write_node(n, depth, out);
289            }
290            out.push(']');
291        }
292        Node::Array(items) => {
293            out.push_str("[\n");
294            for (k, n) in items.iter().enumerate() {
295                out.push_str(&pad(depth.saturating_add(1)));
296                write_node(n, depth.saturating_add(1), out);
297                out.push_str(if k.saturating_add(1) < items.len() {
298                    ",\n"
299                } else {
300                    "\n"
301                });
302            }
303            out.push_str(&pad(depth));
304            out.push(']');
305        }
306        Node::Object(fields) if fields.is_empty() => out.push_str("{}"),
307        Node::Object(fields) => {
308            out.push_str("{\n");
309            for (k, (key, n)) in fields.iter().enumerate() {
310                out.push_str(&pad(depth.saturating_add(1)));
311                let _ = write!(out, "{key}: ");
312                write_node(n, depth.saturating_add(1), out);
313                out.push_str(if k.saturating_add(1) < fields.len() {
314                    ",\n"
315                } else {
316                    "\n"
317                });
318            }
319            out.push_str(&pad(depth));
320            out.push('}');
321        }
322    }
323}
324
325/// Re-indents one line of compact JSON (`--pretty`); anything that is not JSON is returned as is.
326fn pretty_json(line: &str) -> String {
327    let chars: Vec<char> = line.chars().collect();
328    let mut i = 0;
329    match read_node(&chars, &mut i) {
330        Some(node) if i == chars.len() => {
331            let mut out = String::new();
332            write_node(&node, 0, &mut out);
333            out
334        }
335        _ => line.to_string(),
336    }
337}
338
339/// Parsed command line.
340struct Args {
341    /// The density named or written with `--density`, if any.
342    density: Option<String>,
343    value: String,
344    from: String,
345    to: String,
346    exact: bool,
347    json: bool,
348}
349
350/// What `dimvexa units` was asked for.
351enum UnitsTarget {
352    /// The whole table, grouped.
353    All,
354    /// Only the names of the groups.
355    Groups,
356    /// One group (`volume`) or one unit (`psi`, `km`).
357    Query(String),
358}
359
360enum Parsed {
361    Convert(Args),
362    Units {
363        target: UnitsTarget,
364        json: bool,
365    },
366    /// `dimvexa configs [NAME_OR_PATH]`.
367    Configs {
368        target: Option<String>,
369        json: bool,
370    },
371    /// `dimvexa update [--check] [--yes]`.
372    Update {
373        check: bool,
374        yes: bool,
375    },
376    Help,
377    Version,
378}
379
380/// Why the command line could not be used.
381struct UsageError {
382    message: String,
383    json: bool,
384    /// JSON schema id of the failed command (`convert`, `units` or `configs`).
385    schema: &'static str,
386    suggestions: Vec<String>,
387}
388
389/// The JSON schema name of the command the line asks for: the first word that is not an option.
390fn command_schema(args: &[String]) -> &'static str {
391    match args
392        .iter()
393        .find(|a| !a.starts_with('-'))
394        .map(String::as_str)
395    {
396        Some("units") => "units",
397        Some("configs") => "configs",
398        _ => "convert",
399    }
400}
401
402/// The options that take a value, split off the command line before the rest is read.
403struct ValueOptions {
404    configs: Vec<String>,
405    density: Option<String>,
406}
407
408/// Splits `--config NAME_OR_PATH` (repeatable) and `--density NAME_OR_VALUE`, also as
409/// `--opt=value`.
410fn split_options(args: &[String]) -> Result<(ValueOptions, Vec<String>), UsageError> {
411    let mut opts = ValueOptions {
412        configs: Vec::new(),
413        density: None,
414    };
415    let mut rest = Vec::new();
416    let missing = |what: &str| UsageError {
417        message: format!("'{what}' needs a value"),
418        json: args.iter().any(|x| x == "--json"),
419        schema: command_schema(args),
420        suggestions: Vec::new(),
421    };
422    let mut it = args.iter();
423    while let Some(a) = it.next() {
424        let (name, inline) = match a.split_once('=') {
425            Some((n @ ("--config" | "--density"), v)) => (n, Some(v.to_string())),
426            _ => (a.as_str(), None),
427        };
428        match name {
429            "--bridge" => {
430                return Err(UsageError {
431                    message:
432                        "'--bridge' was renamed '--density' (ADR-014): use --density NAME_OR_VALUE"
433                            .to_string(),
434                    ..missing(name)
435                });
436            }
437            "--config" | "--density" => {
438                let value = match inline {
439                    Some(v) => v,
440                    // The next word must be a value, not another option (`--config --json`).
441                    None => it
442                        .next()
443                        .filter(|v| !v.starts_with("--"))
444                        .cloned()
445                        .ok_or_else(|| missing(name))?,
446                };
447                if name == "--config" {
448                    opts.configs.push(value);
449                } else if opts.density.replace(value).is_some() {
450                    return Err(UsageError {
451                        message: "'--density' is given twice; give one density".to_string(),
452                        ..missing(name)
453                    });
454                }
455            }
456            _ => rest.push(a.clone()),
457        }
458    }
459    Ok((opts, rest))
460}
461
462/// The rest of `dimvexa units ...` after the command word.
463fn parse_units(
464    rest: &[&str],
465    want_groups: bool,
466    json: bool,
467    configs: Vec<String>,
468    usage: &dyn Fn(String) -> UsageError,
469) -> Result<(Parsed, Vec<String>), UsageError> {
470    match (rest, want_groups) {
471        ([], false) => Ok((
472            Parsed::Units {
473                target: UnitsTarget::All,
474                json,
475            },
476            configs,
477        )),
478        ([], true) => Ok((
479            Parsed::Units {
480                target: UnitsTarget::Groups,
481                json,
482            },
483            configs,
484        )),
485        ([_], true) => Err(usage(
486            "'--groups' lists the groups and takes no GROUP or UNIT".to_string(),
487        )),
488        ([query], false) => Ok((
489            Parsed::Units {
490                target: UnitsTarget::Query((*query).to_string()),
491                json,
492            },
493            configs,
494        )),
495        _ => Err(usage(format!(
496            "'units' takes at most one argument (a group such as 'length' or a unit such as 'psi'), got {}",
497            rest.len()
498        ))),
499    }
500}
501
502/// The rest of `dimvexa configs ...` after the command word.
503fn parse_configs(
504    rest: &[&str],
505    json: bool,
506    configs: Vec<String>,
507    usage: &dyn Fn(String) -> UsageError,
508) -> Result<(Parsed, Vec<String>), UsageError> {
509    match rest {
510        [] => Ok((Parsed::Configs { target: None, json }, configs)),
511        [one] => Ok((
512            Parsed::Configs {
513                target: Some((*one).to_string()),
514                json,
515            },
516            configs,
517        )),
518        _ => Err(usage(format!(
519            "'configs' takes at most one argument (a name such as 'us' or a path), got {}",
520            rest.len()
521        ))),
522    }
523}
524
525/// Checks what the command line says next to `update`: `extra` is a positional argument,
526/// `--exact`, `--json` or `--groups`; `value_options` is `--config` or `--density`. A refusal is
527/// never a `--json` error: the update has no JSON output and no schema.
528fn check_update_args(
529    extra: bool,
530    value_options: bool,
531    schema: &'static str,
532) -> Result<(), UsageError> {
533    let plain = |message: &str| UsageError {
534        message: message.to_string(),
535        json: false,
536        schema,
537        suggestions: Vec::new(),
538    };
539    if extra {
540        return Err(plain("'update' takes only --check and -y"));
541    }
542    if value_options {
543        return Err(plain("'update' does not take --config or --density"));
544    }
545    Ok(())
546}
547
548/// Forgives how people write a conversion: a degree sign set apart from its scale
549/// (`° F` is `°F`) and a filler `to` (`0 °C to °F` is `0 °C °F`).
550fn normalize_convert(words: &[&str]) -> Vec<String> {
551    let mut out: Vec<String> = Vec::with_capacity(words.len());
552    let mut i = 0;
553    while let Some(&w) = words.get(i) {
554        i = i.saturating_add(1);
555        if matches!(w, "°" | "º") {
556            if let Some(&scale) = words.get(i).filter(|n| matches!(**n, "C" | "F" | "R")) {
557                out.push(format!("°{scale}"));
558                i = i.saturating_add(1);
559                continue;
560            }
561        }
562        out.push(w.to_string());
563    }
564    if let [value, from, to, dest] = out.as_slice() {
565        if to == "to" {
566            return vec![value.clone(), from.clone(), dest.clone()];
567        }
568    }
569    out
570}
571
572fn parse_args(all: &[String]) -> Result<(Parsed, Vec<String>), UsageError> {
573    let (opts, args) = split_options(all)?;
574    let configs = opts.configs;
575    let args = args.as_slice();
576    let json = args.iter().any(|a| a == "--json");
577    let schema = command_schema(args);
578    let usage = |message: String| UsageError {
579        message,
580        json,
581        schema,
582        suggestions: Vec::new(),
583    };
584    let mut positional: Vec<&str> = Vec::new();
585    let (mut exact, mut want_json, mut want_groups) = (false, false, false);
586    let (mut want_check, mut want_yes) = (false, false);
587    for a in args {
588        match a.as_str() {
589            "--check" => want_check = true,
590            "-y" | "--yes" => want_yes = true,
591            "--exact" => exact = true,
592            "--json" => want_json = true,
593            "--groups" => want_groups = true,
594            "-h" | "--help" => return Ok((Parsed::Help, configs)),
595            "-V" | "--version" => return Ok((Parsed::Version, configs)),
596            s if s.starts_with("--") => return Err(usage(format!("unknown option '{s}'"))),
597            s => positional.push(s),
598        }
599    }
600    if let Some((&"update", rest)) = positional.split_first() {
601        check_update_args(
602            !rest.is_empty() || exact || want_json || want_groups,
603            !configs.is_empty() || opts.density.is_some(),
604            schema,
605        )?;
606        return Ok((
607            Parsed::Update {
608                check: want_check,
609                yes: want_yes,
610            },
611            configs,
612        ));
613    }
614    if want_check || want_yes {
615        return Err(usage(
616            "'--check' and '-y' only apply to 'update'".to_string(),
617        ));
618    }
619    if let Some((&"units", rest)) = positional.split_first() {
620        if exact {
621            return Err(usage("'units' does not take --exact".to_string()));
622        }
623        if opts.density.is_some() {
624            return Err(usage(
625                "'--density' selects a density for a conversion; 'units' lists them".to_string(),
626            ));
627        }
628        return parse_units(rest, want_groups, want_json, configs, &usage);
629    }
630    if let Some((&"configs", rest)) = positional.split_first() {
631        if exact || want_groups {
632            return Err(usage(
633                "'configs' takes only NAME_OR_PATH and --json".to_string(),
634            ));
635        }
636        if !configs.is_empty() || opts.density.is_some() {
637            return Err(usage(
638                "'configs' shows configs by name or path; it does not take --config or --density"
639                    .to_string(),
640            ));
641        }
642        return parse_configs(rest, want_json, configs, &usage);
643    }
644    if want_groups {
645        return Err(usage("'--groups' only applies to 'units'".to_string()));
646    }
647    let density = opts.density;
648    if positional.first() == Some(&"convert") {
649        positional.remove(0);
650    }
651    let positional = normalize_convert(&positional);
652    match positional.as_slice() {
653        [value, from, to] => Ok((
654            Parsed::Convert(Args {
655                density,
656                value: value.clone(),
657                from: from.clone(),
658                to: to.clone(),
659                exact,
660                json: want_json,
661            }),
662            configs,
663        )),
664        other => Err(usage(format!(
665            "expected VALUE FROM TO, got {} argument(s); try 'dimvexa --help'",
666            other.len()
667        ))),
668    }
669}
670
671/// Escapes a string for JSON.
672fn json_string(s: &str) -> String {
673    let mut out = String::with_capacity(s.len().saturating_add(2));
674    out.push('"');
675    for c in s.chars() {
676        match c {
677            '"' => out.push_str("\\\""),
678            '\\' => out.push_str("\\\\"),
679            '\n' => out.push_str("\\n"),
680            '\r' => out.push_str("\\r"),
681            '\t' => out.push_str("\\t"),
682            c if u32::from(c) < 0x20 => {
683                let _ = write!(out, "\\u{:04x}", u32::from(c));
684            }
685            c => out.push(c),
686        }
687    }
688    out.push('"');
689    out
690}
691
692/// Formats an `f64` as the shortest text that reads back, switching to exponent form for
693/// very large and very small magnitudes. The output is valid JSON as well.
694fn f64_text(x: f64) -> String {
695    let a = x.abs();
696    if a != 0.0 && !(1e-4..1e16).contains(&a) {
697        format!("{x:e}")
698    } else {
699        format!("{x}")
700    }
701}
702
703/// The JSON schema id of a command: `dimvexa.convert/2`, `dimvexa.units/2`, `dimvexa.configs/1`.
704fn schema_id(command: &str) -> String {
705    let version = match command {
706        "configs" => 1,
707        _ => 2,
708    };
709    format!("{}.{command}/{version}", dimvexa::PROJECT_NAME)
710}
711
712fn json_error(
713    schema: &str,
714    code: &str,
715    message: &str,
716    candidates: &[String],
717    suggestions: &[String],
718) -> String {
719    let mut s = format!(
720        "{{\"schema\":{},\"ok\":false,\"error\":{{\"code\":{},\"message\":{}",
721        json_string(&schema_id(schema)),
722        json_string(code),
723        json_string(message)
724    );
725    if !candidates.is_empty() {
726        let list: Vec<String> = candidates.iter().map(|c| json_string(c)).collect();
727        let _ = write!(s, ",\"candidates\":[{}]", list.join(","));
728    }
729    if !suggestions.is_empty() {
730        let list: Vec<String> = suggestions.iter().map(|c| json_string(c)).collect();
731        let _ = write!(s, ",\"suggestions\":[{}]", list.join(","));
732    }
733    s.push_str("}}");
734    s
735}
736
737/// A registry with the user's configs applied, and which configs those were.
738struct Loaded {
739    reg: Registry,
740    /// The name mappings in effect (empty without a config).
741    mappings: Vec<Mapping>,
742    /// The user units in effect (empty without a config).
743    units: Vec<ConfigUnit>,
744    /// The densities in effect (empty without a config).
745    densities: Vec<Constant>,
746    /// `(origin, content hash)` of each config, in the order given. The origin is the file
747    /// path, or `bundled:NAME` for a config that ships with dimvexa.
748    configs: Vec<(String, String)>,
749}
750
751/// Why the registry could not be built.
752enum LoadError {
753    /// A config file could not be read (a usage problem).
754    Io(String),
755    /// The table or a config was refused by the library.
756    Lib(Error),
757}
758
759/// The largest config file we read: a config is a handful of lines, so anything bigger is
760/// a mistake (or a device such as `/dev/zero`), not a config.
761const MAX_CONFIG_BYTES: u64 = 1 << 20;
762
763/// Reads a config file: a regular file of at most [`MAX_CONFIG_BYTES`], valid UTF-8.
764fn read_config(file: &str) -> Result<String, LoadError> {
765    use std::io::Read as _;
766    let fail = |why: String| LoadError::Io(format!("cannot read config '{file}': {why}"));
767    let meta = std::fs::metadata(file).map_err(|e| fail(e.to_string()))?;
768    if !meta.is_file() {
769        return Err(fail("not a regular file".to_string()));
770    }
771    let mut text = String::new();
772    std::fs::File::open(file)
773        .map_err(|e| fail(e.to_string()))?
774        .take(MAX_CONFIG_BYTES.saturating_add(1))
775        .read_to_string(&mut text)
776        .map_err(|e| fail(e.to_string()))?;
777    if u64::try_from(text.len()).map_or(true, |n| n > MAX_CONFIG_BYTES) {
778        return Err(fail(format!("larger than {MAX_CONFIG_BYTES} bytes")));
779    }
780    Ok(text)
781}
782
783/// Reads one `--config` value: a shipped config by name, or a file by path (ADR-014). A bare
784/// word is always a shipped name; a file called `us` is written `./us`.
785fn read_spec(spec: &str) -> Result<Config, LoadError> {
786    if dimvexa_config::is_path(spec) {
787        let text = read_config(spec)?;
788        return Config::parse(&text, spec).map_err(LoadError::Lib);
789    }
790    dimvexa_config::bundled(spec).map_or_else(
791        || {
792            Err(LoadError::Io(format!(
793                "no config named '{spec}'; shipped configs: {} (a file needs a path, for example ./{spec} or {spec}.toml)",
794                bundled_names()
795            )))
796        },
797        |b| b.config().map_err(LoadError::Lib),
798    )
799}
800
801fn bundled_names() -> String {
802    dimvexa_config::BUNDLED
803        .iter()
804        .map(|b| b.name)
805        .collect::<Vec<_>>()
806        .join(", ")
807}
808
809fn load(config_specs: &[String]) -> Result<Loaded, LoadError> {
810    let mut reg = registry().map_err(LoadError::Lib)?;
811    let (mut parsed, mut configs) = (Vec::new(), Vec::new());
812    for spec in config_specs {
813        let cfg = read_spec(spec)?;
814        configs.push((cfg.origin().to_string(), cfg.hash().to_string()));
815        parsed.push(cfg);
816    }
817    let applied = dimvexa_config::apply(&mut reg, &parsed).map_err(LoadError::Lib)?;
818    let densities = dimvexa_config::constants(&parsed);
819    Ok(Loaded {
820        reg,
821        mappings: applied.mappings,
822        units: applied.units,
823        densities,
824        configs,
825    })
826}
827
828/// Prints a load failure for `schema` and returns the exit code.
829fn report_load_error(
830    e: &LoadError,
831    json: bool,
832    schema: &str,
833    out: &mut dyn Write,
834    err: &mut dyn Write,
835) -> u8 {
836    let (code, message, status) = match e {
837        LoadError::Io(m) => ("usage", m.clone(), 2),
838        LoadError::Lib(e) => (e.code(), e.to_string(), 1),
839    };
840    if json {
841        let _ = writeln!(out, "{}", json_error(schema, code, &message, &[], &[]));
842    } else {
843        let _ = writeln!(err, "error[{code}]: {message}");
844    }
845    status
846}
847
848/// JSON array of the configs used: `[{"file":..,"hash":..}]`.
849fn configs_json(configs: &[(String, String)]) -> String {
850    let items: Vec<String> = configs
851        .iter()
852        .map(|(f, h)| {
853            format!(
854                "{{\"file\":{},\"hash\":{}}}",
855                json_string(f),
856                json_string(h)
857            )
858        })
859        .collect();
860    format!("[{}]", items.join(","))
861}
862
863/// Everything computed for a successful conversion, before formatting.
864struct Outcome<'a> {
865    args: &'a Args,
866    configs: &'a [(String, String)],
867    /// The density the conversion went through, if it needed one.
868    density: Option<&'a Constant>,
869    from: UnitExpr,
870    to: UnitExpr,
871    conv: Conversion,
872    exact: Ratio,
873    /// The exact result rounded once to the nearest `f64` (the CLI always uses the exact path).
874    approx: f64,
875}
876
877fn compute<'a>(
878    args: &'a Args,
879    loaded: &'a Loaded,
880    density: Option<&'a Constant>,
881) -> Result<Outcome<'a>, Error> {
882    let reg = &loaded.reg;
883    let value: Ratio = args.value.parse()?;
884    let from = reg.parse(&args.from)?;
885    let to = reg.parse(&args.to)?;
886    // A density is used only when asked for and only when the plain conversion is refused
887    // for lack of a density; otherwise the ordinary exact conversion stands.
888    let (conv, used) = match (Conversion::between(&from, &to), density) {
889        (Ok(conv), _) => (conv, None),
890        (Err(Error::DimensionMismatch { .. }), Some(d)) => (d.conversion(&from, &to)?, Some(d)),
891        (Err(e), _) => return Err(e),
892    };
893    let exact = conv.apply_exact(value)?;
894    let approx = exact.to_f64()?;
895    Ok(Outcome {
896        args,
897        configs: &loaded.configs,
898        density: used,
899        from,
900        to,
901        conv,
902        exact,
903        approx,
904    })
905}
906
907/// Why `--density` could not be resolved.
908enum DensityError {
909    /// A name that no loaded config defines, or a missing value: a usage problem, with the
910    /// known names.
911    Usage(String, Vec<String>),
912    /// The library refused the caller's own number (not a density, ambiguous unit, overflow).
913    Lib(Error),
914}
915
916/// Resolves `--density`: a number with a unit is the caller's own density, anything else is
917/// the name of a density in a loaded config.
918fn resolve_density(spec: &str, loaded: &Loaded) -> Result<Constant, DensityError> {
919    let starts_like_a_number = spec.starts_with(|c: char| c.is_ascii_digit() || "+-.".contains(c));
920    if starts_like_a_number {
921        return Constant::density_from_text(&loaded.reg, spec).map_err(DensityError::Lib);
922    }
923    let known: Vec<String> = loaded.densities.iter().map(|d| d.name.clone()).collect();
924    loaded
925        .densities
926        .iter()
927        .find(|d| d.name == spec)
928        .cloned()
929        .ok_or_else(|| {
930            let message = if known.is_empty() {
931                format!(
932                    "no density named '{spec}': no loaded config has a [[density]] (try --config water, or write your own: --density 998.2067kg/m3)"
933                )
934            } else {
935                format!("no density named '{spec}'; loaded densities: {}", known.join(", "))
936            };
937            DensityError::Usage(message, known)
938        })
939}
940
941fn render_text(o: &Outcome<'_>, color: bool) -> String {
942    let value = if o.args.exact {
943        o.exact.to_fraction_string()
944    } else {
945        f64_text(o.approx)
946    };
947    if color {
948        format!("{BOLD_GREEN}{value}{RESET} {CYAN}{}{RESET}", o.to)
949    } else {
950        format!("{value} {}", o.to)
951    }
952}
953
954/// The JSON object describing a density: everything a reader needs to judge the assumption.
955fn density_json(b: &Constant) -> String {
956    format!(
957        "{{\"name\":{},\"kind\":{},\"value\":{},\"unit\":{},\"conditions\":{},\"source\":{},\"file\":{},\"line\":{}}}",
958        json_string(&b.name),
959        json_string(b.kind.name()),
960        json_string(&b.value.to_fraction_string()),
961        json_string(b.kind.unit()),
962        json_string(&b.conditions),
963        json_string(&b.source),
964        json_string(&b.origin),
965        b.line
966    )
967}
968
969/// One line for people: the assumption behind a result that used a density. Text from the config has its
970/// control characters replaced, and a density that is not a short decimal is shown as the
971/// exact fraction, so the note never reads as a different number.
972fn density_note(b: &Constant) -> String {
973    let value = match b.value.to_decimal(15) {
974        Ok((decimal, true)) => decimal,
975        Ok((decimal, false)) => format!("{} (about {decimal})", b.value.to_fraction_string()),
976        Err(_) => b.value.to_fraction_string(),
977    };
978    let clean = |s: &str| -> String {
979        s.chars()
980            .map(|c| if c.is_control() { ' ' } else { c })
981            .collect()
982    };
983    let at = if b.line == 0 {
984        clean(&b.origin)
985    } else {
986        format!("{}:{}", clean(&b.origin), b.line)
987    };
988    format!(
989        "note: used density {} ({} {}; {}; source: {}; {})",
990        clean(&b.name),
991        value,
992        b.kind.unit(),
993        clean(&b.conditions),
994        clean(&b.source),
995        at
996    )
997}
998
999fn render_json(o: &Outcome<'_>) -> Result<String, Error> {
1000    let (decimal, complete) = o.exact.to_decimal(15)?;
1001    let dims: Vec<String> =
1002        o.to.dim()
1003            .exponents()
1004            .iter()
1005            .map(ToString::to_string)
1006            .collect();
1007    let mut sources: Vec<String> = o.from.sources().to_vec();
1008    for s in o.to.sources() {
1009        if !sources.contains(s) {
1010            sources.push(s.clone());
1011        }
1012    }
1013    if let Some(b) = o.density {
1014        sources.push(format!("{} (density {})", b.source, b.name));
1015    }
1016    let sources: Vec<String> = sources.iter().map(|s| json_string(s)).collect();
1017    let exactness = o.density.map_or_else(
1018        || o.from.exactness().combine(o.to.exactness()),
1019        |b| {
1020            o.from
1021                .exactness()
1022                .combine(o.to.exactness())
1023                .combine(b.exactness())
1024        },
1025    );
1026    let packs: Vec<String> = dimvexa::PACKS.iter().map(|p| json_string(p)).collect();
1027    Ok(format!(
1028        "{{\"schema\":{},\"ok\":true,\
1029\"input\":{{\"value\":{},\"unit\":{}}},\
1030\"output\":{{\"value\":{},\"unit\":{},\"f64\":{},\"truncated\":{}}},\
1031\"exact\":{{\"value\":{},\"factor\":{},\"offset\":{}}},\
1032\"exactness\":{},\"dimension\":[{}],\"kind\":{},\"quantity_kind\":{},\
1033\"packs\":[{}],\"data_version\":{},\"sources\":[{}],\"config\":{},\"density\":{}}}",
1034        json_string(&schema_id("convert")),
1035        json_string(&o.args.value),
1036        json_string(&o.from.to_string()),
1037        json_string(&decimal),
1038        json_string(&o.to.to_string()),
1039        f64_text(o.approx),
1040        !complete,
1041        json_string(&o.exact.to_fraction_string()),
1042        json_string(&o.conv.factor().to_fraction_string()),
1043        json_string(&o.conv.offset().to_fraction_string()),
1044        json_string(exactness.name()),
1045        dims.join(","),
1046        json_string(o.conv.shape().name()),
1047        json_string(o.to.kind().name()),
1048        packs.join(","),
1049        json_string(&dimvexa::data_version()),
1050        sources.join(","),
1051        configs_json(o.configs),
1052        o.density.map_or_else(|| "null".to_string(), density_json),
1053    ))
1054}
1055
1056/// Quantity group of a dimension (`[kg, m, s, A, K, mol, cd]` exponents), for listing.
1057/// Presentation only: it never takes part in a conversion.
1058fn group_name(dim: Dim) -> String {
1059    let name = match dim.exponents() {
1060        [0, 1, 0, 0, 0, 0, 0] => "length",
1061        [1, 0, 0, 0, 0, 0, 0] => "mass",
1062        [0, 0, 1, 0, 0, 0, 0] => "time",
1063        [0, 0, 0, 1, 0, 0, 0] => "electric_current",
1064        [0, 0, 0, 0, 1, 0, 0] => "temperature",
1065        [0, 0, 0, 0, 0, 1, 0] => "amount_of_substance",
1066        [0, 0, 0, 0, 0, 0, 1] => "luminous_intensity",
1067        [0, 0, 0, 0, 0, 0, 0] => "angle",
1068        [0, -2, 0, 0, 0, 0, 1] => "illuminance",
1069        [0, 0, -1, 0, 0, 1, 0] => "catalytic_activity",
1070        [0, 2, -2, 0, 0, 0, 0] => "absorbed_dose",
1071        [0, 2, 0, 0, 0, 0, 0] => "area",
1072        [0, 3, 0, 0, 0, 0, 0] => "volume",
1073        [0, 1, -1, 0, 0, 0, 0] => "speed",
1074        [0, 1, -2, 0, 0, 0, 0] => "acceleration",
1075        [0, 0, -1, 0, 0, 0, 0] => "frequency",
1076        [1, 1, -2, 0, 0, 0, 0] => "force",
1077        [1, -1, -2, 0, 0, 0, 0] => "pressure",
1078        [1, -1, -1, 0, 0, 0, 0] => "dynamic_viscosity",
1079        [0, 2, -1, 0, 0, 0, 0] => "kinematic_viscosity",
1080        [1, 2, -2, 0, 0, 0, 0] => "energy",
1081        [1, 2, -3, 0, 0, 0, 0] => "power",
1082        [0, 0, 1, 1, 0, 0, 0] => "electric_charge",
1083        [1, 2, -3, -1, 0, 0, 0] => "voltage",
1084        [1, 2, -3, -2, 0, 0, 0] => "resistance",
1085        [-1, -2, 4, 2, 0, 0, 0] => "capacitance",
1086        [-1, -2, 3, 2, 0, 0, 0] => "conductance",
1087        [1, 2, -2, -1, 0, 0, 0] => "magnetic_flux",
1088        [1, 0, -2, -1, 0, 0, 0] => "magnetic_flux_density",
1089        [1, 2, -2, -2, 0, 0, 0] => "inductance",
1090        _ => return format!("dimension[{dim}]"),
1091    };
1092    name.to_string()
1093}
1094
1095/// The ambiguous symbols to list: all of them, or with a group the ones that have a
1096/// candidate in it.
1097fn select_ambiguous(
1098    units: &[(String, &Unit)],
1099    filtered: bool,
1100) -> Vec<&'static dimvexa::AmbiguousDef> {
1101    dimvexa::AMBIGUOUS
1102        .iter()
1103        .filter(|a| {
1104            !filtered
1105                || a.candidates
1106                    .iter()
1107                    .any(|c| units.iter().any(|(_, u)| u.symbol() == *c))
1108        })
1109        .collect()
1110}
1111
1112/// The config unit that defines `symbol`, if the unit comes from a `--config` file.
1113fn user_unit<'a>(user: &'a [ConfigUnit], symbol: &str) -> Option<&'a ConfigUnit> {
1114    user.iter().find(|u| u.symbol == symbol)
1115}
1116
1117/// JSON for where a unit was defined: `{"file":..,"line":..}` or `null`.
1118fn origin_json(u: Option<&ConfigUnit>) -> String {
1119    u.map_or_else(
1120        || "null".to_string(),
1121        |u| {
1122            format!(
1123                "{{\"file\":{},\"line\":{}}}",
1124                json_string(&u.origin),
1125                u.line
1126            )
1127        },
1128    )
1129}
1130
1131/// A text-table field: tabs and line breaks would break the columns.
1132fn cell(s: &str) -> String {
1133    let t: String = s
1134        .chars()
1135        .map(|c| if c.is_control() { ' ' } else { c })
1136        .collect();
1137    if t.is_empty() { "-".to_string() } else { t }
1138}
1139
1140/// Every group of the whole table with its number of units, in table order. These are the
1141/// values `dimvexa units GROUP` accepts, whatever the current listing is narrowed to.
1142fn group_counts(reg: &Registry) -> Vec<(String, usize)> {
1143    let mut groups: Vec<(String, usize)> = Vec::new();
1144    for u in reg.units() {
1145        let g = group_name(u.dim());
1146        match groups.iter_mut().find(|(name, _)| *name == g) {
1147            Some((_, n)) => *n = n.saturating_add(1),
1148            None => groups.push((g, 1)),
1149        }
1150    }
1151    groups
1152}
1153
1154/// The group overview: one `group<TAB>units` row per group.
1155fn groups_text(groups: &[(String, usize)]) -> String {
1156    let mut s = String::from("# columns: group, units\n");
1157    for (g, n) in groups {
1158        let _ = writeln!(s, "{g}\t{n}");
1159    }
1160    s
1161}
1162
1163fn units_text(
1164    units: &[(String, &Unit)],
1165    ambiguous: &[&dimvexa::AmbiguousDef],
1166    mappings: &[Mapping],
1167    densities: &[Constant],
1168    user: &[ConfigUnit],
1169) -> String {
1170    let mut s =
1171        String::from("# columns: symbol, quantity_kind, exactness, prefixes, aliases, source\n");
1172    let mut current: Option<&str> = None;
1173    for (g, u) in units {
1174        if current != Some(g.as_str()) {
1175            let _ = writeln!(s, "\n## {g}");
1176            current = Some(g.as_str());
1177        }
1178        let _ = writeln!(
1179            s,
1180            "{}\t{}\t{}\t{}\t{}\t{}",
1181            cell(u.symbol()),
1182            u.kind().name(),
1183            u.exactness().name(),
1184            u.prefixes().name(),
1185            cell(&u.aliases().join(", ")),
1186            cell(&user_unit(user, u.symbol()).map_or_else(
1187                || u.source().to_string(),
1188                |c| format!("{} [yours: {}:{}]", u.source(), c.origin, c.line)
1189            ))
1190        );
1191    }
1192    if !ambiguous.is_empty() {
1193        s.push_str(
1194            "\n## ambiguous symbols (refused unless your config resolves them)\n\
1195# columns: symbol, candidates, planned, note\n",
1196        );
1197        for a in ambiguous {
1198            let note = mappings.iter().find(|m| m.name == a.symbol).map_or_else(
1199                || a.note.to_string(),
1200                |m| {
1201                    format!(
1202                        "resolved by config as {} ({}); {}",
1203                        m.target, m.origin, a.note
1204                    )
1205                },
1206            );
1207            let _ = writeln!(
1208                s,
1209                "{}\t{}\t{}\t{}",
1210                cell(a.symbol),
1211                cell(&a.candidates.join(", ")),
1212                cell(&a.planned.join(", ")),
1213                cell(&note)
1214            );
1215        }
1216    }
1217    if !mappings.is_empty() {
1218        s.push_str(
1219            "\n## config mappings (names your --config files resolve)\n# columns: name, unit, file:line\n",
1220        );
1221        for m in mappings {
1222            let _ = writeln!(
1223                s,
1224                "{}\t{}\t{}:{}",
1225                cell(&m.name),
1226                cell(&m.target),
1227                cell(&m.origin),
1228                m.line
1229            );
1230        }
1231    }
1232    if !densities.is_empty() {
1233        s.push_str(
1234            "\n## config densities (constants you supply; use with --density NAME)\n\
1235# columns: name, kind, value, unit, conditions, source, file:line\n",
1236        );
1237        for b in densities {
1238            let _ = writeln!(
1239                s,
1240                "{}\t{}\t{}\t{}\t{}\t{}\t{}:{}",
1241                cell(&b.name),
1242                b.kind.name(),
1243                b.value.to_fraction_string(),
1244                b.kind.unit(),
1245                cell(&b.conditions),
1246                cell(&b.source),
1247                cell(&b.origin),
1248                b.line
1249            );
1250        }
1251    }
1252    s
1253}
1254
1255fn units_json(
1256    groups: &[(String, usize)],
1257    units: &[(String, &Unit)],
1258    ambiguous: &[&dimvexa::AmbiguousDef],
1259    group: Option<&str>,
1260    loaded: &Loaded,
1261) -> String {
1262    let items: Vec<String> = units
1263        .iter()
1264        .map(|(g, u)| {
1265            let aliases: Vec<String> = u.aliases().iter().map(|a| json_string(a)).collect();
1266            let dims: Vec<String> = u.dim().exponents().iter().map(ToString::to_string).collect();
1267            format!(
1268                "{{\"symbol\":{},\"group\":{},\"aliases\":[{}],\"dimension\":[{}],\"quantity_kind\":{},\"exactness\":{},\"prefixes\":{},\"namespace\":{},\"source\":{},\"origin\":{}}}",
1269                json_string(u.symbol()),
1270                json_string(g),
1271                aliases.join(","),
1272                dims.join(","),
1273                json_string(u.kind().name()),
1274                json_string(u.exactness().name()),
1275                json_string(u.prefixes().name()),
1276                json_string(u.namespace()),
1277                json_string(u.source()),
1278                origin_json(user_unit(&loaded.units, u.symbol())),
1279            )
1280        })
1281        .collect();
1282    let strs = |v: &[&str]| {
1283        v.iter()
1284            .map(|x| json_string(x))
1285            .collect::<Vec<_>>()
1286            .join(",")
1287    };
1288    let ambiguous: Vec<String> = ambiguous
1289        .iter()
1290        .map(|a| {
1291            format!(
1292                "{{\"symbol\":{},\"candidates\":[{}],\"planned\":[{}],\"note\":{},\"resolved_by\":{}}}",
1293                json_string(a.symbol),
1294                strs(a.candidates),
1295                strs(a.planned),
1296                json_string(a.note),
1297                loaded
1298                    .mappings
1299                    .iter()
1300                    .find(|m| m.name == a.symbol)
1301                    .map_or_else(
1302                        || "null".to_string(),
1303                        |m| format!(
1304                            "{{\"unit\":{},\"file\":{},\"line\":{}}}",
1305                            json_string(&m.target),
1306                            json_string(&m.origin),
1307                            m.line
1308                        )
1309                    )
1310            )
1311        })
1312        .collect();
1313    let group_list: Vec<String> = groups
1314        .iter()
1315        .map(|(g, n)| format!("{{\"name\":{},\"units\":{n}}}", json_string(g)))
1316        .collect();
1317    let mappings: Vec<String> = loaded
1318        .mappings
1319        .iter()
1320        .map(|m| {
1321            format!(
1322                "{{\"name\":{},\"unit\":{},\"file\":{},\"line\":{}}}",
1323                json_string(&m.name),
1324                json_string(&m.target),
1325                json_string(&m.origin),
1326                m.line
1327            )
1328        })
1329        .collect();
1330    let group = group.map_or_else(
1331        || "null".to_string(),
1332        |g| json_string(&g.to_ascii_lowercase()),
1333    );
1334    format!(
1335        "{{\"schema\":{},\"ok\":true,\"data_version\":{},\"group\":{},\"groups\":[{}],\"units\":[{}],\"ambiguous\":[{}],\"config\":{},\"mappings\":[{}],\"densities\":[{}]}}",
1336        json_string(&schema_id("units")),
1337        json_string(&dimvexa::data_version()),
1338        group,
1339        group_list.join(","),
1340        items.join(","),
1341        ambiguous.join(","),
1342        configs_json(&loaded.configs),
1343        mappings.join(","),
1344        loaded
1345            .densities
1346            .iter()
1347            .map(density_json)
1348            .collect::<Vec<_>>()
1349            .join(",")
1350    )
1351}
1352
1353/// Everything shown for one unit (a table unit, or an expression such as `km`).
1354struct Detail {
1355    /// What was asked for (a symbol, an alias or a mapped name).
1356    queried: String,
1357    symbol: String,
1358    group: String,
1359    aliases: Vec<String>,
1360    dim: Dim,
1361    quantity_kind: &'static str,
1362    exactness: &'static str,
1363    /// Only for a table unit.
1364    prefixes: Option<&'static str>,
1365    namespace: Option<String>,
1366    factor: String,
1367    offset: String,
1368    sources: Vec<String>,
1369    note: Option<String>,
1370    /// `(file, line)` of the config that defines the unit, if it is a user unit.
1371    origin: Option<(String, usize)>,
1372    /// `(file, line)` of the config `[map]` entry that makes the queried name mean this unit.
1373    mapping: Option<(String, usize)>,
1374}
1375
1376fn unit_detail(
1377    reg: &Registry,
1378    user: &[ConfigUnit],
1379    mappings: &[Mapping],
1380    query: &str,
1381) -> Result<Detail, Error> {
1382    if let Some(u) = reg.get(query) {
1383        return Ok(Detail {
1384            queried: query.to_string(),
1385            symbol: u.symbol().to_string(),
1386            group: group_name(u.dim()),
1387            aliases: u.aliases().to_vec(),
1388            dim: u.dim(),
1389            quantity_kind: u.kind().name(),
1390            exactness: u.exactness().name(),
1391            prefixes: Some(u.prefixes().name()),
1392            namespace: Some(u.namespace().to_string()),
1393            factor: u.factor().to_fraction_string(),
1394            offset: u.offset().to_fraction_string(),
1395            sources: vec![u.source().to_string()],
1396            note: Some(u.note().to_string()),
1397            origin: user_unit(user, u.symbol()).map(|c| (c.origin.clone(), c.line)),
1398            mapping: mappings
1399                .iter()
1400                .find(|m| m.name == query)
1401                .map(|m| (m.origin.clone(), m.line)),
1402        });
1403    }
1404    // Not a table symbol: a prefixed or compound expression such as km or kW.h, or an
1405    // error (unknown, ambiguous) that the library words.
1406    let e = reg.parse(query)?;
1407    Ok(Detail {
1408        queried: query.to_string(),
1409        symbol: query.to_string(),
1410        group: group_name(e.dim()),
1411        aliases: Vec::new(),
1412        dim: e.dim(),
1413        quantity_kind: e.kind().name(),
1414        exactness: e.exactness().name(),
1415        prefixes: None,
1416        namespace: None,
1417        factor: e.factor().to_fraction_string(),
1418        offset: e.offset().to_fraction_string(),
1419        sources: e.sources().to_vec(),
1420        note: None,
1421        origin: None,
1422        mapping: None,
1423    })
1424}
1425
1426fn detail_text(d: &Detail) -> String {
1427    let none = |s: &str| {
1428        if s.is_empty() {
1429            "-".to_string()
1430        } else {
1431            s.to_string()
1432        }
1433    };
1434    let mut s = format!("{}\n", d.symbol);
1435    let mut line = |k: &str, v: String| {
1436        let _ = writeln!(s, "  {k:<14}{}", cell(&v));
1437    };
1438    line("group", d.group.clone());
1439    line("aliases", none(&d.aliases.join(", ")));
1440    line("dimension", d.dim.to_string());
1441    line("quantity kind", d.quantity_kind.to_string());
1442    line("exactness", d.exactness.to_string());
1443    if let Some(p) = d.prefixes {
1444        line("prefixes", p.to_string());
1445    }
1446    if let Some(n) = &d.namespace {
1447        line("namespace", n.clone());
1448    }
1449    line("factor to SI", d.factor.clone());
1450    line("offset to SI", d.offset.clone());
1451    for src in &d.sources {
1452        line("source", src.clone());
1453    }
1454    if let Some(n) = d.note.as_deref().filter(|n| !n.is_empty()) {
1455        line("note", n.to_string());
1456    }
1457    if let Some((file, at)) = &d.mapping {
1458        line(
1459            "your name",
1460            format!(
1461                "'{query}' means this unit in your config ({file}:{at})",
1462                query = d.queried
1463            ),
1464        );
1465    }
1466    if let Some((file, at)) = &d.origin {
1467        line(
1468            "defined in",
1469            format!("{file}:{at} (your config, not the shipped table)"),
1470        );
1471    }
1472    s
1473}
1474
1475fn detail_json(d: &Detail) -> String {
1476    let list = |v: &[String]| {
1477        v.iter()
1478            .map(|x| json_string(x))
1479            .collect::<Vec<_>>()
1480            .join(",")
1481    };
1482    let opt = |v: Option<&str>| v.map_or_else(|| "null".to_string(), json_string);
1483    let dims: Vec<String> = d.dim.exponents().iter().map(ToString::to_string).collect();
1484    format!(
1485        "{{\"schema\":{},\"ok\":true,\"data_version\":{},\"unit\":{{\"symbol\":{},\"group\":{},\"aliases\":[{}],\"dimension\":[{}],\"quantity_kind\":{},\"exactness\":{},\"prefixes\":{},\"namespace\":{},\"factor\":{},\"offset\":{},\"sources\":[{}],\"note\":{},\"origin\":{},\"mapping\":{}}}}}",
1486        json_string(&schema_id("units")),
1487        json_string(&dimvexa::data_version()),
1488        json_string(&d.symbol),
1489        json_string(&d.group),
1490        list(&d.aliases),
1491        dims.join(","),
1492        json_string(d.quantity_kind),
1493        json_string(d.exactness),
1494        opt(d.prefixes),
1495        opt(d.namespace.as_deref()),
1496        json_string(&d.factor),
1497        json_string(&d.offset),
1498        list(&d.sources),
1499        opt(d.note.as_deref()),
1500        d.origin.as_ref().map_or_else(
1501            || "null".to_string(),
1502            |(f, l)| format!("{{\"file\":{},\"line\":{l}}}", json_string(f)),
1503        ),
1504        d.mapping.as_ref().map_or_else(
1505            || "null".to_string(),
1506            |(f, l)| {
1507                format!(
1508                    "{{\"name\":{},\"file\":{},\"line\":{l}}}",
1509                    json_string(&d.queried),
1510                    json_string(f)
1511                )
1512            },
1513        ),
1514    )
1515}
1516
1517/// Runs `dimvexa units`; returns the exit code.
1518fn run_units(
1519    target: &UnitsTarget,
1520    config_files: &[String],
1521    json: bool,
1522    out: &mut dyn Write,
1523    err: &mut dyn Write,
1524) -> u8 {
1525    let loaded = match load(config_files) {
1526        Ok(l) => l,
1527        Err(e) => return report_load_error(&e, json, "units", out, err),
1528    };
1529    let mut fail = |code: &str, message: &str, suggestions: &[String], status: u8| -> u8 {
1530        if json {
1531            let _ = writeln!(
1532                out,
1533                "{}",
1534                json_error("units", code, message, &[], suggestions)
1535            );
1536        } else {
1537            let _ = writeln!(err, "error[{code}]: {message}");
1538        }
1539        status
1540    };
1541    let reg = &loaded.reg;
1542    let groups = group_counts(reg);
1543    let (selected, group) = match target {
1544        UnitsTarget::Groups => {
1545            let text = if json {
1546                units_json(&groups, &[], &[], None, &loaded)
1547            } else {
1548                groups_text(&groups)
1549            };
1550            let _ = write!(out, "{text}");
1551            if json {
1552                let _ = writeln!(out);
1553            }
1554            return 0;
1555        }
1556        UnitsTarget::All => (None, None),
1557        UnitsTarget::Query(q) => {
1558            let q_lower = q.to_ascii_lowercase();
1559            if groups.iter().any(|(g, _)| *g == q_lower) {
1560                (Some(q_lower.clone()), Some(q_lower))
1561            } else {
1562                return match unit_detail(reg, &loaded.units, &loaded.mappings, q) {
1563                    Ok(d) => {
1564                        let text = if json {
1565                            detail_json(&d)
1566                        } else {
1567                            detail_text(&d)
1568                        };
1569                        let _ = write!(out, "{text}");
1570                        if json {
1571                            let _ = writeln!(out);
1572                        }
1573                        0
1574                    }
1575                    Err(e) => {
1576                        let hint = if e.code() == "unknown_unit" {
1577                            "; it is not a group either (see 'dimvexa units --groups')".to_string()
1578                        } else {
1579                            String::new()
1580                        };
1581                        let mut near = e.candidates().to_vec();
1582                        for sug in e.suggestions() {
1583                            if !near.contains(sug) {
1584                                near.push(sug.clone());
1585                            }
1586                        }
1587                        fail(e.code(), &format!("{e}{hint}"), &near, 1)
1588                    }
1589                };
1590            }
1591        }
1592    };
1593    let units: Vec<(String, &Unit)> = reg
1594        .units()
1595        .iter()
1596        .map(|u| (group_name(u.dim()), u))
1597        .filter(|(g, _)| selected.as_ref().is_none_or(|want| g == want))
1598        .collect();
1599    let ambiguous = select_ambiguous(&units, group.is_some());
1600    if json {
1601        let _ = writeln!(
1602            out,
1603            "{}",
1604            units_json(&groups, &units, &ambiguous, group.as_deref(), &loaded)
1605        );
1606    } else {
1607        let _ = write!(
1608            out,
1609            "{}",
1610            units_text(
1611                &units,
1612                &ambiguous,
1613                &loaded.mappings,
1614                &loaded.densities,
1615                &loaded.units,
1616            )
1617        );
1618    }
1619    0
1620}
1621
1622/// The columns of `dimvexa configs`: one row per shipped config.
1623fn configs_list_text() -> String {
1624    let mut s = String::from("# columns: name, names, units, densities, hash\n");
1625    for b in dimvexa_config::BUNDLED {
1626        match b.config() {
1627            Ok(c) => {
1628                let _ = writeln!(
1629                    s,
1630                    "{}\t{}\t{}\t{}\t{}",
1631                    b.name,
1632                    c.mappings().len(),
1633                    c.units().len(),
1634                    c.constants().len(),
1635                    c.hash()
1636                );
1637            }
1638            Err(e) => {
1639                let _ = writeln!(s, "{}\terror: {e}", b.name);
1640            }
1641        }
1642    }
1643    s
1644}
1645
1646fn configs_list_json() -> String {
1647    let items: Vec<String> = dimvexa_config::BUNDLED
1648        .iter()
1649        .filter_map(|b| {
1650            let c = b.config().ok()?;
1651            Some(format!(
1652                "{{\"name\":{},\"hash\":{},\"mappings\":{},\"units\":{},\"densities\":{}}}",
1653                json_string(b.name),
1654                json_string(c.hash()),
1655                c.mappings().len(),
1656                c.units().len(),
1657                c.constants().len()
1658            ))
1659        })
1660        .collect();
1661    format!(
1662        "{{\"schema\":{},\"ok\":true,\"configs\":[{}]}}",
1663        json_string(&schema_id("configs")),
1664        items.join(",")
1665    )
1666}
1667
1668/// What one config contains, as text: its names, units and densities with their sources.
1669fn config_detail_text(c: &Config) -> String {
1670    let mut s = format!("{} (hash {})\n", cell(c.origin()), c.hash());
1671    if !c.mappings().is_empty() {
1672        s.push_str(
1673            "\n## names (what they mean once the config is loaded)\n# columns: name, unit\n",
1674        );
1675        for m in c.mappings() {
1676            let _ = writeln!(s, "{}\t{}", cell(&m.name), cell(&m.target));
1677        }
1678    }
1679    if !c.units().is_empty() {
1680        s.push_str("\n## units\n# columns: symbol, base, factor, exactness, source\n");
1681        for u in c.units() {
1682            let _ = writeln!(
1683                s,
1684                "{}\t{}\t{}\t{}\t{}",
1685                cell(&u.symbol),
1686                cell(&u.base),
1687                u.factor.to_fraction_string(),
1688                u.exactness.name(),
1689                cell(&u.source)
1690            );
1691        }
1692    }
1693    if !c.constants().is_empty() {
1694        s.push_str(
1695            "\n## densities (use with --density NAME)\n# columns: name, value, unit, conditions, source\n",
1696        );
1697        for d in c.constants() {
1698            let _ = writeln!(
1699                s,
1700                "{}\t{}\t{}\t{}\t{}",
1701                cell(&d.name),
1702                d.value.to_fraction_string(),
1703                d.kind.unit(),
1704                cell(&d.conditions),
1705                cell(&d.source)
1706            );
1707        }
1708    }
1709    s
1710}
1711
1712fn config_detail_json(c: &Config) -> String {
1713    let mappings: Vec<String> = c
1714        .mappings()
1715        .iter()
1716        .map(|m| {
1717            format!(
1718                "{{\"name\":{},\"unit\":{},\"line\":{}}}",
1719                json_string(&m.name),
1720                json_string(&m.target),
1721                m.line
1722            )
1723        })
1724        .collect();
1725    let units: Vec<String> = c
1726        .units()
1727        .iter()
1728        .map(|u| {
1729            format!(
1730                "{{\"symbol\":{},\"base\":{},\"factor\":{},\"exactness\":{},\"source\":{},\"line\":{}}}",
1731                json_string(&u.symbol),
1732                json_string(&u.base),
1733                json_string(&u.factor.to_fraction_string()),
1734                json_string(u.exactness.name()),
1735                json_string(&u.source),
1736                u.line
1737            )
1738        })
1739        .collect();
1740    let densities: Vec<String> = c.constants().iter().map(density_json).collect();
1741    format!(
1742        "{{\"schema\":{},\"ok\":true,\"config\":{{\"name\":{},\"hash\":{},\"mappings\":[{}],\"units\":[{}],\"densities\":[{}]}}}}",
1743        json_string(&schema_id("configs")),
1744        json_string(c.origin()),
1745        json_string(c.hash()),
1746        mappings.join(","),
1747        units.join(","),
1748        densities.join(",")
1749    )
1750}
1751
1752/// Runs `dimvexa configs [NAME_OR_PATH]`; returns the exit code.
1753fn run_configs(target: Option<&str>, json: bool, out: &mut dyn Write, err: &mut dyn Write) -> u8 {
1754    let text = match target {
1755        None if json => configs_list_json(),
1756        None => configs_list_text(),
1757        Some(spec) => match read_spec(spec) {
1758            Ok(c) if json => config_detail_json(&c),
1759            Ok(c) => config_detail_text(&c),
1760            Err(e) => {
1761                let suggestions: Vec<String> = if matches!(e, LoadError::Io(_)) {
1762                    dimvexa_config::BUNDLED
1763                        .iter()
1764                        .map(|b| b.name.to_string())
1765                        .collect()
1766                } else {
1767                    Vec::new()
1768                };
1769                let (code, message, status) = match &e {
1770                    LoadError::Io(m) => ("usage", m.clone(), 2),
1771                    LoadError::Lib(e) => (e.code(), e.to_string(), 1),
1772                };
1773                if json {
1774                    let _ = writeln!(
1775                        out,
1776                        "{}",
1777                        json_error("configs", code, &message, &[], &suggestions)
1778                    );
1779                } else {
1780                    let _ = writeln!(err, "error[{code}]: {message}");
1781                }
1782                return status;
1783            }
1784        },
1785    };
1786    let _ = write!(out, "{text}");
1787    if json {
1788        let _ = writeln!(out);
1789    }
1790    0
1791}
1792
1793/// Runs the command line `args` (without the program name), writing to `out` and `err`.
1794/// Returns the process exit code.
1795pub fn run<I>(args: I, out: &mut dyn Write, err: &mut dyn Write) -> u8
1796where
1797    I: IntoIterator<Item = String>,
1798{
1799    run_with(args, out, err, Color::default())
1800}
1801
1802/// Like [`run`], with ANSI colour on the streams `color` names. JSON output is never coloured.
1803pub fn run_with<I>(args: I, out: &mut dyn Write, err: &mut dyn Write, color: Color) -> u8
1804where
1805    I: IntoIterator<Item = String>,
1806{
1807    let mut args: Vec<String> = args.into_iter().collect();
1808    let mut color = color;
1809    match take_color_option(&mut args) {
1810        Ok(Some(when)) => {
1811            color = match when {
1812                ColorWhen::Always => Color {
1813                    out: true,
1814                    err: true,
1815                },
1816                ColorWhen::Never => Color {
1817                    out: false,
1818                    err: false,
1819                },
1820                ColorWhen::Auto => color,
1821            };
1822        }
1823        Ok(None) => {}
1824        Err(message) => {
1825            if args.iter().any(|a| a == "--json" || a == "--pretty") {
1826                let line = json_error(command_schema(&args), "usage", &message, &[], &[]);
1827                let _ = writeln!(out, "{line}");
1828            } else {
1829                let _ = writeln!(err, "error[usage]: {message}\n\n{USAGE}");
1830            }
1831            return 2;
1832        }
1833    }
1834    if args.iter().any(|a| a == "--pretty") {
1835        // `--pretty` is `--json` with indentation.
1836        args.retain(|a| a != "--pretty");
1837        if !args.iter().any(|a| a == "--json") {
1838            args.push("--json".to_string());
1839        }
1840        let mut buffered = Vec::new();
1841        let code = run_with(args, &mut buffered, err, color);
1842        let text = String::from_utf8_lossy(&buffered);
1843        for line in text.lines() {
1844            let _ = writeln!(out, "{}", pretty_json(line));
1845        }
1846        return code;
1847    }
1848    if !color.err {
1849        return run_inner(args, out, err, color);
1850    }
1851    let mut buffered = Vec::new();
1852    let code = run_inner(args, out, &mut buffered, color);
1853    let text = String::from_utf8_lossy(&buffered);
1854    let _ = write!(err, "{}", colorize_diagnostics(&text));
1855    code
1856}
1857
1858/// Prints a usage error (JSON on stdout for `--json`, text with the usage on stderr); returns 2.
1859fn report_usage_error(u: &UsageError, out: &mut dyn Write, err: &mut dyn Write) -> u8 {
1860    if u.json {
1861        let _ = writeln!(
1862            out,
1863            "{}",
1864            json_error(u.schema, "usage", &u.message, &[], &u.suggestions)
1865        );
1866    } else {
1867        let _ = writeln!(err, "error[usage]: {}\n\n{USAGE}", u.message);
1868    }
1869    2
1870}
1871
1872fn run_inner<I>(args: I, out: &mut dyn Write, err: &mut dyn Write, color: Color) -> u8
1873where
1874    I: IntoIterator<Item = String>,
1875{
1876    let args: Vec<String> = args.into_iter().collect();
1877    let (parsed, config_files) = match parse_args(&args) {
1878        Ok(p) => p,
1879        Err(u) => return report_usage_error(&u, out, err),
1880    };
1881    let args = match parsed {
1882        Parsed::Help => {
1883            let _ = write!(out, "{}", help_text());
1884            return 0;
1885        }
1886        Parsed::Version => {
1887            let _ = writeln!(
1888                out,
1889                "{} {} (data {})",
1890                dimvexa::PROJECT_NAME,
1891                env!("CARGO_PKG_VERSION"),
1892                dimvexa::data_version()
1893            );
1894            let _ = write!(out, "{}", about());
1895            return 0;
1896        }
1897        Parsed::Units { target, json } => {
1898            return run_units(&target, &config_files, json, out, err);
1899        }
1900        Parsed::Configs { target, json } => {
1901            return run_configs(target.as_deref(), json, out, err);
1902        }
1903        Parsed::Update { check, yes } => return update::run(check, yes, out, err),
1904        Parsed::Convert(a) => a,
1905    };
1906    let loaded = match load(&config_files) {
1907        Ok(l) => l,
1908        Err(e) => return report_load_error(&e, args.json, "convert", out, err),
1909    };
1910    let density = match &args.density {
1911        None => None,
1912        Some(spec) => match resolve_density(spec, &loaded) {
1913            Ok(d) => Some(d),
1914            Err(e) => {
1915                let (code, message, candidates, suggestions, status) = match &e {
1916                    DensityError::Usage(m, known) => {
1917                        ("usage", m.clone(), Vec::new(), known.clone(), 2)
1918                    }
1919                    DensityError::Lib(e) => (
1920                        e.code(),
1921                        format!("--density: {e}"),
1922                        e.candidates().to_vec(),
1923                        e.suggestions().to_vec(),
1924                        1,
1925                    ),
1926                };
1927                if args.json {
1928                    let line = json_error("convert", code, &message, &candidates, &suggestions);
1929                    let _ = writeln!(out, "{line}");
1930                } else {
1931                    let _ = writeln!(err, "error[{code}]: {message}");
1932                }
1933                return status;
1934            }
1935        },
1936    };
1937    match compute(&args, &loaded, density.as_ref()) {
1938        Ok(outcome) => {
1939            let text = if args.json {
1940                match render_json(&outcome) {
1941                    Ok(t) => t,
1942                    Err(e) => return fail(&args, &e, out, err),
1943                }
1944            } else {
1945                render_text(&outcome, color.out)
1946            };
1947            let _ = writeln!(out, "{text}");
1948            if !args.json {
1949                match (outcome.density, &args.density) {
1950                    (Some(b), _) => {
1951                        let _ = writeln!(err, "{}", density_note(b));
1952                    }
1953                    (None, Some(spec)) => {
1954                        let _ = writeln!(
1955                            err,
1956                            "note: density {spec} was not needed: both units have the same dimension"
1957                        );
1958                    }
1959                    (None, None) => {}
1960                }
1961            }
1962            0
1963        }
1964        Err(e) => fail(&args, &e, out, err),
1965    }
1966}
1967
1968fn fail(args: &Args, e: &Error, out: &mut dyn Write, err: &mut dyn Write) -> u8 {
1969    if args.json {
1970        let _ = writeln!(
1971            out,
1972            "{}",
1973            json_error(
1974                "convert",
1975                e.code(),
1976                &e.to_string(),
1977                e.candidates(),
1978                e.suggestions()
1979            )
1980        );
1981    } else {
1982        let _ = writeln!(err, "error[{}]: {e}", e.code());
1983        if let Error::DimensionMismatch { from, to } = e {
1984            let bare = |w: &str| matches!(w, "F" | "C");
1985            for (word, other, other_word) in
1986                [(&args.to, *from, &args.from), (&args.from, *to, &args.to)]
1987            {
1988                // A bare F or C beside a temperature, or beside another bare F or C
1989                // (`0 C F`), is almost always a temperature scale.
1990                if bare(word) && (other == Dim::K || bare(other_word)) {
1991                    let (what, fix) = if word == "F" {
1992                        ("farad", "degF or °F")
1993                    } else {
1994                        ("coulomb", "degC or °C")
1995                    };
1996                    let _ = writeln!(
1997                        err,
1998                        "hint: '{word}' alone is the {what}, not a temperature scale; for the scale write {fix}"
1999                    );
2000                }
2001            }
2002            if Constant::density_needed(*from, *to) && args.density.is_none() {
2003                let _ = writeln!(
2004                    err,
2005                    "hint: mass and volume need a density. Pass your own number, for example \
2006--density 998.2067kg/m3, or a named one: --config water --density water_25C \
2007(docs/adr/014-one-config-mechanism.md)"
2008                );
2009            }
2010        }
2011    }
2012    1
2013}
2014
2015#[cfg(test)]
2016mod tests {
2017    use super::*;
2018
2019    fn run_str(args: &[&str]) -> (u8, String, String) {
2020        let (mut out, mut err) = (Vec::new(), Vec::new());
2021        let code = run(args.iter().map(ToString::to_string), &mut out, &mut err);
2022        (
2023            code,
2024            String::from_utf8(out).unwrap(),
2025            String::from_utf8(err).unwrap(),
2026        )
2027    }
2028
2029    #[test]
2030    fn escapes_json() {
2031        assert_eq!(json_string("a\"b\\c\n\u{1}é"), "\"a\\\"b\\\\c\\n\\u0001é\"");
2032    }
2033
2034    #[test]
2035    fn f64_text_forms() {
2036        assert_eq!(f64_text(36.0), "36");
2037        assert_eq!(f64_text(0.0), "0");
2038        assert_eq!(f64_text(1e300), "1e300");
2039        assert_eq!(f64_text(2.5e-7), "2.5e-7");
2040        assert_eq!(f64_text(-0.5), "-0.5");
2041    }
2042
2043    #[test]
2044    fn basic_conversion_and_negative_numbers() {
2045        assert_eq!(
2046            run_str(&["convert", "10", "m/s", "km/h"]),
2047            (0, "36 km/h\n".into(), String::new())
2048        );
2049        assert_eq!(run_str(&["-40", "degF", "degC"]).1, "-40 degC\n");
2050    }
2051
2052    #[test]
2053    fn units_lists_the_table() {
2054        let (code, out, _) = run_str(&["units"]);
2055        assert_eq!(code, 0);
2056        assert!(out.lines().any(|l| l == "## pressure"));
2057        assert!(out.lines().any(|l| l.starts_with("psi\t")));
2058        assert!(out.lines().any(|l| l.starts_with("ton\t")));
2059        let (code, out, _) = run_str(&["units", "--groups"]);
2060        assert_eq!(code, 0);
2061        assert!(out.lines().any(|l| l == "volume\t12"));
2062        let (code, out, _) = run_str(&["units", "psi"]);
2063        assert_eq!(code, 0);
2064        assert!(out.contains("group         pressure"));
2065        let (code, out, _) = run_str(&["units", "volume"]);
2066        assert_eq!(code, 0);
2067        assert!(out.contains("gal_us\t") && !out.contains("psi\t"));
2068        assert!(out.contains("\ngal\t"), "ambiguous gal belongs to volume");
2069        let (code, out, _) = run_str(&["units", "--json"]);
2070        assert_eq!(code, 0);
2071        assert!(out.contains("\"schema\":\"dimvexa.units/2\""));
2072        assert_eq!(run_str(&["units", "--exact"]).0, 2);
2073        assert_eq!(run_str(&["units", "nope"]).0, 1);
2074        assert_eq!(run_str(&["units", "--groups", "psi"]).0, 2);
2075        assert_eq!(run_str(&["1", "m", "km", "--groups"]).0, 2);
2076        assert_eq!(run_str(&["units", "length", "mass"]).0, 2);
2077        assert_eq!(run_str(&["convert", "units"]).0, 2);
2078    }
2079
2080    #[test]
2081    fn forgives_degree_spacing_and_to() {
2082        for args in [
2083            ["0", "°C", "°F"].as_slice(),
2084            &["0", "°C", "°", "F"],
2085            &["0", "°C", "to", "°F"],
2086            &["convert", "0", "°C", "to", "°", "F"],
2087        ] {
2088            assert_eq!(
2089                run_str(args),
2090                (0, "32 °F\n".into(), String::new()),
2091                "{args:?}"
2092            );
2093        }
2094    }
2095
2096    #[test]
2097    fn bare_f_after_a_temperature_hints_at_degf() {
2098        let (code, _, err) = run_str(&["0", "°C", "F"]);
2099        assert_eq!(code, 1);
2100        assert!(err.contains("'F' alone is the farad"), "{err}");
2101        assert!(err.contains("write degF or °F"), "{err}");
2102    }
2103
2104    #[test]
2105    fn normalisation_edges() {
2106        // the masculine ordinal sign joins; an angle degree before a real unit does not.
2107        assert_eq!(run_str(&["0", "°C", "º", "F"]).1, "32 °F\n");
2108        assert_eq!(run_str(&["180", "°", "rad"]).0, 0);
2109        assert_eq!(run_str(&["0", "°C", "to"]).0, 1);
2110        assert_eq!(run_str(&["units", "°", "F"]).0, 2);
2111    }
2112
2113    #[test]
2114    fn scale_hints_cover_both_sides() {
2115        let hint = |a: &[&str]| run_str(a).2;
2116        assert!(hint(&["32", "F", "°C"]).contains("'F' alone is the farad"));
2117        assert!(hint(&["0", "°C", "C"]).contains("'C' alone is the coulomb"));
2118        assert!(hint(&["0", "C", "F"]).contains("write degC or °C"));
2119        assert!(hint(&["0", "C", "F"]).contains("write degF or °F"));
2120        assert!(!hint(&["1", "m", "F"]).contains("hint:"));
2121    }
2122
2123    #[test]
2124    fn diagnostics_colour_labels_only() {
2125        let text = "error[usage]: x\nhint: y\nnote: z\nerror[ broken\nplain\n";
2126        let c = colorize_diagnostics(text);
2127        assert!(c.contains("\x1b[1;31merror[usage]\x1b[0m: x"));
2128        assert!(c.contains("\x1b[36mhint:\x1b[0m y"));
2129        assert!(c.contains("\x1b[33mnote:\x1b[0m z"));
2130        assert!(c.contains("error[ broken\nplain\n"));
2131    }
2132
2133    #[test]
2134    fn pretty_passes_help_and_version_through() {
2135        assert_eq!(run_str(&["--help", "--pretty"]).1, run_str(&["--help"]).1);
2136        assert_eq!(
2137            run_str(&["--version", "--pretty"]).1,
2138            run_str(&["--version"]).1
2139        );
2140        let both = run_str(&["1", "m", "km", "--pretty", "--json"]).1;
2141        assert_eq!(both, run_str(&["1", "m", "km", "--pretty"]).1);
2142        let (_, units, _) = run_str(&["units", "gal", "--pretty"]);
2143        assert!(units.starts_with("{\n"), "{units}");
2144    }
2145
2146    #[test]
2147    fn color_option_overrides_the_default() {
2148        let run = |a: &[&str], c: Color| {
2149            let (mut out, mut err) = (Vec::new(), Vec::new());
2150            let code = run_with(a.iter().map(ToString::to_string), &mut out, &mut err, c);
2151            (
2152                code,
2153                String::from_utf8(out).unwrap(),
2154                String::from_utf8(err).unwrap(),
2155            )
2156        };
2157        let on = Color {
2158            out: true,
2159            err: true,
2160        };
2161        let off = Color::default();
2162        assert!(
2163            run(&["1", "m", "km", "--color=always"], off)
2164                .1
2165                .contains('\x1b')
2166        );
2167        assert!(
2168            run(&["--color", "always", "1", "m", "km"], off)
2169                .1
2170                .contains('\x1b')
2171        );
2172        assert!(
2173            !run(&["1", "m", "km", "--color=never"], on)
2174                .1
2175                .contains('\x1b')
2176        );
2177        assert!(
2178            run(&["1", "m", "km", "--color=auto"], on)
2179                .1
2180                .contains('\x1b')
2181        );
2182        assert!(
2183            !run(&["1", "m", "km", "--color=always", "--json"], off)
2184                .1
2185                .contains('\x1b')
2186        );
2187        let (code, _, err) = run(&["1", "m", "km", "--color=sometimes"], off);
2188        assert_eq!(code, 2);
2189        assert!(err.contains("auto, always or never"));
2190        assert_eq!(run(&["1", "m", "km", "--color"], off).0, 2);
2191    }
2192
2193    #[test]
2194    fn color_only_where_asked() {
2195        let (mut out, mut err) = (Vec::new(), Vec::new());
2196        let on = Color {
2197            out: true,
2198            err: true,
2199        };
2200        let args = ["0", "°C", "F"].map(String::from);
2201        run_with(args, &mut out, &mut err, on);
2202        assert!(
2203            String::from_utf8(err)
2204                .unwrap()
2205                .contains("\x1b[1;31merror[dimension_mismatch]\x1b[0m")
2206        );
2207        let args = ["0", "degC", "degF"].map(String::from);
2208        let mut out = Vec::new();
2209        run_with(args, &mut out, &mut Vec::new(), on);
2210        assert_eq!(
2211            String::from_utf8(out).unwrap(),
2212            "\x1b[1;32m32\x1b[0m \x1b[36mdegF\x1b[0m\n"
2213        );
2214        let (_, json, _) = run_str(&["0", "degC", "degF", "--json"]);
2215        assert!(!json.contains('\x1b'));
2216    }
2217
2218    #[test]
2219    fn pretty_is_json_with_indentation() {
2220        let (code, out, _) = run_str(&["100", "degC", "degF", "--pretty"]);
2221        assert_eq!(code, 0);
2222        assert!(
2223            out.contains("\n  \"output\": {\n    \"value\": \"212\""),
2224            "{out}"
2225        );
2226        assert!(
2227            out.contains("\"dimension\": [0, 0, 0, 0, 1, 0, 0]"),
2228            "{out}"
2229        );
2230        let compact = run_str(&["100", "degC", "degF", "--json"]).1;
2231        assert_eq!(pretty_json(compact.trim()), out.trim_end());
2232        let (code, out, _) = run_str(&["1", "m", "--pretty"]);
2233        assert_eq!(code, 2);
2234        assert!(out.contains("\"code\": \"usage\""));
2235    }
2236
2237    #[test]
2238    fn usage_errors_exit_2() {
2239        assert_eq!(run_str(&[]).0, 2);
2240        assert_eq!(run_str(&["convert", "1", "m"]).0, 2);
2241        assert_eq!(run_str(&["1", "m", "km", "--nope"]).0, 2);
2242        let (code, out, _) = run_str(&["1", "m", "--json"]);
2243        assert_eq!(code, 2);
2244        assert!(out.contains("\"code\":\"usage\""));
2245    }
2246}