mirror of https://github.com/zkat/miette.git
Add skeleton
This commit is contained in:
parent
09d7d15098
commit
e6d2eede61
|
|
@ -0,0 +1,72 @@
|
|||
use std::{
|
||||
error::Error,
|
||||
fmt::{Debug, Display},
|
||||
};
|
||||
|
||||
use crate::Diagnostic;
|
||||
|
||||
/// Diagnostic that can be created at runtime.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct DynamicDiagnostic {
|
||||
/// Displayed diagnostic description
|
||||
pub description: String,
|
||||
/// Unique diagnostic code to look up more information
|
||||
/// about this Diagnostic. Ideally also globally unique, and documented
|
||||
/// in the toplevel crate's documentation for easy searching.
|
||||
/// Rust path format (`foo::bar::baz`) is recommended, but more classic
|
||||
/// codes like `E0123` will work just fine.
|
||||
pub code: Option<String>,
|
||||
}
|
||||
|
||||
impl Display for DynamicDiagnostic {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", &self.description)
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for DynamicDiagnostic {}
|
||||
|
||||
impl Diagnostic for DynamicDiagnostic {
|
||||
fn code<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
|
||||
self.code
|
||||
.as_ref()
|
||||
.map(Box::new)
|
||||
.map(|c| c as Box<dyn Display>)
|
||||
}
|
||||
}
|
||||
|
||||
impl DynamicDiagnostic {
|
||||
/// Create a new dynamic diagnostic with the given description.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use miette::{Diagnostic, DynamicDiagnostic};
|
||||
///
|
||||
/// let diag = DynamicDiagnostic::new("Oops, something went wrong!");
|
||||
/// assert_eq!(diag.to_string(), "Oops, something went wrong!");
|
||||
/// assert_eq!(diag.description, "Oops, something went wrong!");
|
||||
/// ```
|
||||
pub fn new(description: impl Into<String>) -> Self {
|
||||
Self {
|
||||
description: description.into(),
|
||||
code: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Return new diagnostic with the given code.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use miette::{Diagnostic, DynamicDiagnostic};
|
||||
///
|
||||
/// let diag = DynamicDiagnostic::new("Oops, something went wrong!").with_code("foo::bar::baz");
|
||||
/// assert_eq!(diag.description, "Oops, something went wrong!");
|
||||
/// assert_eq!(diag.code, Some("foo::bar::baz".to_string()));
|
||||
/// ```
|
||||
pub fn with_code(self, code: impl Into<String>) -> Self {
|
||||
Self {
|
||||
code: Some(code.into()),
|
||||
..self
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -249,7 +249,7 @@
|
|||
//! To construct your own simple adhoc error use the [miette!] macro:
|
||||
//! ```rust
|
||||
//! // my_app/lib/my_internal_file.rs
|
||||
//! use miette::{IntoDiagnostic, Result, WrapErr, miette};
|
||||
//! use miette::{miette, IntoDiagnostic, Result, WrapErr};
|
||||
//! use semver::Version;
|
||||
//!
|
||||
//! pub fn some_tool() -> Result<Version> {
|
||||
|
|
@ -619,6 +619,7 @@
|
|||
//! [`ariadne`](https://github.com/zesterer/ariadne), which is MIT licensed.
|
||||
pub use miette_derive::*;
|
||||
|
||||
pub use dynamic_diagnostic::*;
|
||||
pub use error::*;
|
||||
pub use eyreish::*;
|
||||
#[cfg(feature = "fancy-no-backtrace")]
|
||||
|
|
@ -631,6 +632,7 @@ pub use protocol::*;
|
|||
|
||||
mod chain;
|
||||
mod diagnostic_chain;
|
||||
mod dynamic_diagnostic;
|
||||
mod error;
|
||||
mod eyreish;
|
||||
#[cfg(feature = "fancy-no-backtrace")]
|
||||
|
|
|
|||
Loading…
Reference in New Issue