pub struct ParseDiagnostic { /* private fields */ }
Expand description

A specialized diagnostic for the parser

Parser diagnostics are always errors.

A parser diagnostics structured in this way:

  1. a mandatory message and a mandatory [TextRange]
  2. a list of details, useful to give more information and context around the error
  3. a hint, which should tell the user how they could fix their issue

These information are printed in this exact order.

Implementations§

source§

impl ParseDiagnostic

source

pub fn new(message: impl Display, span: impl AsSpan) -> ParseDiagnostic

source

pub const fn is_error(&self) -> bool

source

pub fn detail( self, range: impl AsSpan, message: impl Display ) -> ParseDiagnostic

Use this API if you want to highlight more code frame, to help to explain where’s the error.

A detail is printed after the actual error and before the hint.

Examples

let source = "const a";
let range = TextRange::new(TextSize::from(0), TextSize::from(5));
let mut diagnostic = ParseDiagnostic::new("this is wrong!", range)
    .detail(TextRange::new(TextSize::from(6), TextSize::from(7)), "This is reason why it's broken");

let mut write = biome_diagnostics::termcolor::Buffer::no_color();
let error = diagnostic
    .clone()
    .with_file_path("example.js")
    .with_file_source_code(source.to_string());
Formatter::new(&mut Termcolor(&mut write))
    .write_markup(markup! {
    {PrintDiagnostic::verbose(&error)}
})
    .expect("failed to emit diagnostic");

let mut result = String::new();
write!(
    result,
    "{}",
    std::str::from_utf8(write.as_slice()).expect("non utf8 in error buffer")
).expect("");
source

pub fn hint(self, message: impl Display) -> ParseDiagnostic

Small message that should suggest the user how they could fix the error

Hints are rendered a last part of the diagnostics

Examples

let source = "const a";
let range = TextRange::new(TextSize::from(0), TextSize::from(5));
let mut diagnostic = ParseDiagnostic::new("this is wrong!", range)
    .hint("You should delete the code");

let mut write = biome_diagnostics::termcolor::Buffer::no_color();
let error = diagnostic
    .clone()
    .with_file_path("example.js")
    .with_file_source_code(source.to_string());
Formatter::new(&mut Termcolor(&mut write))
    .write_markup(markup! {
    {PrintDiagnostic::verbose(&error)}
})
    .expect("failed to emit diagnostic");

let mut result = String::new();
write!(
    result,
    "{}",
    std::str::from_utf8(write.as_slice()).expect("non utf8 in error buffer")
).expect("");

assert!(result.contains("× this is wrong!"));
assert!(result.contains("i You should delete the code"));
assert!(result.contains("> 1 │ const a"));

Trait Implementations§

source§

impl Clone for ParseDiagnostic

source§

fn clone(&self) -> ParseDiagnostic

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for ParseDiagnostic

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl Diagnostic for ParseDiagnostic

source§

fn category(&self) -> Option<&'static Category>

The category of a diagnostic uniquely identifying this diagnostic type, such as lint/correctness/noArguments, args/invalid or format/disabled.
source§

fn severity(&self) -> Severity

The severity defines whether this diagnostic reports an error, a warning, an information or a hint to the user.
source§

fn description(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>

The description is a text-only explanation of the issue this diagnostic is reporting, intended for display contexts that do not support rich markup such as in-editor popovers Read more
source§

fn message(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>

An explanation of the issue this diagnostic is reporting Read more
source§

fn advices(&self, visitor: &mut dyn Visit) -> Result<(), Error>

Advices are the main building blocks used compose rich errors. They are implemented using a visitor pattern, where consumers of a diagnostic can visit the object and collect the advices that make it up for the purpose of display or introspection.
source§

fn location(&self) -> Location<'_>

A diagnostic can be tied to a specific “location”: this can be a file, memory buffer, command line argument, etc. It may also be tied to a specific text range within the content of that location. Finally, it may also provide the source string for that location (this is required in order to display a code frame advice for the diagnostic).
source§

fn verbose_advices(&self, visitor: &mut dyn Visit) -> Result<(), Error>

Diagnostics can defines additional advices to be printed if the user requires more detail about the diagnostic.
source§

fn tags(&self) -> DiagnosticTags

Tags convey additional boolean metadata about the nature of a diagnostic: Read more
source§

fn source(&self) -> Option<&dyn Diagnostic>

Similarly to the source method of the std::error::Error trait, this returns another diagnostic that’s the logical “cause” for this issue. For instance, a “request failed” diagnostic may have been cause by a “deserialization error”. This allows low-level error to be wrapped in higher level concepts, while retaining enough information to display and fix the underlying issue.
source§

impl<P> ToDiagnostic<P> for ParseDiagnostic
where P: Parser,

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<E> DiagnosticExt for E
where E: AsDiagnostic,

source§

fn context<M>(self, message: M) -> Error
where E: 'static, M: Display + 'static, Error: From<ContextDiagnostic<M, E>>,

Returns a new diagnostic with the provided message as a message and description, and self as a source diagnostic. This is useful to create chains of diagnostics, where high level errors wrap lower level causes.
source§

fn with_category(self, category: &'static Category) -> Error
where Error: From<CategoryDiagnostic<E>>,

Returns a new diagnostic using the provided category if self doesn’t already have one.
source§

fn with_file_path(self, path: impl AsResource) -> Error
where Error: From<FilePathDiagnostic<E>>,

Returns a new diagnostic using the provided path if self doesn’t already have one.
source§

fn with_file_span(self, span: impl AsSpan) -> Error
where Error: From<FileSpanDiagnostic<E>>,

Returns a new diagnostic using the provided span instead of the one in self.
source§

fn with_file_source_code(self, source_code: impl AsSourceCode) -> Error
where Error: From<FileSourceCodeDiagnostic<E>>,

Returns a new diagnostic using the provided source_code if self doesn’t already have one.
source§

fn with_tags(self, tags: DiagnosticTags) -> Error
where Error: From<TagsDiagnostic<E>>,

Returns a new diagnostic with additional tags
source§

fn with_severity(self, severity: Severity) -> Error
where Error: From<SeverityDiagnostic<E>>,

Returns a new diagnostic with additional severity
source§

impl<T> DynClone for T
where T: Clone,

source§

fn __clone_box(&self, _: Private) -> *mut ()

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more