Skip to content

Commit

Permalink
feat: add APIs to build AboutMetadata from Cargo package metadata (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd authored Sep 1, 2023
1 parent 0f7006b commit c1fbde7
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changes/from-cargo-metadata.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"muda": "patch"
---

Added `AboutMetadata::from_cargo_metadata` and `AboutMetadataBuilder::with_cargo_metadata` to build the application metadata from Cargo package metadata.
79 changes: 79 additions & 0 deletions src/about_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,58 @@ impl AboutMetadata {
.unwrap_or_default()
))
}

/// Creates [`AboutMetadata`] from [Cargo metadata][cargo]. The following fields are set by this function.
///
/// - [`AboutMetadata::name`] (from `CARGO_PKG_NAME`)
/// - [`AboutMetadata::version`] (from `CARGO_PKG_VERSION`)
/// - [`AboutMetadata::short_version`] (from `CARGO_PKG_VERSION_MAJOR` and `CARGO_PKG_VERSION_MINOR`)
/// - [`AboutMetadata::authors`] (from `CARGO_PKG_AUTHORS`)
/// - [`AboutMetadata::comments`] (from `CARGO_PKG_DESCRIPTION`)
/// - [`AboutMetadata::license`] (from `CARGO_PKG_LICENSE`)
/// - [`AboutMetadata::homepage`] (from `CARGO_PKG_HOMEPAGE`)
///
/// [cargo]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates
pub fn from_cargo_metadata() -> Self {
#[allow(unused_mut)]
let mut m = Self {
name: Some(env!("CARGO_PKG_NAME").into()),
version: Some(env!("CARGO_PKG_VERSION").into()),
short_version: Some(format!(
"{}.{}",
env!("CARGO_PKG_VERSION_MAJOR"),
env!("CARGO_PKG_VERSION_MINOR"),
)),
..Default::default()
};

#[cfg(not(target_os = "macos"))]
{
let authors = env!("CARGO_PKG_AUTHORS")
.split(';')
.map(|a| a.trim().to_string())
.collect::<Vec<_>>();
m.authors = if !authors.is_empty() {
Some(authors)
} else {
None
};

fn non_empty(s: &str) -> Option<String> {
if !s.is_empty() {
Some(s.to_string())
} else {
None
}
}

m.comments = non_empty(env!("CARGO_PKG_DESCRIPTION"));
m.license = non_empty(env!("CARGO_PKG_LICENSE"));
m.website = non_empty(env!("CARGO_PKG_HOMEPAGE"));
}

m
}
}

/// A builder type for [`AboutMetadata`].
Expand All @@ -81,6 +133,12 @@ impl AboutMetadataBuilder {
Default::default()
}

/// Creates [`AboutMetadataBuilder`] with Cargo metadata.
/// See [`AboutMetadata::from_cargo_metadata`] for more details.
pub fn with_cargo_metadata() -> Self {
Self(AboutMetadata::from_cargo_metadata())
}

/// Sets the application name.
pub fn name<S: Into<String>>(mut self, name: Option<S>) -> Self {
self.0.name = name.map(|s| s.into());
Expand Down Expand Up @@ -174,3 +232,24 @@ impl AboutMetadataBuilder {
self.0
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_build_from_metadata() {
let m = AboutMetadata::from_cargo_metadata();
assert_eq!(m.name, Some("muda".to_string()));
assert!(m.version.is_some());
assert!(m.short_version.is_some());

#[cfg(not(target_os = "macos"))]
{
assert!(matches!(m.authors, Some(a) if !a.is_empty()));
assert!(m.comments.is_some());
assert!(m.license.is_some());
// Note: `m.website` is not tested because this package doesn't have the "website" field
}
}
}

0 comments on commit c1fbde7

Please sign in to comment.