Skip to content

Commit

Permalink
refactor: Update human_panic to v2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
CosmicHorrorDev committed Jun 1, 2024
1 parent 7bc581e commit fe752fa
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ html-escape = "0.2.13"
# Parsing the HTML document that the markdown+html was converted into
html5ever = "0.27.0"
# Provides some extra helpers that we use for our custom panic hook
human-panic = "1.2.3"
human-panic = "2.0.0"
# Generic image decoding
image = "0.25.1"
# 2D GPU graphics rendering
Expand Down
30 changes: 15 additions & 15 deletions src/panic_hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,26 @@ use std::{
path::{Path, PathBuf},
};

use human_panic::{report::Method, Metadata};
use human_panic::report::Method;
use serde::Deserialize;

const PKG_NAME: &str = env!("CARGO_PKG_NAME");
const PKG_VERSION: &str = env!("CARGO_PKG_VERSION");

#[macro_export]
macro_rules! setup_panic {
() => {
match ::human_panic::PanicStyle::default() {
::human_panic::PanicStyle::Debug => {}
::human_panic::PanicStyle::Human => {
let meta = ::human_panic::metadata!();

::std::panic::set_hook(::std::boxed::Box::new(
move |info: &::std::panic::PanicInfo| {
eprintln!("{info}");
let file_path = $crate::panic_hook::handle_dump(&meta, info);
$crate::panic_hook::print_msg(file_path.as_deref(), &meta).unwrap();
let file_path = $crate::panic_hook::handle_dump(info);
$crate::panic_hook::print_msg(file_path.as_deref()).unwrap();
},
));
}
_ => {}
}
};
}
Expand All @@ -48,8 +49,8 @@ struct Report {
}

impl Report {
fn new(name: &str, version: &str, method: Method, explanation: String, cause: String) -> Self {
human_panic::report::Report::new(name, version, method, explanation, cause).into()
fn new(method: Method, explanation: String, cause: String) -> Self {
human_panic::report::Report::new(PKG_NAME, PKG_VERSION, method, explanation, cause).into()
}

fn serialize(&self) -> Option<String> {
Expand Down Expand Up @@ -121,7 +122,7 @@ impl From<human_panic::report::Report> for Report {
}
}

pub fn handle_dump(meta: &Metadata, panic_info: &PanicInfo) -> Option<PathBuf> {
pub fn handle_dump(panic_info: &PanicInfo) -> Option<PathBuf> {
let mut expl = String::new();

let message = match (
Expand All @@ -147,7 +148,7 @@ pub fn handle_dump(meta: &Metadata, panic_info: &PanicInfo) -> Option<PathBuf> {
None => expl.push_str("Panic location unknown.\n"),
}

let report = Report::new(&meta.name, &meta.version, Method::Panic, expl, cause);
let report = Report::new(Method::Panic, expl, cause);
let maybe = report.persist();
if maybe.is_none() {
eprintln!("{}", report.serialize().unwrap());
Expand All @@ -156,21 +157,20 @@ pub fn handle_dump(meta: &Metadata, panic_info: &PanicInfo) -> Option<PathBuf> {
maybe
}

pub fn print_msg(file_path: Option<&Path>, meta: &Metadata) -> Option<()> {
pub fn print_msg(file_path: Option<&Path>) -> Option<()> {
use io::Write as _;

let stderr = anstream::stderr();
let mut stderr = stderr.lock();

write!(stderr, "{}", anstyle::AnsiColor::Red.render_fg()).ok()?;
write_msg(&mut stderr, file_path, meta)?;
write_msg(&mut stderr, file_path)?;
write!(stderr, "{}", anstyle::Reset.render()).ok()?;

Some(())
}

fn write_msg(buffer: &mut impl io::Write, file_path: Option<&Path>, meta: &Metadata) -> Option<()> {
let name = &meta.name;
fn write_msg(buffer: &mut impl io::Write, file_path: Option<&Path>) -> Option<()> {
let report_path = match file_path {
Some(fp) => format!("{}", fp.display()),
None => "<Failed to store file to disk>".to_string(),
Expand All @@ -181,7 +181,7 @@ fn write_msg(buffer: &mut impl io::Write, file_path: Option<&Path>, meta: &Metad
"\
Well, this is embarrassing.
{name} had a problem and crashed. To help us diagnose the problem you can send us a crash report.
{PKG_NAME} had a problem and crashed. To help us diagnose the problem you can send us a crash report.
We have generated a report file at \"{report_path}\". You can search
for issues with similar explanations at the following url:
Expand Down

0 comments on commit fe752fa

Please sign in to comment.