Skip to main content

bynk/
explain.rs

1//! `bynk explain <code>` — the compiler's own explanation of a diagnostic code
2//! (#853), the analogue of `rustc --explain`.
3//!
4//! Every `bynk.*` diagnostic carries a stable machine code, but a code alone is
5//! a dead end for a newcomer. This subcommand prints the curated, *offline-
6//! complete* explanation behind a code — what the rule is, why it exists, and a
7//! minimal before/after example — plus a link to the relevant Book concept
8//! page. The same [`bynk_syntax::diagnostics::EXPLANATIONS`] table backs the
9//! editor's clickable diagnostic-code links (`codeDescription`), so the two
10//! surfaces never drift (DECISION A).
11//!
12//! Coverage is incremental (DECISION B): a code with no curated explanation is
13//! reported gracefully (its one-line registry summary, and a note that a longer
14//! explanation is not written yet), and a code the compiler does not recognise
15//! at all exits non-zero. Neither is an error state for the feature — a
16//! half-covered set is a designed, documented condition.
17
18use bynk_driver::emsg;
19use std::process::ExitCode;
20
21use bynk_syntax::diagnostics::{self, Explain};
22
23/// What `explain` found for a requested code.
24enum Lookup {
25    /// A curated explanation.
26    Explained(&'static Explain),
27    /// A real diagnostic code with no curated explanation yet; carries its
28    /// one-line registry summary.
29    KnownUnexplained(&'static str),
30    /// Not a code the compiler emits.
31    Unknown,
32}
33
34fn lookup(code: &str) -> Lookup {
35    if let Some(e) = diagnostics::explain(code) {
36        Lookup::Explained(e)
37    } else if let Some(info) = diagnostics::REGISTRY.iter().find(|d| d.code == code) {
38        Lookup::KnownUnexplained(info.summary)
39    } else {
40        Lookup::Unknown
41    }
42}
43
44/// The full explanation text for a curated code (the offline-complete answer).
45fn render_explained(e: &Explain) -> String {
46    let mut out = String::new();
47    out.push_str(e.code);
48    out.push_str("\n\n");
49    out.push_str(e.blurb);
50    out.push_str("\n\nExample:\n\n");
51    for line in e.example.lines() {
52        out.push_str("    ");
53        out.push_str(line);
54        out.push('\n');
55    }
56    out.push_str("\nLearn more: ");
57    out.push_str(&e.href());
58    out.push('\n');
59    out
60}
61
62/// The graceful message for a real code that has no curated explanation yet.
63fn render_known_unexplained(code: &str, summary: &str) -> String {
64    format!(
65        "{code}\n\n{summary}\n\n\
66         No extended explanation is written for this code yet. Its page links the \
67         other codes in its category:\n\n    {}\n",
68        diagnostics::href(code)
69    )
70}
71
72/// Run `bynk explain <code>`. Exits `0` for a recognised code (explained or
73/// not) and non-zero for an unrecognised one.
74pub fn run(code: &str) -> ExitCode {
75    match lookup(code) {
76        Lookup::Explained(e) => {
77            print!("{}", render_explained(e));
78            ExitCode::SUCCESS
79        }
80        Lookup::KnownUnexplained(summary) => {
81            print!("{}", render_known_unexplained(code, summary));
82            ExitCode::SUCCESS
83        }
84        Lookup::Unknown => {
85            emsg!(
86                "error: `{code}` is not a diagnostic code the compiler emits.\n\
87                 Diagnostic codes look like `bynk.resolve.unknown_type`; see \
88                 {} for the full list.",
89                diagnostics::href("")
90            );
91            ExitCode::FAILURE
92        }
93    }
94}
95
96#[cfg(test)]
97mod tests {
98    use super::*;
99
100    #[test]
101    fn explained_code_prints_blurb_example_and_href() {
102        let e = diagnostics::explain("bynk.resolve.unknown_type").expect("curated");
103        let out = render_explained(e);
104        assert!(out.contains("bynk.resolve.unknown_type"));
105        assert!(out.contains(e.blurb));
106        assert!(out.contains("Example:"));
107        // The hosted href is the offline-visible "learn more" target.
108        assert!(out.contains("https://bynk-lang.org/d/bynk.resolve.unknown_type"));
109    }
110
111    #[test]
112    fn known_but_unexplained_code_is_graceful() {
113        // Pick a real registry code with no curated explanation. If this ever
114        // gains one, swap it — the point is a code that exists but isn't curated.
115        let code = "bynk.wasm.panic";
116        assert!(diagnostics::explain(code).is_none());
117        assert!(matches!(lookup(code), Lookup::KnownUnexplained(_)));
118        let out = render_known_unexplained(code, "The in-browser compiler panicked internally.");
119        assert!(out.contains(code));
120        assert!(out.contains("No extended explanation"));
121        assert!(
122            out.contains("https://bynk-lang.org/d/bynk.wasm.panic\n"),
123            "{out}"
124        );
125    }
126
127    #[test]
128    fn unknown_code_is_not_found() {
129        assert!(matches!(lookup("bynk.not.a_real_code"), Lookup::Unknown));
130        assert!(matches!(lookup("nonsense"), Lookup::Unknown));
131    }
132}