Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dynamic diagnostic #262

Merged
merged 30 commits into from
May 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
09d7d15
Remove dot
gavrilikhin-d May 5, 2023
e6d2eed
Add skeleton
gavrilikhin-d May 5, 2023
ab20069
DynamicDiagnostic -> MietteDiagnostic
gavrilikhin-d May 6, 2023
01b34a4
Make `Severity::Error` to be default severity
gavrilikhin-d May 6, 2023
61d43f8
Add severity field
gavrilikhin-d May 6, 2023
53b21ea
Add help field
gavrilikhin-d May 6, 2023
a1f602c
Add url field
gavrilikhin-d May 6, 2023
c6e977b
Add labels field
gavrilikhin-d May 6, 2023
a3ee52a
Add convenience function to `LabeledSpan`
gavrilikhin-d May 6, 2023
8aaba99
Use convenience functions in examples
gavrilikhin-d May 6, 2023
021eb01
Adjust `miette!` a little bit
gavrilikhin-d May 6, 2023
b9a892f
Use `Option<Severity>`
gavrilikhin-d May 6, 2023
c2d793e
labels: `Option<Vec<_>>`
gavrilikhin-d May 6, 2023
bcf18f8
Fully implement support for `MietteDiagnostic`-like arguments in `mie…
gavrilikhin-d May 6, 2023
287ffc5
Add `miette_diagnostic!`
gavrilikhin-d May 6, 2023
4f0bc3e
Add `ensure!` support
gavrilikhin-d May 6, 2023
52e2dcb
Add `bail!` support
gavrilikhin-d May 6, 2023
7c4dd12
Add docs
gavrilikhin-d May 6, 2023
0e5512a
Add dot
gavrilikhin-d May 7, 2023
e35a63a
description -> message
gavrilikhin-d May 7, 2023
4148552
`miette_diagnostic!` -> `diagnostic!`
gavrilikhin-d May 7, 2023
b658720
Add `and_label(s)`
gavrilikhin-d May 7, 2023
cb5a1d3
Implement interpolation
gavrilikhin-d May 7, 2023
55f41a6
Remove literal case from ensure
gavrilikhin-d May 7, 2023
3e62212
Fix macro
gavrilikhin-d May 12, 2023
5e67953
Use `mut self` in builder functions
gavrilikhin-d May 12, 2023
5c94dd0
Sync README.md
gavrilikhin-d May 12, 2023
253a0f8
Update .tpl
gavrilikhin-d May 12, 2023
4520d66
Fix clippy
gavrilikhin-d May 13, 2023
7fb4cb2
Add and use `no-format-args-capture` flag
gavrilikhin-d May 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

## Introduction

Thank you so much for your interest in contributing!. All types of contributions are encouraged and valued. See the [table of contents](#toc) for different ways to help and details about how this project handles them!📝
Thank you so much for your interest in contributing! All types of contributions are encouraged and valued. See the [table of contents](#toc) for different ways to help and details about how this project handles them!📝

Please make sure to read the relevant section before making your contribution! It will make it a lot easier for us maintainers to make the most of it and smooth out the experience for all involved. 💚

Expand Down
72 changes: 72 additions & 0 deletions src/dynamic_diagnostic.rs
Original file line number Diff line number Diff line change
@@ -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 {
gavrilikhin-d marked this conversation as resolved.
Show resolved Hide resolved
/// 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>,
}
gavrilikhin-d marked this conversation as resolved.
Show resolved Hide resolved

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 {
gavrilikhin-d marked this conversation as resolved.
Show resolved Hide resolved
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
}
}
}
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
gavrilikhin-d marked this conversation as resolved.
Show resolved Hide resolved
//! use semver::Version;
//!
//! pub fn some_tool() -> Result<Version> {
Expand Down Expand Up @@ -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")]
Expand All @@ -631,6 +632,7 @@ pub use protocol::*;

mod chain;
mod diagnostic_chain;
mod dynamic_diagnostic;
mod error;
mod eyreish;
#[cfg(feature = "fancy-no-backtrace")]
Expand Down