gentleduck/diagnostic
Generic diagnostic engine for Rust. Drops into compilers, linters, SQL engines, and config validators that need rich error output.
Links
- Crate: https://crates.io/crates/duck-diagnostic
- Docs: https://docs.rs/duck-diagnostic
- Repository: https://github.com/gentleduck/duck-diagnostic
Module Matrix
| Path | Role | Status |
|---|---|---|
src/diagnostic.rs |
Diagnostic, Label, Span, Severity, Suggestion, Applicability |
Active |
src/formatter.rs |
Terminal formatter with caret rendering, color, tab/Unicode-width handling | Active |
src/smart.rs |
Multi-file smart printer | Active |
src/compact.rs |
Source-less compact rendering and internal style helpers | Active |
src/json.rs |
Stable JSON schema output (feature-gated json) |
Active |
src/style.rs |
RenderOptions, color toggles, line width clamps |
Active |
src/macros.rs |
diag! macro |
Active |
Feature Matrix
| Feature | Default | Role |
|---|---|---|
json |
yes | Enables serde + serde_json, exposes format_all_json() |
Capability Matrix
| Capability | Notes |
|---|---|
| Multi-file diagnostics | Labels in different files render as separate sections |
| Suggestions / fix-its | with_suggestion(...) plus Applicability levels |
| rustc-style diff suggestions | - original / + replacement, red/green, aligned gutter |
Severity::Bug |
ICE category, separate count and color |
| JSON output | engine.format_all_json(), schema stable |
Span::from_zero_based |
Drop-in for parsers that emit 0-based positions |
Label::with_note |
Span-local note rendered under the caret |
| Tab + Unicode-width aware | Carets line up under emoji, CJK, tab indents |
SourceCache |
Split source once, reuse across diagnostics |
RenderOptions |
Tab width, context lines, max line width, color toggle |
diag! macro |
diag!(MyError::Foo, span, "msg") |
| Long-line truncation | RenderOptions::max_line_width clamps with ellipsis |
| Error code URLs | DiagnosticCode::url() rendered after the code |
Install
[]
= "0.7"
Usage
use *;
Output:
error: [E0001]: unexpected `;`
--> main.lang:1:8
|
1 | let x = ;
| ^ expected expression before `;`
|
= help: try `let x = <value>;`
Architecture
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>
API
DiagnosticCode
Severity variants: Bug, Error, Warning, Note, Help.
Span
new
from_zero_based
Label
primary // ^^^^ main site
secondary // ---- related context
Diagnostic
new
.with_label
.with_note
.with_help
.with_suggestion
Builder methods take impl Into<String>.
DiagnosticEngine
let mut engine = new;
engine.emit;
engine.emit_errors;
engine.emit_warnings;
engine.extend;
engine.has_errors;
engine.has_warnings;
engine.error_count;
engine.warning_count;
engine.print_all;
engine.format_all;
engine.format_all_plain;
engine.format_all_json;
engine.get_diagnostics;
engine.get_errors;
engine.get_warnings;
engine.len;
engine.is_empty;
engine.clear;
RenderOptions
let opts = RenderOptions ;
let s = engine.format_all_with;
SourceCache
let cache = new;
for d in engine.get_diagnostics
Suggestion
new
.with_suggestion;
diag!
let d = diag!.with_help;
Examples
| Path | Role |
|---|---|
examples/compiler.rs |
Scanner / parser / semantic errors |
examples/sql_engine.rs |
Unknown columns, division by zero, missing indexes |
examples/config_linter.rs |
Duplicate keys, invalid values, deprecated fields |
examples/api_validator.rs |
Missing fields, bad formats, deprecated endpoints |
examples/suggestion.rs |
Auto-applicable rewrites |
examples/diff_suggestions.rs |
rustc-style -/+ rendering, multi-line, applicability |
examples/multi_file.rs |
Labels across two files |
examples/json_output.rs |
LSP / IDE-friendly JSON |
examples/showcase.rs |
One of every diagnostic flavor in a fake compiler run |
examples/demo.rs |
All examples at once |
Contributing
See CONTRIBUTING.md and CODE_OF_CONDUCT.md.
Security
See SECURITY.md for reporting vulnerabilities.
License
MIT. See LICENSE.