Skip to content

Commit

Permalink
fix!: Add a builder API to Metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Apr 17, 2024
1 parent 2215653 commit 86a3f15
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 29 deletions.
62 changes: 41 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,50 @@ use std::path::{Path, PathBuf};
/// A convenient metadata struct that describes a crate
///
/// See [`metadata!`]
#[allow(clippy::exhaustive_structs)]
pub struct Metadata {
/// The crate version
pub version: Cow<'static, str>,
/// The crate name
pub name: Cow<'static, str>,
name: Cow<'static, str>,
version: Cow<'static, str>,
authors: Option<Cow<'static, str>>,
homepage: Option<Cow<'static, str>>,
}

impl Metadata {
/// See [`metadata!`]
pub fn new(name: impl Into<Cow<'static, str>>, version: impl Into<Cow<'static, str>>) -> Self {
Self {
name: name.into(),
version: version.into(),
authors: None,
homepage: None,
}
}

/// The list of authors of the crate
pub authors: Cow<'static, str>,
pub fn authors(mut self, value: impl Into<Cow<'static, str>>) -> Self {
let value = value.into();
if !value.is_empty() {
self.authors = value.into();
}
self
}

/// The URL of the crate's website
pub homepage: Cow<'static, str>,
pub fn homepage(mut self, value: impl Into<Cow<'static, str>>) -> Self {
let value = value.into();
if !value.is_empty() {
self.homepage = value.into();
}
self
}
}

/// 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(),
}
$crate::Metadata::new(env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"))
.authors(env!("CARGO_PKG_AUTHORS").replace(":", ", "))
.homepage(env!("CARGO_PKG_HOMEPAGE"))
}};
}

Expand All @@ -96,12 +118,10 @@ macro_rules! metadata {
/// use human_panic::setup_panic;
/// use human_panic::Metadata;
///
/// setup_panic!(Metadata {
/// name: env!("CARGO_PKG_NAME").into(),
/// version: env!("CARGO_PKG_VERSION").into(),
/// authors: "My Company Support <support@mycompany.com>".into(),
/// homepage: "support.mycompany.com".into(),
/// });
/// setup_panic!(Metadata::new(env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"))
/// .authors("My Company Support <support@mycompany.com>")
/// .homepage("support.mycompany.com")
/// );
/// ```
#[macro_export]
macro_rules! setup_panic {
Expand Down Expand Up @@ -207,10 +227,10 @@ 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}")?;
}
writeln!(
Expand Down
11 changes: 4 additions & 7 deletions tests/custom-panic/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use human_panic::metadata;
use human_panic::setup_panic;
use human_panic::Metadata;

fn main() {
setup_panic!(Metadata {
name: env!("CARGO_PKG_NAME").into(),
version: env!("CARGO_PKG_VERSION").into(),
authors: "My Company Support <support@mycompany.com".into(),
homepage: "support.mycompany.com".into(),
});
setup_panic!(metadata!()
.authors("My Company Support <support@mycompany.com")
.homepage("support.mycompany.com"));

println!("A normal log message");
panic!("OMG EVERYTHING IS ON FIRE!!!");
Expand Down
2 changes: 1 addition & 1 deletion tests/custom-panic/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn debug() {
snapbox::cmd::Command::new(snapbox::cmd::cargo_bin!("custom-panic-test"))
.assert()
.stderr_matches(snapbox::str![[r#"
thread 'main' panicked at tests/custom-panic/src/main.rs:13:5:
thread 'main' panicked at tests/custom-panic/src/main.rs:10:5:
OMG EVERYTHING IS ON FIRE!!!
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
"#]])
Expand Down

0 comments on commit 86a3f15

Please sign in to comment.