pub struct Diagnostic {
pub code: &'static str,
pub severity: Severity,
pub span: Span,
pub message: Cow<'static, str>,
pub labels: Vec<Label>,
}Expand description
Something a parser has to say about its input: a stable code to
match on (like unterminated-string), where, and a sentence for
people. With a 'static message and no labels, making one allocates
nothing.
Fields§
§code: &'static str§severity: Severity§span: Span§message: Cow<'static, str>§labels: Vec<Label>Implementations§
Source§impl Diagnostic
impl Diagnostic
Sourcepub fn render(
&self,
src: &str,
lines: &LineIndex<'_>,
options: &RenderOptions<'_>,
) -> String
pub fn render( &self, src: &str, lines: &LineIndex<'_>, options: &RenderOptions<'_>, ) -> String
The diagnostic as a code frame: a header with its severity, code and message, where it is, and each line it or a label points at, carets under its own span and dashes under each label’s.
lines is an index of src. Columns are counted as a terminal
shows them: tabs expanded, and East Asian wide characters two
columns.
use parsec_base::{Diagnostic, LineIndex, RenderOptions, Span};
let src = "if (ready {\n go();\n}\n";
let d = Diagnostic::error("unexpected-token", Span::new(10, 11), "`{` can't go here")
.with_label(Span::new(3, 4), "this `(` is still open");
let frame = d.render(src, &LineIndex::new(src), &RenderOptions::default());
assert_eq!(frame, "\
error[unexpected-token]: `{` can't go here
--> 1:11
|
1 | if (ready {
| - ^
| |
| this `(` is still open
");Source§impl Diagnostic
impl Diagnostic
pub fn new( code: &'static str, severity: Severity, span: Span, message: impl Into<Cow<'static, str>>, ) -> Diagnostic
pub fn error( code: &'static str, span: Span, message: impl Into<Cow<'static, str>>, ) -> Diagnostic
pub fn warning( code: &'static str, span: Span, message: impl Into<Cow<'static, str>>, ) -> Diagnostic
Sourcepub fn with_label(
self,
span: Span,
message: impl Into<Cow<'static, str>>,
) -> Diagnostic
pub fn with_label( self, span: Span, message: impl Into<Cow<'static, str>>, ) -> Diagnostic
The same, pointing at span too.
pub fn is_error(&self) -> bool
Trait Implementations§
Source§impl Clone for Diagnostic
impl Clone for Diagnostic
Source§fn clone(&self) -> Diagnostic
fn clone(&self) -> Diagnostic
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · 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 Diagnostic
impl Debug for Diagnostic
Source§impl Display for Diagnostic
impl Display for Diagnostic
impl Eq for Diagnostic
Source§impl Error for Diagnostic
impl Error for Diagnostic
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
👎Deprecated since 1.42.0:
use the Display impl or to_string()
Source§impl Hash for Diagnostic
impl Hash for Diagnostic
Source§impl PartialEq for Diagnostic
impl PartialEq for Diagnostic
impl StructuralPartialEq for Diagnostic
Auto Trait Implementations§
impl Forget for Diagnostic
impl Freeze for Diagnostic
impl Move for Diagnostic
impl RefUnwindSafe for Diagnostic
impl Send for Diagnostic
impl Sync for Diagnostic
impl Unpin for Diagnostic
impl UnsafeUnpin for Diagnostic
impl UnwindSafe for Diagnostic
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