Quick start
[]
= "0.1"
use *;
Output:
error: [E0001]: unexpected `;`
--> main.lang:1:8
|
1 | let x = ;
| ^ expected expression before `;`
|
= help: try `let x = <value>;`
How it works
You define an enum with your error codes and implement DiagnosticCode on it. That's it. The engine handles collecting, counting, and rendering.
DiagnosticEngine<C> collects diagnostics, tracks counts, renders output
Diagnostic<C> single error/warning with labels, notes, help
C: DiagnosticCode your enum
Label points at source code (span + message + style)
Span file + line + column + length
notes: Vec<String>
help: Option<String>
DiagnosticCode trait
API
Span
new
Just a location. Not tied to any lexer or parser.
Label
primary // ^^^^ main error site
secondary // ---- related context
Diagnostic
new
.with_label
.with_note
.with_help
Builder methods take impl Into<String>, so both &str and String work.
DiagnosticEngine
let mut engine = new;
engine.emit;
engine.emit_errors; // batch emit
engine.emit_warnings;
engine.extend; // merge two engines
engine.has_errors;
engine.has_warnings;
engine.error_count;
engine.warning_count;
engine.print_all; // colored terminal output
engine.format_all; // colored string (no print)
engine.format_all_plain; // plain text for logs/CI
engine.get_diagnostics; // &[Diagnostic<C>]
engine.get_errors; // Vec<&Diagnostic<C>>
engine.get_warnings;
engine.len;
engine.is_empty;
engine.clear;
Examples
Compiler - scanner/parser/semantic errors: examples/compiler.rs
SQL engine - unknown columns, division by zero, missing indexes: examples/sql_engine.rs
Config linter - duplicate keys, invalid values, deprecated fields: examples/config_linter.rs
API validator - missing fields, bad formats, deprecated endpoints: examples/api_validator.rs
All at once: cargo run --example demo
Contributing
See CONTRIBUTING.md for guidelines.
Security
See SECURITY.md for reporting vulnerabilities.
License
MIT - Copyright (c) 2024 @gentleduck