pub struct Diagnostic { /* private fields */ }Expand description
A user-facing diagnostic (manifest §16, §31).
Identity and default severity come from a 'static DiagnosticDef in
codes; the instance carries the resolved severity (today always the
def’s default, later a config override) so rendering never has to consult
the def. Fields are private — construct via Diagnostic::simple or
Diagnostic::new.
§Examples
use mos_core::{Diagnostic, Severity, codes};
let diagnostic = Diagnostic::simple(&codes::MOS0010, None, "boom");
assert_eq!(diagnostic.severity(), Severity::Error);
assert_eq!(diagnostic.def().code().to_string(), "MOS0010");Implementations§
Source§impl Diagnostic
impl Diagnostic
Sourcepub fn new(
def: &'static DiagnosticDef,
severity: Severity,
span: Option<SourceSpan>,
message: impl Into<String>,
) -> Self
pub fn new( def: &'static DiagnosticDef, severity: Severity, span: Option<SourceSpan>, message: impl Into<String>, ) -> Self
Full constructor: the caller supplies the resolved severity. The future config resolver uses this; nothing has to crack open the struct.
§Examples
use mos_core::{Diagnostic, Severity, codes};
// Promote a warning-by-default code to an error.
let d = Diagnostic::new(&codes::MOS0028, Severity::Error, None, "promoted");
assert_eq!(d.severity(), Severity::Error);Sourcepub fn simple(
def: &'static DiagnosticDef,
span: Option<SourceSpan>,
message: impl Into<String>,
) -> Self
pub fn simple( def: &'static DiagnosticDef, span: Option<SourceSpan>, message: impl Into<String>, ) -> Self
Convenience: severity defaults to def.default_severity().
§Examples
use mos_core::{Diagnostic, Severity, codes};
let d = Diagnostic::simple(&codes::MOS0018, None, "substituted Noto Sans");
assert_eq!(d.severity(), Severity::Notice);Sourcepub fn with_annotation(self, annotation: DiagnosticAnnotation) -> Self
pub fn with_annotation(self, annotation: DiagnosticAnnotation) -> Self
Attach a sub-message annotation, builder-style.
§Examples
use mos_core::{Diagnostic, DiagnosticAnnotation, codes};
let d = Diagnostic::simple(&codes::MOS0033, None, "unknown label")
.with_annotation(DiagnosticAnnotation::Help("did you mean `@intro`?".to_owned()));
assert_eq!(d.annotations().len(), 1);Sourcepub fn with_suggestion(self, suggestion: Suggestion) -> Self
pub fn with_suggestion(self, suggestion: Suggestion) -> Self
Attach a machine-actionable Suggestion, builder-style.
§Examples
use std::path::PathBuf;
use mos_core::{Diagnostic, SourceSpan, Suggestion, codes};
let span = SourceSpan::new(PathBuf::from("main.mos"), 4, 10);
let d = Diagnostic::simple(&codes::MOS0033, None, "unknown label")
.with_suggestion(Suggestion::new(span, "@intro"));
assert_eq!(d.suggestions().len(), 1);Sourcepub fn with_span(self, span: SourceSpan) -> Self
pub fn with_span(self, span: SourceSpan) -> Self
Attach a span, builder-style.
§Examples
use std::path::PathBuf;
use mos_core::{Diagnostic, SourceSpan, codes};
let span = SourceSpan::new(PathBuf::from("main.mos"), 4, 10);
let diagnostic = Diagnostic::simple(&codes::MOS0033, None, "unknown label")
.with_span(span.clone());
assert_eq!(diagnostic.span(), Some(&span));Sourcepub fn def(&self) -> &'static DiagnosticDef
pub fn def(&self) -> &'static DiagnosticDef
The registry definition behind this diagnostic.
§Examples
use mos_core::{Diagnostic, codes};
let diagnostic = Diagnostic::simple(&codes::MOS0033, None, "unknown label");
assert_eq!(diagnostic.def().code(), codes::MOS0033.code());Sourcepub fn severity(&self) -> Severity
pub fn severity(&self) -> Severity
The resolved severity carried by this instance.
§Examples
use mos_core::{Diagnostic, Severity, codes};
let diagnostic = Diagnostic::simple(&codes::MOS0033, None, "unknown label");
assert_eq!(diagnostic.severity(), Severity::Error);Sourcepub fn span(&self) -> Option<&SourceSpan>
pub fn span(&self) -> Option<&SourceSpan>
The primary span, if any.
§Examples
use mos_core::{Diagnostic, codes};
let diagnostic = Diagnostic::simple(&codes::MOS0033, None, "unknown label");
assert!(diagnostic.span().is_none());Sourcepub fn message(&self) -> &str
pub fn message(&self) -> &str
The primary message.
§Examples
use mos_core::{Diagnostic, codes};
let diagnostic = Diagnostic::simple(&codes::MOS0033, None, "unknown label");
assert_eq!(diagnostic.message(), "unknown label");Sourcepub fn annotations(&self) -> &[DiagnosticAnnotation]
pub fn annotations(&self) -> &[DiagnosticAnnotation]
The attached sub-message annotations, in attach order.
§Examples
use mos_core::{Diagnostic, DiagnosticAnnotation, codes};
let diagnostic = Diagnostic::simple(&codes::MOS0033, None, "unknown label")
.with_annotation(DiagnosticAnnotation::Help("declare `<intro>` first".to_owned()));
assert_eq!(diagnostic.annotations().len(), 1);Sourcepub fn suggestions(&self) -> &[Suggestion]
pub fn suggestions(&self) -> &[Suggestion]
The attached machine-actionable suggestions, in attach order.
§Examples
use std::path::PathBuf;
use mos_core::{Diagnostic, SourceSpan, Suggestion, codes};
let span = SourceSpan::new(PathBuf::from("main.mos"), 4, 10);
let diagnostic = Diagnostic::simple(&codes::MOS0033, None, "unknown label")
.with_suggestion(Suggestion::new(span, "@intro"));
assert_eq!(diagnostic.suggestions().len(), 1);Trait Implementations§
Source§impl Clone for Diagnostic
impl Clone for Diagnostic
Source§fn clone(&self) -> Diagnostic
fn clone(&self) -> Diagnostic
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Diagnostic
impl Debug for Diagnostic
Source§impl Display for Diagnostic
impl Display 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)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()