gd_report¶
Elm-style diagnostic reports shared by every Guidedog crate.
Libraries return structured errors; hosts turn them into a Report and render it with render_text for people or render_json for tools:
-- UNRESOLVED REFERENCE ----------------------------------------------- intro.rst:12:5
No target is named `instal`.
12| See instal_ for details.
^^^^^^^
Hint: Define it with `.. _instal:` above the section, or correct the spelling.
Columns count Unicode scalars, as editors do; terminal cell widths (wide East Asian characters, combining marks, escaped controls) only affect where carets are drawn. The crate has no dependencies and never prints.
use gd_report::{Report, Style, render_text};
let source = "[site]\ntitle = 1\n";
let start = source.find('1').unwrap();
let report = Report::error("conf.type", "WRONG TYPE", "`title` must be a string.")
.hint("Quote the value: `title = \"1\"`.")
.at("conf.toml", source, start, start + 1);
let text = render_text(&report, &Style { color: false, width: 80 });
assert!(text.contains("conf.toml:2:9"));
assert!(text.contains("Hint: Quote the value"));
Modules
gd_report::ansi– ANSI styling for terminals: the style codes reports and command lines use, text in a style, and whether a stream should be styled at all.
Structs
- pub struct gd_report::SymbolEnv¶
-
The environment variables that decide the symbols, read once by the caller so the decision itself is a pure function. Each field is the variable’s value, or
Nonewhen it is unset.Fields
- pub no_color: bool¶
-
Whether
NO_COLORis set: a request for plain output.
- pub windows_terminal: bool¶
-
Whether
WT_SESSIONis set: Windows Terminal, which is always UTF-8.
Implementations
- pub struct gd_report::Symbols¶
-
The symbols a command decorates its terminal output with. Plain data: pick
Symbols::UNICODEorSymbols::ASCII, or askterminal_symbols.Fields
- pub check: &'static str¶
-
Marks a finished step or a passing check.
- pub cross: &'static str¶
-
Marks a failed step.
- pub dot: &'static str¶
-
Separates short items on one line.
- pub ellipsis: &'static str¶
-
Ends a shortened text or list.
Implementations
- impl Symbols
Trait Implementations
- pub struct gd_report::Style¶
-
How
render_textlays a report out.Fields
- pub color: bool¶
-
Whether to use ANSI colors and bold; words never depend on them.
- pub width: usize¶
-
The width of the title rule and of wrapped prose, in terminal cells.
Trait Implementations
- pub struct gd_report::Excerpt¶
-
The source line a report points at, with the byte range to underline.
Fields
- pub start: usize¶
-
Byte offset in
textwhere the underline starts (clamped to a char boundary).
- pub end: usize¶
-
Byte offset in
textwhere the underline ends; at least one caret is drawn.
- pub struct gd_report::Report¶
-
A diagnostic ready to render: what happened, where, and what to do about it.
Fill it with struct-update syntax, or start from
Report::error,Report::warningorReport::noteand attach a position withReport::at.Fields
- pub line: usize¶
-
1-based line, or 0 when the report has no position.
- pub column: usize¶
-
1-based column counting Unicode scalars, or 0 when unknown.
- pub end_line: usize¶
-
1-based line where the reported span ends, or 0.
- pub end_column: usize¶
-
1-based column where the reported span ends (exclusive), or 0.
Implementations
- impl Report
-
- pub fn error(code: &str, title: &str, message: &str) -> Report¶
-
Starts an error report with a code, title and message; it has no position yet.
- pub fn warning(code: &str, title: &str, message: &str) -> Report¶
-
Starts a warning report with a code, title and message; it has no position yet.
Enums
- pub enum gd_report::Severity¶
-
How serious a report is.
Variants
- Error¶
-
The input cannot be used as it is.
- Warning¶
-
The result was produced but probably not as intended.
- Note¶
-
Extra information, such as where something was first defined.
Implementations
Functions
- pub fn gd_report::escape_json(out: &mut String, text: &str)¶
-
Appends
texttooutas a JSON string literal with its quotes, escaping quotes, backslashes and control characters (and U+2028/U+2029 for embedding in scripts).
- pub fn gd_report::render_json(report: &Report) -> String¶
-
Writes
reportas one JSON object on one line (schemaSCHEMA):schema,code,severity,title,message,hint(the hints joined by line breaks),hints,path,line,column,end_line,end_column,source(the complete source line of the excerpt, or"") andcategory. The terminal window and carets are left out; columns count Unicode scalars. Returns an allocated string without a line break;write_jsonappends to a reused buffer instead.
- pub fn gd_report::write_json(out: &mut String, report: &Report)¶
-
Appends the object
render_jsonreturns toout.
- pub fn gd_report::symbols_for(env: &SymbolEnv) -> Symbols¶
-
The symbols for the terminal
envdescribes: Unicode only when the locale is UTF-8 (or the terminal is Windows Terminal),TERMis notdumbandNO_COLORis unset.
- pub fn gd_report::terminal_symbols() -> Symbols¶
-
The symbols for this process’s terminal; see
symbols_for.
- pub fn gd_report::render_text(report: &Report, style: &Style) -> String¶
-
Lays
reportout the Elm way: a title rule naming the place, the explanation, the source line with carets under the span, and the hints. Every text from the report is escaped so control characters in file names or sources cannot reach the terminal; only the style’s own colors do. Returns an allocated string ending in a line break;write_textappends to a buffer the caller reuses instead.
- pub fn gd_report::wrap(text: &str, width: usize) -> String¶
-
Wraps
textat spaces so lines take at mostwidthcells where words allow, keeping its own line breaks and the indentation that starts each line. Returns allocated text ending in a line break (empty for empty text);write_wrappedappends instead.
- pub fn gd_report::write_text(out: &mut String, report: &Report, style: &Style)¶
-
Appends the text
render_textreturns toout, so a caller rendering many reports canclear()and reuse one buffer.
- pub fn gd_report::write_wrapped(out: &mut String, text: &str, width: usize)¶
-
Appends
textwrapped aswrapwraps it toout.
- pub fn gd_report::char_width(c: char) -> usize¶
-
The number of terminal cells
ctakes: 0 for controls and combining or zero-width characters, 2 for East Asian Wide and Fullwidth ones, 1 for the rest.
- pub fn gd_report::display_width(text: &str) -> usize¶
-
The number of terminal cells
texttakes, the sum ofchar_widthover its characters. Grapheme clusters such as emoji sequences are not joined.
- pub fn gd_report::escape_terminal(text: &str) -> Cow<'_, str>¶
-
Returns
textwith every control character except the line break escaped as\uXXXX(tabs included), so file names and sources cannot send escape sequences to a terminal. Borrowstextwhen it has nothing to escape and allocates otherwise.
- pub fn gd_report::is_control(c: char) -> bool¶
-
Reports whether
cis a C0 or C1 control character or DEL.
- pub fn gd_report::is_wide(c: char) -> bool¶
-
Reports whether
cis East Asian Wide or Fullwidth, taking two terminal cells.
- pub fn gd_report::is_zero_width(c: char) -> bool¶
-
Reports whether
cis a combining mark or another character drawn in no cell.
- pub fn gd_report::set_span(report: &mut Report, source: &str, start: usize, end: usize)¶
-
Fills
report’s line, columns and excerpt from bytesstart..endofsource. Offsets past the end are clamped and offsets inside a character move back to its start. The excerpt underlines the part of the span on its first line.
- pub fn gd_report::set_line_column(report: &mut Report, source: &str, line: usize, column: usize)¶
-
Fills
report’s position and excerpt from a 1-basedlineand scalarcolumnofsource, for positions that tools report without byte offsets. Does nothing when the line does not exist.
- pub fn gd_report::line_offset(text: &str, line: usize) -> Option<usize>¶
-
The byte offset where 1-based line
lineoftextstarts, orNonepast the end.
- pub fn gd_report::line_column(text: &str, offset: usize) -> (usize, usize)¶
-
The 1-based line and column of byte
offsetintext; the column counts Unicode scalars. Offsets past the end or inside a character are clamped down.
- pub fn gd_report::line_bounds(text: &str, offset: usize) -> (usize, usize)¶
-
The byte range of the line holding
offset, without its\nor\r\nending.
- pub fn gd_report::floor_boundary(text: &str, offset: usize) -> usize¶
-
Clamps
offsettotextand moves it back to the start of the character it is in.
- pub fn gd_report::edit_distance(a: &str, b: &str) -> usize¶
-
The Levenshtein distance between
aandb, counting Unicode scalars.
- pub fn gd_report::suggest<'a, I>(name: &str, candidates: I) -> Option<&'a str> where I: IntoIterator<Item = &'a str>¶
-
The candidate closest to
namewhen it is near enough to be a typo (one edit for names of up to three characters, two otherwise), for “Did you mean” hints. Borrows fromcandidates; returnsNonewhen nothing is close ornameitself is listed.
Constants
- pub const gd_report::SCHEMA: u32 = 1¶
-
The version of the JSON form
render_jsonwrites; it changes only when a field does.