Struct biome_json_parser::ParseDiagnostic
source · 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:
- a mandatory message and a mandatory [TextRange]
- a list of details, useful to give more information and context around the error
- 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
impl ParseDiagnostic
pub fn new(message: impl Display, span: impl AsSpan) -> ParseDiagnostic
pub const fn is_error(&self) -> bool
sourcepub fn detail(
self,
range: impl AsSpan,
message: impl Display
) -> ParseDiagnostic
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("");sourcepub fn hint(self, message: impl Display) -> ParseDiagnostic
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
impl Clone for ParseDiagnostic
source§fn clone(&self) -> ParseDiagnostic
fn clone(&self) -> ParseDiagnostic
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moresource§impl Debug for ParseDiagnostic
impl Debug for ParseDiagnostic
source§impl Diagnostic for ParseDiagnostic
impl Diagnostic for ParseDiagnostic
source§fn category(&self) -> Option<&'static Category>
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
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>
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>
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>
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<'_>
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>
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.
Tags convey additional boolean metadata about the nature of a diagnostic: Read more
source§fn source(&self) -> Option<&dyn Diagnostic>
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 ParseDiagnosticwhere
P: Parser,
impl<P> ToDiagnostic<P> for ParseDiagnosticwhere
P: Parser,
fn into_diagnostic(self, _: &P) -> ParseDiagnostic
Auto Trait Implementations§
impl RefUnwindSafe for ParseDiagnostic
impl Send for ParseDiagnostic
impl Sync for ParseDiagnostic
impl Unpin for ParseDiagnostic
impl UnwindSafe for ParseDiagnostic
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<E> DiagnosticExt for Ewhere
E: AsDiagnostic,
impl<E> DiagnosticExt for Ewhere
E: AsDiagnostic,
source§fn context<M>(self, message: M) -> Error
fn context<M>(self, message: M) -> Error
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
fn with_category(self, category: &'static Category) -> Error
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
fn with_file_path(self, path: impl AsResource) -> Error
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
fn with_file_span(self, span: impl AsSpan) -> Error
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
fn with_file_source_code(self, source_code: impl AsSourceCode) -> Error
Returns a new diagnostic using the provided
source_code if self
doesn’t already have one.Returns a new diagnostic with additional
tags