11 releases
Uses new Rust 2024
| 0.1.10 | Feb 21, 2026 |
|---|---|
| 0.1.9 | Feb 16, 2026 |
#2415 in Parser implementations
Used in 2 crates
37KB
625 lines
zpl_toolchain_diagnostics
Shared diagnostic types for parser/validator/CLI.
Part of the zpl-toolchain project.
Code generation
Diagnostic ID constants (codes::ARITY, etc.) and the explain() function are
auto-generated from spec/diagnostics.jsonc (this crate's
crates/diagnostics/spec/diagnostics.jsonc) at build time via build.rs.
This makes the JSONC file the single source of truth — adding or updating a
diagnostic only requires editing the spec, and the Rust code regenerates
automatically.
Types
Span { start: usize, end: usize }-- canonical byte-offset span, re-exported bycore::grammar::diag.Severity:Error | Warn | Info. ImplementsDisplay(lowercase:error,warn,info).Diagnostic { id, severity, message, span, context }with convenience constructors:Diagnostic::error(),::warn(),::info().context: Option<BTreeMap<String, String>>— machine-readable structured metadata for tooling. UsesBTreeMapfor deterministic key ordering in JSON output. Attach via the.with_context(map)builder method. Omitted from serialized JSON whenNone.- Implements
Display— formats asseverity[id]: message(e.g.error[ZPL1101]: too many arguments). - Derives
PartialEq,Eqfor easy test assertions and exhaustive equality checks.
Functions
explain(code: &str) -> Option<&'static str>-- human-readable explanation for all 46 diagnostic codes (auto-generated).Diagnostic::explain(&self) -> Option<&'static str>-- convenience method that calls the freeexplain()function with the diagnostic's ownid.severity_for_code(code: &str) -> Option<Severity>-- default severity lookup from spec (auto-generated).message_template_for(code: &str, variant: &str) -> Option<&'static str>-- optional message template lookup from spec (auto-generated).
Guidance
- Use stable IDs:
ZPL1xxx(value-level),ZPL2xxx(structural/semantic),ZPL3xxx(notes),ZPL.PARSER.xxxx(parser). - Keep messages concise and actionable.
- To add a new diagnostic: add an entry to
spec/diagnostics.jsonc(in this crate) withid,constName,severity,category,summary,description,contextKeys, and optionallymessageTemplatesfor variant-specific message formatting. - See
docs/DIAGNOSTIC_CODES.mdfor the full reference.
Dependencies
~0.2–0.9MB
~19K SLoC