gd_report ========= .. rust:crate:: gd_report :synopsis: Elm-style diagnostic reports shared by every Guidedog crate. Elm-style diagnostic reports shared by every Guidedog crate. Libraries return structured errors; hosts turn them into a :rust:struct:`Report ` and render it with :rust:fn:`render_text ` for people or :rust:fn:`render_json ` for tools: .. code-block:: text -- 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. .. code-block:: rust 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")); .. rubric:: Modules - :rust:mod:`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. .. rubric:: Structs .. rust:struct:: pub struct 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 ``None`` when it is unset. .. rubric:: Fields .. rust:field:: pub lc_all: Option ``LC_ALL``, which overrides the other locale variables. .. rust:field:: pub lc_ctype: Option ``LC_CTYPE``, the locale of the character set. .. rust:field:: pub lang: Option ``LANG``, the default locale. .. rust:field:: pub term: Option ``TERM``; ``dumb`` means a terminal without Unicode or styling. .. rust:field:: pub no_color: bool Whether ``NO_COLOR`` is set: a request for plain output. .. rust:field:: pub windows_terminal: bool Whether ``WT_SESSION`` is set: Windows Terminal, which is always UTF-8. .. rubric:: Implementations .. rust:impl:: impl SymbolEnv :noindex: :for: SymbolEnv .. rust:method:: pub fn from_process() -> SymbolEnv Reads the variables from the process environment. Never fails: an unreadable (non-Unicode) value counts as unset. .. rust:struct:: pub struct Symbols The symbols a command decorates its terminal output with. Plain data: pick :rust:const:`Symbols::UNICODE ` or :rust:const:`Symbols::ASCII `, or ask :rust:fn:`terminal_symbols `. .. rubric:: Fields .. rust:field:: pub check: &'static str Marks a finished step or a passing check. .. rust:field:: pub cross: &'static str Marks a failed step. .. rust:field:: pub dot: &'static str Separates short items on one line. .. rust:field:: pub ellipsis: &'static str Ends a shortened text or list. .. rubric:: Implementations .. rust:impl:: impl Symbols :noindex: :for: Symbols .. rust:associatedconst:: pub const UNICODE: Symbols The symbols for a UTF-8 terminal. .. rust:associatedconst:: pub const ASCII: Symbols The symbols every terminal shows. .. rubric:: Trait Implementations .. rust:impl:: impl Default for Symbols :noindex: :for: Symbols :trait: Default .. rust:method:: fn default() -> Symbols :noindex: ASCII, which is always safe. .. rust:struct:: pub struct Style How :rust:fn:`render_text ` lays a report out. .. rubric:: Fields .. rust:field:: pub color: bool Whether to use ANSI colors and bold; words never depend on them. .. rust:field:: pub width: usize The width of the title rule and of wrapped prose, in terminal cells. .. rubric:: Trait Implementations .. rust:impl:: impl Default for Style :noindex: :for: Style :trait: Default .. rust:method:: fn default() -> Style :noindex: .. rust:struct:: pub struct Excerpt The source line a report points at, with the byte range to underline. .. rubric:: Fields .. rust:field:: pub text: String The complete source line, without its line ending. .. rust:field:: pub start: usize Byte offset in ``text`` where the underline starts (clamped to a char boundary). .. rust:field:: pub end: usize Byte offset in ``text`` where the underline ends; at least one caret is drawn. .. rust:field:: pub before: String The line before, shown above for context when it is not blank; may be empty. .. rust:struct:: pub struct Report A diagnostic ready to render: what happened, where, and what to do about it. Fill it with struct-update syntax, or start from :rust:fn:`Report::error `, :rust:fn:`Report::warning ` or :rust:fn:`Report::note ` and attach a position with :rust:fn:`Report::at `. .. rubric:: Fields .. rust:field:: pub code: String A stable identifier such as ``"toml.duplicate-key"``; may be empty. .. rust:field:: pub severity: Severity How serious the report is. .. rust:field:: pub title: String A short upper-case title such as ``"DUPLICATE KEY"``. .. rust:field:: pub message: String The plain explanation; may span several lines. .. rust:field:: pub hints: Vec How to fix it; the first is shown after ``Hint:``, the rest on their own lines. .. rust:field:: pub path: String The file name as it should be shown; may be empty. .. rust:field:: pub line: usize 1-based line, or 0 when the report has no position. .. rust:field:: pub column: usize 1-based column counting Unicode scalars, or 0 when unknown. .. rust:field:: pub end_line: usize 1-based line where the reported span ends, or 0. .. rust:field:: pub end_column: usize 1-based column where the reported span ends (exclusive), or 0. .. rust:field:: pub excerpt: Option The source line to show with carets, if any. .. rust:field:: pub category: String A category as ``"type.subtype"`` that users may silence, such as ``"ref.term"``. .. rubric:: Implementations .. rust:impl:: impl Report :noindex: :for: Report .. rust:method:: 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. .. rust:method:: 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. .. rust:method:: pub fn note(code: &str, title: &str, message: &str) -> Report Starts a note with a code, title and message; it has no position yet. .. rust:method:: pub fn hint(mut self, hint: &str) -> Report Adds a hint and returns the report, for chaining. .. rust:method:: pub fn at(mut self, path: &str, source: &str, start: usize, end: usize) -> Report Positions the report at bytes ``start..end`` of ``source`` (clamped to the text and to char boundaries), naming the file ``path``; fills line, columns and excerpt. .. rubric:: Enums .. rust:enum:: pub enum Severity How serious a report is. .. rubric:: Variants .. rust:variant:: Error The input cannot be used as it is. .. rust:variant:: Warning The result was produced but probably not as intended. .. rust:variant:: Note Extra information, such as where something was first defined. .. rubric:: Implementations .. rust:impl:: impl Severity :noindex: :for: Severity .. rust:method:: pub fn name(self) -> &'static str The lower-case name used in JSON: ``"error"``, ``"warning"`` or ``"note"``. .. rubric:: Functions .. rust:function:: pub fn escape_json(out: &mut String, text: &str) Appends ``text`` to ``out`` as a JSON string literal with its quotes, escaping quotes, backslashes and control characters (and U+2028/U+2029 for embedding in scripts). .. rust:function:: pub fn render_json(report: &Report) -> String Writes ``report`` as one JSON object on one line (schema :rust:const:`SCHEMA `): ``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 ``""``) and ``category``. The terminal window and carets are left out; columns count Unicode scalars. Returns an allocated string without a line break; :rust:fn:`write_json ` appends to a reused buffer instead. .. rust:function:: pub fn write_json(out: &mut String, report: &Report) Appends the object :rust:fn:`render_json ` returns to ``out``. .. rust:function:: pub fn symbols_for(env: &SymbolEnv) -> Symbols The symbols for the terminal ``env`` describes: Unicode only when the locale is UTF-8 (or the terminal is Windows Terminal), ``TERM`` is not ``dumb`` and ``NO_COLOR`` is unset. .. rust:function:: pub fn terminal_symbols() -> Symbols The symbols for this process's terminal; see :rust:fn:`symbols_for `. .. rust:function:: pub fn render_text(report: &Report, style: &Style) -> String Lays ``report`` out 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; :rust:fn:`write_text ` appends to a buffer the caller reuses instead. .. rust:function:: pub fn wrap(text: &str, width: usize) -> String Wraps ``text`` at spaces so lines take at most ``width`` cells 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); :rust:fn:`write_wrapped ` appends instead. .. rust:function:: pub fn write_text(out: &mut String, report: &Report, style: &Style) Appends the text :rust:fn:`render_text ` returns to ``out``, so a caller rendering many reports can ``clear()`` and reuse one buffer. .. rust:function:: pub fn write_wrapped(out: &mut String, text: &str, width: usize) Appends ``text`` wrapped as :rust:fn:`wrap ` wraps it to ``out``. .. rust:function:: pub fn char_width(c: char) -> usize The number of terminal cells ``c`` takes: 0 for controls and combining or zero-width characters, 2 for East Asian Wide and Fullwidth ones, 1 for the rest. .. rust:function:: pub fn display_width(text: &str) -> usize The number of terminal cells ``text`` takes, the sum of :rust:fn:`char_width ` over its characters. Grapheme clusters such as emoji sequences are not joined. .. rust:function:: pub fn escape_terminal(text: &str) -> Cow<'_, str> Returns ``text`` with every control character except the line break escaped as ``\uXXXX`` (tabs included), so file names and sources cannot send escape sequences to a terminal. Borrows ``text`` when it has nothing to escape and allocates otherwise. .. rust:function:: pub fn is_control(c: char) -> bool Reports whether ``c`` is a C0 or C1 control character or DEL. .. rust:function:: pub fn is_wide(c: char) -> bool Reports whether ``c`` is East Asian Wide or Fullwidth, taking two terminal cells. .. rust:function:: pub fn is_zero_width(c: char) -> bool Reports whether ``c`` is a combining mark or another character drawn in no cell. .. rust:function:: pub fn set_span(report: &mut Report, source: &str, start: usize, end: usize) Fills ``report``'s line, columns and excerpt from bytes ``start..end`` of ``source``. 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. .. rust:function:: pub fn set_line_column(report: &mut Report, source: &str, line: usize, column: usize) Fills ``report``'s position and excerpt from a 1-based ``line`` and scalar ``column`` of ``source``, for positions that tools report without byte offsets. Does nothing when the line does not exist. .. rust:function:: pub fn line_offset(text: &str, line: usize) -> Option The byte offset where 1-based line ``line`` of ``text`` starts, or ``None`` past the end. .. rust:function:: pub fn line_column(text: &str, offset: usize) -> (usize, usize) The 1-based line and column of byte ``offset`` in ``text``; the column counts Unicode scalars. Offsets past the end or inside a character are clamped down. .. rust:function:: pub fn line_bounds(text: &str, offset: usize) -> (usize, usize) The byte range of the line holding ``offset``, without its ``\n`` or ``\r\n`` ending. .. rust:function:: pub fn floor_boundary(text: &str, offset: usize) -> usize Clamps ``offset`` to ``text`` and moves it back to the start of the character it is in. .. rust:function:: pub fn edit_distance(a: &str, b: &str) -> usize The Levenshtein distance between ``a`` and ``b``, counting Unicode scalars. .. rust:function:: pub fn suggest<'a, I>(name: &str, candidates: I) -> Option<&'a str> where I: IntoIterator The candidate closest to ``name`` when 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 from ``candidates``; returns ``None`` when nothing is close or ``name`` itself is listed. .. rubric:: Constants .. rust:const:: pub const SCHEMA: u32 = 1 The version of the JSON form :rust:fn:`render_json ` writes; it changes only when a field does.