Guidedog マニュアル 1.0.0-beta.16
言語
このページの内容
Guidedog / ドキュメント 1.0.0-beta.16

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 None when it is unset.

Fields

pub lc_all: Option<String>

LC_ALL, which overrides the other locale variables.

pub lc_ctype: Option<String>

LC_CTYPE, the locale of the character set.

pub lang: Option<String>

LANG, the default locale.

pub term: Option<String>

TERM; dumb means a terminal without Unicode or styling.

pub no_color: bool

Whether NO_COLOR is set: a request for plain output.

pub windows_terminal: bool

Whether WT_SESSION is set: Windows Terminal, which is always UTF-8.

Implementations

impl SymbolEnv
pub fn from_process() -> SymbolEnv

Reads the variables from the process environment. Never fails: an unreadable (non-Unicode) value counts as unset.

pub struct gd_report::Symbols

The symbols a command decorates its terminal output with. Plain data: pick Symbols::UNICODE or Symbols::ASCII, or ask terminal_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
pub const UNICODE: Symbols

The symbols for a UTF-8 terminal.

pub const ASCII: Symbols

The symbols every terminal shows.

Trait Implementations

impl Default for Symbols
fn default() -> Symbols

ASCII, which is always safe.

pub struct gd_report::Style

How render_text lays 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

impl Default for Style
fn default() -> Style
pub struct gd_report::Excerpt

The source line a report points at, with the byte range to underline.

Fields

pub text: String

The complete source line, without its line ending.

pub start: usize

Byte offset in text where the underline starts (clamped to a char boundary).

pub end: usize

Byte offset in text where the underline ends; at least one caret is drawn.

pub before: String

The line before, shown above for context when it is not blank; may be empty.

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::warning or Report::note and attach a position with Report::at.

Fields

pub code: String

A stable identifier such as "toml.duplicate-key"; may be empty.

pub severity: Severity

How serious the report is.

pub title: String

A short upper-case title such as "DUPLICATE KEY".

pub message: String

The plain explanation; may span several lines.

pub hints: Vec<String>

How to fix it; the first is shown after Hint:, the rest on their own lines.

pub path: String

The file name as it should be shown; may be empty.

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.

pub excerpt: Option<Excerpt>

The source line to show with carets, if any.

pub category: String

A category as "type.subtype" that users may silence, such as "ref.term".

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.

pub fn note(code: &str, title: &str, message: &str) -> Report

Starts a note with a code, title and message; it has no position yet.

pub fn hint(mut self, hint: &str) -> Report

Adds a hint and returns the report, for chaining.

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.

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

impl Severity
pub fn name(self) -> &'static str

The lower-case name used in JSON: "error", "warning" or "note".

Functions

pub fn gd_report::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).

pub fn gd_report::render_json(report: &Report) -> String

Writes report as one JSON object on one line (schema 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; write_json appends to a reused buffer instead.

pub fn gd_report::write_json(out: &mut String, report: &Report)

Appends the object render_json returns to out.

pub fn gd_report::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.

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 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; write_text appends to a buffer the caller reuses instead.

pub fn gd_report::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); write_wrapped appends instead.

pub fn gd_report::write_text(out: &mut String, report: &Report, style: &Style)

Appends the text render_text returns to out, so a caller rendering many reports can clear() and reuse one buffer.

pub fn gd_report::write_wrapped(out: &mut String, text: &str, width: usize)

Appends text wrapped as wrap wraps it to out.

pub fn gd_report::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.

pub fn gd_report::display_width(text: &str) -> usize

The number of terminal cells text takes, the sum of char_width over its characters. Grapheme clusters such as emoji sequences are not joined.

pub fn gd_report::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.

pub fn gd_report::is_control(c: char) -> bool

Reports whether c is a C0 or C1 control character or DEL.

pub fn gd_report::is_wide(c: char) -> bool

Reports whether c is East Asian Wide or Fullwidth, taking two terminal cells.

pub fn gd_report::is_zero_width(c: char) -> bool

Reports whether c is 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 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.

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-based line and scalar column of source, 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 line of text starts, or None past the end.

pub fn gd_report::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.

pub fn gd_report::line_bounds(text: &str, offset: usize) -> (usize, usize)

The byte range of the line holding offset, without its \n or \r\n ending.

pub fn gd_report::floor_boundary(text: &str, offset: usize) -> usize

Clamps offset to text and 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 a and b, 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 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.

Constants

pub const gd_report::SCHEMA: u32 = 1

The version of the JSON form render_json writes; it changes only when a field does.