pub struct SimpleDiagnostic {
pub message: String,
pub code: Option<String>,
pub severity: Severity,
pub help: Vec<Help>,
pub labels: Option<Vec<Label>>,
pub causes: Vec<Box<dyn Diagnostic + Send + Sync>>,
pub related: Vec<Box<dyn Diagnostic + Send + Sync>>,
}
Expand description
Diagnostic which can be created at runtime.
Fields§
§message: String
Defines the message being displayed along with the diagnostic.
code: Option<String>
Unique code for the diagnostic, which can be used to look up more information about the diagnostic.
severity: Severity
Defines the severity of the diagnostic. Defaults to Severity::Error
.
help: Vec<Help>
Defines a list of help messages which can help or guide the user about the diagnostic.
labels: Option<Vec<Label>>
Defines a list of labels which can provide additional context about the diagnostic.
causes: Vec<Box<dyn Diagnostic + Send + Sync>>
Defines the underlying cause for the diagnostic to be raised.
Defines the diagnostics which are related to the current one, if any.
Implementations§
Source§impl SimpleDiagnostic
impl SimpleDiagnostic
Sourcepub fn new(message: impl Into<String>) -> Self
pub fn new(message: impl Into<String>) -> Self
Creates a new SimpleDiagnostic
with the given message content.
§Examples
use error_snippet::SimpleDiagnostic;
let diag = SimpleDiagnostic::new("Whoops, that wasn't supposed to happen!");
assert_eq!(diag.to_string(), "Whoops, that wasn't supposed to happen!");
assert_eq!(diag.message, "Whoops, that wasn't supposed to happen!");
Sourcepub fn with_severity(self, severity: impl Into<Severity>) -> Self
pub fn with_severity(self, severity: impl Into<Severity>) -> Self
Sets the severity for the current diagnostic instance.
§Examples
use error_snippet::{Severity, SimpleDiagnostic};
let diag = SimpleDiagnostic::new("Hmm, this could certainly be done better.")
.with_severity(Severity::Warning);
assert_eq!(diag.message, "Hmm, this could certainly be done better.");
assert_eq!(diag.severity, Severity::Warning);
Sourcepub fn with_code(self, code: impl Into<String>) -> Self
pub fn with_code(self, code: impl Into<String>) -> Self
Sets the diagnostic code for the current instance.
§Examples
use error_snippet::SimpleDiagnostic;
let diag = SimpleDiagnostic::new("Whoops, that wasn't supposed to happen!")
.with_code("E1010");
assert_eq!(diag.message, "Whoops, that wasn't supposed to happen!");
assert_eq!(diag.code, Some(String::from("E1010")));
Sourcepub fn with_help(self, help: impl Into<Help>) -> Self
pub fn with_help(self, help: impl Into<Help>) -> Self
Adds a new help message to the current instance.
§Examples
use error_snippet::{Help, SimpleDiagnostic};
let diag = SimpleDiagnostic::new("Whoops, that wasn't supposed to happen!")
.with_help("have you tried restarting?");
assert_eq!(diag.message, "Whoops, that wasn't supposed to happen!");
assert_eq!(diag.help, vec![Help::new("have you tried restarting?")]);
Sourcepub fn set_help(self, help: impl Into<Help>) -> Self
pub fn set_help(self, help: impl Into<Help>) -> Self
Sets the help message of the current instance.
§Examples
use error_snippet::{Help, SimpleDiagnostic};
let diag = SimpleDiagnostic::new("Whoops, that wasn't supposed to happen!")
.set_help("have you tried restarting?");
assert_eq!(diag.message, "Whoops, that wasn't supposed to happen!");
assert_eq!(diag.help, vec![Help::new("have you tried restarting?")]);
Sourcepub fn with_label(self, label: impl Into<Label>) -> Self
pub fn with_label(self, label: impl Into<Label>) -> Self
Adds a new label to the current instance.
§Examples
use std::sync::Arc;
use error_snippet::{SimpleDiagnostic, Label, NamedSource};
let source = Arc::new(NamedSource::new(
"src/lib.rs",
r#"fn main() -> int {
let a = new Testing();
let b = a.invok();
return false;
}"#,
));
let label1 = Label::new(Some(source.clone()), 60..65, "could not find method 'invok'");
let label2 = Label::new(Some(source.clone()), 81..86, "expected 'int', found 'boolean'");
let diag = SimpleDiagnostic::new("Whoops, that wasn't supposed to happen!")
.with_label(label1.clone())
.with_label(label2.clone());
assert_eq!(diag.message, "Whoops, that wasn't supposed to happen!");
assert_eq!(diag.labels, Some(vec![label1, label2]));
Sourcepub fn with_labels(
self,
labels: impl IntoIterator<Item = impl Into<Label>>,
) -> Self
pub fn with_labels( self, labels: impl IntoIterator<Item = impl Into<Label>>, ) -> Self
Adds a list of labels to the current instance. The given labels are appended onto the existing label array in the diagnostic, so nothing is overwritten.
§Examples
use std::sync::Arc;
use error_snippet::{SimpleDiagnostic, Label, NamedSource};
let source = Arc::new(NamedSource::new(
"src/lib.rs",
r#"fn main() -> int {
let a = new Testing();
let b = a.invok();
return false;
}"#,
));
let label1 = Label::new(Some(source.clone()), 60..65, "could not find method 'invok'");
let label2 = Label::new(Some(source.clone()), 81..86, "expected 'int', found 'boolean'");
let diag = SimpleDiagnostic::new("Whoops, that wasn't supposed to happen!")
.with_labels([label1.clone(), label2.clone()]);
assert_eq!(diag.message, "Whoops, that wasn't supposed to happen!");
assert_eq!(diag.labels, Some(vec![label1, label2]));
Adds a related diagnostic to the current instance.
§Examples
use error_snippet::SimpleDiagnostic;
let related1 = std::io::Error::new(std::io::ErrorKind::Other, "failed to read file");
let related2 = std::io::Error::new(std::io::ErrorKind::Other, "file is unaccessible");
let diag = SimpleDiagnostic::new("failed to perform I/O operation")
.add_related(related1)
.add_related(related2);
assert_eq!(diag.message, "failed to perform I/O operation");
assert_eq!(diag.related.iter().map(|e| e.to_string()).collect::<Vec<_>>(), vec![
"failed to read file".to_string(),
"file is unaccessible".to_string()
]);
Adds multiple related diagnostics to the current instance.
§Examples
use error_snippet::SimpleDiagnostic;
let related1 = std::io::Error::new(std::io::ErrorKind::Other, "failed to read file");
let related2 = std::io::Error::new(std::io::ErrorKind::Other, "file is unaccessible");
let diag = SimpleDiagnostic::new("failed to perform I/O operation")
.append_related([related1, related2]);
assert_eq!(diag.message, "failed to perform I/O operation");
assert_eq!(diag.related.iter().map(|e| e.to_string()).collect::<Vec<_>>(), vec![
"failed to read file".to_string(),
"file is unaccessible".to_string()
]);
Sourcepub fn add_cause(
self,
cause: impl Into<Box<dyn Diagnostic + Send + Sync>>,
) -> Self
pub fn add_cause( self, cause: impl Into<Box<dyn Diagnostic + Send + Sync>>, ) -> Self
Adds a causing error diagnostic to the current instance.
§Examples
use error_snippet::SimpleDiagnostic;
let cause1 = std::io::Error::new(std::io::ErrorKind::Other, "failed to read file");
let cause2 = std::io::Error::new(std::io::ErrorKind::Other, "file is unaccessible");
let diag = SimpleDiagnostic::new("failed to perform I/O operation")
.add_cause(cause1)
.add_cause(cause2);
assert_eq!(diag.message, "failed to perform I/O operation");
assert_eq!(diag.causes.iter().map(|e| e.to_string()).collect::<Vec<_>>(), vec![
"failed to read file".to_string(),
"file is unaccessible".to_string()
]);
Sourcepub fn add_causes(
self,
causes: impl IntoIterator<Item = impl Into<Box<dyn Diagnostic + Send + Sync>>>,
) -> Self
pub fn add_causes( self, causes: impl IntoIterator<Item = impl Into<Box<dyn Diagnostic + Send + Sync>>>, ) -> Self
Adds multiple causing error diagnostics to the current instance.
§Examples
use error_snippet::SimpleDiagnostic;
let cause1 = std::io::Error::new(std::io::ErrorKind::Other, "failed to read file");
let cause2 = std::io::Error::new(std::io::ErrorKind::Other, "file is unaccessible");
let diag = SimpleDiagnostic::new("failed to perform I/O operation")
.add_causes([cause1, cause2]);
assert_eq!(diag.message, "failed to perform I/O operation");
assert_eq!(diag.causes.iter().map(|e| e.to_string()).collect::<Vec<_>>(), vec![
"failed to read file".to_string(),
"file is unaccessible".to_string()
]);
Trait Implementations§
Source§impl Debug for SimpleDiagnostic
impl Debug for SimpleDiagnostic
Source§impl Default for SimpleDiagnostic
impl Default for SimpleDiagnostic
Source§fn default() -> SimpleDiagnostic
fn default() -> SimpleDiagnostic
Source§impl Diagnostic for SimpleDiagnostic
impl Diagnostic for SimpleDiagnostic
Source§fn code(&self) -> Option<Box<dyn Display + '_>>
fn code(&self) -> Option<Box<dyn Display + '_>>
Source§fn help(&self) -> Option<Box<dyn Iterator<Item = Help> + '_>>
fn help(&self) -> Option<Box<dyn Iterator<Item = Help> + '_>>
Source§fn labels(&self) -> Option<Box<dyn Iterator<Item = Label> + '_>>
fn labels(&self) -> Option<Box<dyn Iterator<Item = Label> + '_>>
Auto Trait Implementations§
impl Freeze for SimpleDiagnostic
impl !RefUnwindSafe for SimpleDiagnostic
impl Send for SimpleDiagnostic
impl Sync for SimpleDiagnostic
impl Unpin for SimpleDiagnostic
impl !UnwindSafe for SimpleDiagnostic
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
Source§impl<D> OwoColorize for D
impl<D> OwoColorize for D
Source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
Source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
Source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
Source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
Source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
Source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
Source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
Source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
Source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
Source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
Source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
Source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
Source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
Source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
Source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
Source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
Source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
Source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
Source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
Source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
Source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
Source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
Source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
Source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
Source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
Source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
Source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
Source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
Source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
Source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
Source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
Source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
Source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
Source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
Source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
Source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
Source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
Source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
Source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
Source§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg
or
a color-specific method, such as OwoColorize::green
, Read moreSource§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg
or
a color-specific method, such as OwoColorize::on_yellow
, Read more