Skip to main content

Diagnostic

Struct Diagnostic 

Source
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

Source

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);
Source

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);
Source

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);
Source

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);
Source

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));
Source

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());
Source

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);
Source

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());
Source

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");
Source

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);
Source

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

Source§

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)

Performs copy-assignment from source. Read more
Source§

impl Debug for Diagnostic

Source§

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

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

impl Display for Diagnostic

Source§

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

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

impl Error for Diagnostic

1.30.0 · Source§

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

👎Deprecated since 1.42.0:

use the Display impl or to_string()

1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more

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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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,

Source§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

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>,

Source§

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.