pub struct ParseDiagnostic {
pub message: MessageAndDescription,
/* 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.
Fields§
§message: MessageAndDescriptionImplementations§
Source§impl ParseDiagnostic
impl ParseDiagnostic
pub fn new(message: impl Display, span: impl AsSpan) -> ParseDiagnostic
pub fn new_single_node( name: &str, range: TextRange, p: &impl Parser, ) -> ParseDiagnostic
pub fn new_with_any( names: &[&str], range: TextRange, p: &impl Parser, ) -> ParseDiagnostic
pub const fn is_error(&self) -> bool
Sourcepub fn with_detail(
self,
range: impl AsSpan,
message: impl Display,
) -> ParseDiagnostic
pub fn with_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)
.with_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 with_hint(self, message: impl Display) -> ParseDiagnostic
pub fn with_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)
.with_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"));Sourcepub fn with_alternatives(
self,
message: impl Display,
alternatives: &[impl Display],
) -> ParseDiagnostic
pub fn with_alternatives( self, message: impl Display, alternatives: &[impl Display], ) -> ParseDiagnostic
A message that also allows to list of alternatives in case a fixed range of values/characters are expected.
§Examples
let source = "const a";
let range = TextRange::new(TextSize::from(0), TextSize::from(5));
let mut diagnostic = ParseDiagnostic::new("this is wrong!", range)
.with_alternatives("Expected one of the following values:", &["foo", "bar"]);
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 Expected one of the following values:"));
assert!(result.contains("- foo"));
assert!(result.contains("- bar"));Trait Implementations§
Source§impl Clone for ParseDiagnostic
impl Clone for ParseDiagnostic
Source§fn clone(&self) -> ParseDiagnostic
fn clone(&self) -> ParseDiagnostic
Returns a duplicate 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 Freeze for ParseDiagnostic
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<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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