Add skeleton

This commit is contained in:
Gavrilikhin Daniil 2023-05-05 22:46:50 +08:00
parent 09d7d15098
commit e6d2eede61
2 changed files with 75 additions and 1 deletions

72
src/dynamic_diagnostic.rs Normal file
View File

@ -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
}
}
}

View File

@ -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")]