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

feat: Add support section #133

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
5 changes: 2 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ on:
- master

env:
RUST_BACKTRACE: 1
CARGO_TERM_COLOR: always
CLICOLOR: 1

Expand Down Expand Up @@ -45,9 +44,9 @@ jobs:
- name: Test Debug
run: cargo test
- name: Build Release
run: cargo test --no-run --release
run: cargo test --no-run --release --workspace
- name: Test Release
run: cargo test --release
run: cargo test --release --workspace
msrv:
name: "Check MSRV: 1.72"
runs-on: ubuntu-latest
Expand Down
49 changes: 29 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ use std::io::Result as IoResult;
use std::panic::PanicInfo;
use std::path::{Path, PathBuf};

pub type MaybeString = Option<Cow<'static, str>>;

/// A convenient metadata struct that describes a crate
///
/// See [`metadata!`]
Expand All @@ -61,20 +63,23 @@ pub struct Metadata {
/// The crate name
pub name: Cow<'static, str>,
/// The list of authors of the crate
pub authors: Cow<'static, str>,
pub authors: MaybeString,
/// The URL of the crate's website
pub homepage: Cow<'static, str>,
pub homepage: MaybeString,
/// The support information
pub support: MaybeString,
}

/// Initialize [`Metadata`]
/// Initialize [`Metadata`].
#[macro_export]
macro_rules! metadata {
() => {{
$crate::Metadata {
version: env!("CARGO_PKG_VERSION").into(),
name: env!("CARGO_PKG_NAME").into(),
authors: env!("CARGO_PKG_AUTHORS").replace(":", ", ").into(),
homepage: env!("CARGO_PKG_HOMEPAGE").into(),
authors: Some(env!("CARGO_PKG_AUTHORS").replace(":", ", ").into()),
homepage: None,
epage marked this conversation as resolved.
Show resolved Hide resolved
support: None,
}
}};
}
Expand All @@ -92,14 +97,13 @@ macro_rules! metadata {
/// `main()` function of the program.
///
/// ```
/// use human_panic::setup_panic;
/// use human_panic::{metadata, setup_panic};
///
/// setup_panic!(Metadata {
/// name: env!("CARGO_PKG_NAME").into(),
/// version: env!("CARGO_PKG_VERSION").into(),
/// authors: "My Company Support <[email protected]>".into(),
/// homepage: "support.mycompany.com".into(),
/// });
/// let mut metadata = metadata!();
epage marked this conversation as resolved.
Show resolved Hide resolved
/// metadata.authors = Some("My Company Support <[email protected]>".into());
/// metadata.homepage = Some("www.mycompany.com".into());
/// metadata.support = Some("- Open a support request by email to [email protected]".into());
/// setup_panic!(metadata);
/// ```
#[macro_export]
macro_rules! setup_panic {
Expand Down Expand Up @@ -180,8 +184,13 @@ fn write_msg<P: AsRef<Path>>(
file_path: Option<P>,
meta: &Metadata,
) -> IoResult<()> {
let (_version, name, authors, homepage) =
(&meta.version, &meta.name, &meta.authors, &meta.homepage);
let Metadata {
name,
authors,
homepage,
support,
..
} = meta;

writeln!(buffer, "Well, this is embarrassing.\n")?;
writeln!(
Expand All @@ -201,12 +210,15 @@ fn write_msg<P: AsRef<Path>>(
name
)?;

if !homepage.is_empty() {
if let Some(homepage) = homepage {
writeln!(buffer, "- Homepage: {homepage}")?;
}
if !authors.is_empty() {
if let Some(authors) = authors {
writeln!(buffer, "- Authors: {authors}")?;
}
if let Some(support) = support {
writeln!(buffer, "\nTo submit the crash report:\n\n{support}")?;
}
writeln!(
buffer,
"\nWe take privacy seriously, and do not perform any \
Expand Down Expand Up @@ -235,10 +247,7 @@ pub fn handle_dump(meta: &Metadata, panic_info: &PanicInfo) -> Option<PathBuf> {
(None, None) => None,
};

let cause = match message {
Some(m) => m,
None => "Unknown".into(),
};
let cause = message.unwrap_or_else(|| "Unknown".into());

match panic_info.location() {
Some(location) => expl.push_str(&format!(
Expand Down
15 changes: 8 additions & 7 deletions tests/custom-panic/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use human_panic::setup_panic;
use human_panic::{metadata, setup_panic};

fn main() {
setup_panic!(Metadata {
name: env!("CARGO_PKG_NAME").into(),
version: env!("CARGO_PKG_VERSION").into(),
authors: "My Company Support <[email protected]".into(),
homepage: "support.mycompany.com".into(),
});
let mut metadata = metadata!();
metadata.name = env!("CARGO_PKG_NAME").into();
metadata.version = env!("CARGO_PKG_VERSION").into();
metadata.authors = Some("My Company Support <[email protected]".into());
metadata.homepage = Some("www.mycompany.com".into());
metadata.support = Some("- Open a support request by email to [email protected]".into());
setup_panic!(metadata);

println!("A normal log message");
panic!("OMG EVERYTHING IS ON FIRE!!!");
Expand Down
9 changes: 7 additions & 2 deletions tests/custom-panic/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ custom-panic-test had a problem and crashed. To help us diagnose the problem you
We have generated a report file at \"[..].toml\". Submit an issue or email with the subject of \"custom-panic-test Crash Report\" and include the report as an attachment.
- Homepage: support.mycompany.com
- Homepage: www.mycompany.com
- Authors: My Company Support <[email protected]
To submit the crash report:
- Open a support request by email to [email protected]
We take privacy seriously, and do not perform any automated error collection. In order to improve the software, we rely on people to submit reports.
Thank you kindly!
Expand All @@ -29,7 +33,8 @@ fn debug() {
.assert()
.stderr_matches(
"\
thread 'main' panicked at 'OMG EVERYTHING IS ON FIRE!!!', tests/custom-panic/src/main.rs:12:3
thread 'main' panicked at tests/custom-panic/src/main.rs:13:5:
OMG EVERYTHING IS ON FIRE!!!
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
",
)
Expand Down
3 changes: 2 additions & 1 deletion tests/single-panic/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ fn debug() {
.assert()
.stderr_matches(
"\
thread 'main' panicked at 'OMG EVERYTHING IS ON FIRE!!!', tests/single-panic/src/main.rs:7:3
thread 'main' panicked at tests/single-panic/src/main.rs:7:5:
OMG EVERYTHING IS ON FIRE!!!
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
",
)
Expand Down
Loading