mirror of https://github.com/zkat/miette.git
fix(fancy): `fancy` no longer depends on `derive`
Before this change, the `fancy` feature required the `derive` feature to compile. Despite this, `derive` was not listed as a required feature to use `fancy`. This meant that building with: `cargo build --no-default-features --features fancy` would result in a compilation error! Now `fancy` can be used without `derive` enabled, and another use of `thiserror` has been removed!
This commit is contained in:
parent
3b1c87134a
commit
ffe374c1a1
58
src/panic.rs
58
src/panic.rs
|
|
@ -1,7 +1,8 @@
|
||||||
use backtrace::Backtrace;
|
use std::{error::Error, fmt::Display};
|
||||||
use thiserror::Error;
|
|
||||||
|
|
||||||
use crate::{self as miette, Context, Diagnostic, Result};
|
use backtrace::Backtrace;
|
||||||
|
|
||||||
|
use crate::{Context, Diagnostic, Result};
|
||||||
|
|
||||||
/// Tells miette to render panics using its rendering engine.
|
/// Tells miette to render panics using its rendering engine.
|
||||||
pub fn set_panic_hook() {
|
pub fn set_panic_hook() {
|
||||||
|
|
@ -25,11 +26,27 @@ pub fn set_panic_hook() {
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Error, Diagnostic)]
|
#[derive(Debug)]
|
||||||
#[error("{0}{panic}", panic = Panic::backtrace())]
|
|
||||||
#[diagnostic(help("set the `RUST_BACKTRACE=1` environment variable to display a backtrace."))]
|
|
||||||
struct Panic(String);
|
struct Panic(String);
|
||||||
|
|
||||||
|
impl Display for Panic {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
let msg = &self.0;
|
||||||
|
let panic = Panic::backtrace();
|
||||||
|
write!(f, "{msg}{panic}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Error for Panic {}
|
||||||
|
|
||||||
|
impl Diagnostic for Panic {
|
||||||
|
fn help<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
|
||||||
|
Some(Box::new(
|
||||||
|
"set the `RUST_BACKTRACE=1` environment variable to display a backtrace.",
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Panic {
|
impl Panic {
|
||||||
fn backtrace() -> String {
|
fn backtrace() -> String {
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
|
|
@ -84,3 +101,32 @@ impl Panic {
|
||||||
"".into()
|
"".into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn panic() {
|
||||||
|
let panic = Panic("ruh roh raggy".to_owned());
|
||||||
|
|
||||||
|
assert_eq!(panic.to_string(), "ruh roh raggy");
|
||||||
|
assert!(panic.source().is_none());
|
||||||
|
assert!(panic.code().is_none());
|
||||||
|
assert!(panic.severity().is_none());
|
||||||
|
assert_eq!(
|
||||||
|
panic.help().map(|h| h.to_string()),
|
||||||
|
Some(
|
||||||
|
"set the `RUST_BACKTRACE=1` environment variable to display a backtrace."
|
||||||
|
.to_owned()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
assert!(panic.url().is_none());
|
||||||
|
assert!(panic.source_code().is_none());
|
||||||
|
assert!(panic.labels().is_none());
|
||||||
|
assert!(panic.related().is_none());
|
||||||
|
assert!(panic.diagnostic_source().is_none());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue