From 3b91f14503e95007dc7c96e9db27ce3db7976cab Mon Sep 17 00:00:00 2001 From: messense Date: Mon, 21 Jun 2021 10:59:45 +0800 Subject: [PATCH] Make `Cargo.toml`'s `authors` field optional Per https://github.com/rust-lang/rfcs/pull/3052 --- src/cargo_toml.rs | 30 +++++++++++++++++++++++++++++- src/metadata.rs | 20 +++++++++++++------- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/cargo_toml.rs b/src/cargo_toml.rs index a5f987244..cc6aaa4f4 100644 --- a/src/cargo_toml.rs +++ b/src/cargo_toml.rs @@ -20,8 +20,8 @@ pub(crate) struct CargoTomlPackage { // https://doc.rust-lang.org/cargo/reference/manifest.html#the-package-section pub(crate) name: String, pub(crate) version: String, - pub(crate) authors: Vec, // All other fields are optional + pub(crate) authors: Option>, pub(crate) description: Option, pub(crate) documentation: Option, pub(crate) homepage: Option, @@ -200,4 +200,32 @@ mod test { assert_eq!(cargo_toml.classifiers(), classifiers); } + + #[test] + fn test_metadata_from_cargo_toml_without_authors() { + let cargo_toml = indoc!( + r#" + [package] + name = "info-project" + version = "0.1.0" + description = "A test project" + homepage = "https://example.org" + keywords = ["ffi", "test"] + + [lib] + crate-type = ["cdylib"] + name = "pyo3_pure" + + [package.metadata.maturin.scripts] + ph = "maturin:print_hello" + + [package.metadata.maturin] + classifiers = ["Programming Language :: Python"] + requires-dist = ["flask~=1.1.0", "toml==0.10.0"] + "# + ); + + let cargo_toml: Result = toml::from_str(&cargo_toml); + assert!(cargo_toml.is_ok()); + } } diff --git a/src/metadata.rs b/src/metadata.rs index 21539599f..540bf33be 100644 --- a/src/metadata.rs +++ b/src/metadata.rs @@ -256,15 +256,21 @@ impl Metadata21 { cargo_toml: &CargoToml, manifest_path: impl AsRef, ) -> Result { - let authors = cargo_toml.package.authors.join(", "); + let authors = cargo_toml + .package + .authors + .as_ref() + .map(|authors| authors.join(", ")); let classifiers = cargo_toml.classifiers(); - let author_email = if authors.contains('@') { - Some(authors.clone()) - } else { - None - }; + let author_email = authors.as_ref().and_then(|authors| { + if authors.contains('@') { + Some(authors.clone()) + } else { + None + } + }); let extra_metadata = cargo_toml.remaining_core_metadata(); @@ -313,7 +319,7 @@ impl Metadata21 { home_page: cargo_toml.package.homepage.clone(), download_url: None, // Cargo.toml has no distinction between author and author email - author: Some(authors), + author: authors, author_email, license: cargo_toml.package.license.clone(),