Repository: oli-obk/cargo_metadata
Pull request #174 (2022-04-17 21:12:58)

Small changes to improve the overall crate quality
see #175


Patch:
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index 8a97ed4..e0a2296 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -21,7 +21,7 @@ jobs:
           - rust: stable
           - rust: beta
           - rust: nightly
-          - rust: 1.40.0
+          - rust: 1.42.0
     steps:
     - uses: actions/checkout@v2
     - name: Install rust
diff --git a/Cargo.toml b/Cargo.toml
index c34928a..fd9c36a 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -7,14 +7,15 @@ description = "structured access to the output of `cargo metadata`"
 license = "MIT"
 readme = "README.md"
 edition = "2018"
+rust-version = "1.42.0"
 
 [dependencies]
-camino = { version = "1.0.1", features = ["serde1"] }
-cargo-platform = "0.1"
-derive_builder = { version = "0.9.0", optional = true }
-semver = { version = "1", features = ["serde"] }
-serde = { version = "1.0.107", features = ["derive"] }
-serde_json = { version = "1.0.59", features = ["unbounded_depth"] }
+camino = { version = "1.0.7", features = ["serde1"] }
+cargo-platform = "0.1.2"
+derive_builder = { version = "0.11.1", optional = true }
+semver = { version = "1.0.7", features = ["serde"] }
+serde = { version = "1.0.136", features = ["derive"] }
+serde_json = { version = "1.0.79", features = ["unbounded_depth"] }
 
 [features]
 default = []
diff --git a/src/lib.rs b/src/lib.rs
index c88d324..3ebcc76 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -124,12 +124,9 @@ impl std::fmt::Display for PackageId {
     }
 }
 
-// Helpers for default metadata fields
+/// Helpers for default metadata fields
 fn is_null(value: &serde_json::Value) -> bool {
-    match value {
-        serde_json::Value::Null => true,
-        _ => false,
-    }
+    matches!(value, serde_json::Value::Null)
 }
 
 #[derive(Clone, Serialize, Deserialize, Debug)]
@@ -310,8 +307,8 @@ pub struct Package {
     ///
     /// Beware that individual targets may specify their own edition in
     /// [`Target::edition`].
-    #[serde(default = "edition_default")]
-    pub edition: String,
+    #[serde(default)]
+    pub edition: Edition,
     /// Contents of the free form package.metadata section
     ///
     /// This contents can be serialized to a struct using serde:
@@ -420,9 +417,9 @@ pub struct Target {
     /// Path to the main source file of the target
     pub src_path: Utf8PathBuf,
     /// Rust edition for this target
-    #[serde(default = "edition_default")]
-    #[cfg_attr(feature = "builder", builder(default = "edition_default()"))]
-    pub edition: String,
+    #[serde(default)]
+    #[cfg_attr(feature = "builder", builder(default))]
+    pub edition: Edition,
     /// Whether or not this target has doc tests enabled, and the target is
     /// compatible with doc testing.
     ///
@@ -444,12 +441,29 @@ pub struct Target {
     pub doc: bool,
 }
 
-fn default_true() -> bool {
-    true
+#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
+#[non_exhaustive]
+/// The rust edition
+pub enum Edition {
+    /// Edition 2015
+    #[serde(rename = "2015")]
+    E2015,
+    /// Edition 2018
+    #[serde(rename = "2018")]
+    E2018,
+    /// Edition 2021
+    #[serde(rename = "2021")]
+    E2021,
+}
+
+impl Default for Edition {
+    fn default() -> Self {
+        Self::E2015
+    }
 }
 
-fn edition_default() -> String {
-    "2015".to_string()
+fn default_true() -> bool {
+    true
 }
 
 /// Cargo features flags


Test patch:
diff --git a/tests/test_samples.rs b/tests/test_samples.rs
index f899e6d..27a02f5 100644
--- a/tests/test_samples.rs
+++ b/tests/test_samples.rs
@@ -4,7 +4,7 @@ extern crate semver;
 extern crate serde_json;
 
 use camino::Utf8PathBuf;
-use cargo_metadata::{CargoOpt, DependencyKind, Metadata, MetadataCommand};
+use cargo_metadata::{CargoOpt, DependencyKind, Edition, Metadata, MetadataCommand};
 
 #[test]
 fn old_minimal() {
@@ -81,8 +81,8 @@ fn old_minimal() {
     assert_eq!(dep.source, None);
     assert_eq!(dep.req, semver::VersionReq::parse("^1.0").unwrap());
     assert_eq!(dep.kind, DependencyKind::Normal);
-    assert_eq!(dep.optional, false);
-    assert_eq!(dep.uses_default_features, true);
+    assert!(!dep.optional);
+    assert!(dep.uses_default_features);
     assert_eq!(dep.features.len(), 0);
     assert!(dep.target.is_none());
     assert_eq!(dep.rename, None);
@@ -94,10 +94,10 @@ fn old_minimal() {
     assert_eq!(target.crate_types, vec!["bin"]);
     assert_eq!(target.required_features.len(), 0);
     assert_eq!(target.src_path, "/foo/src/main.rs");
-    assert_eq!(target.edition, "2015");
-    assert_eq!(target.doctest, true);
-    assert_eq!(target.test, true);
-    assert_eq!(target.doc, true);
+    assert_eq!(target.edition, Edition::E2015);
+    assert!(target.doctest);
+    assert!(target.test);
+    assert!(target.doc);
     assert_eq!(pkg.features.len(), 0);
     assert_eq!(pkg.manifest_path, "/foo/Cargo.toml");
     assert_eq!(pkg.categories.len(), 0);
@@ -106,7 +106,7 @@ fn old_minimal() {
     assert_eq!(pkg.repository, None);
     assert_eq!(pkg.homepage, None);
     assert_eq!(pkg.documentation, None);
-    assert_eq!(pkg.edition, "2015");
+    assert_eq!(pkg.edition, Edition::E2015);
     assert_eq!(pkg.metadata, serde_json::Value::Null);
     assert_eq!(pkg.links, None);
     assert_eq!(pkg.publish, None);
@@ -224,7 +224,7 @@ fn all_the_fields() {
         bitflags.source,
         Some("registry+https://github.com/rust-lang/crates.io-index".to_string())
     );
-    assert_eq!(bitflags.optional, true);
+    assert!(bitflags.optional);
     assert_eq!(bitflags.req, semver::VersionReq::parse("^1.0").unwrap());
 
     let path_dep = all
@@ -251,7 +251,7 @@ fn all_the_fields() {
         .find(|d| d.name == "featdep")
         .unwrap();
     assert_eq!(featdep.features, vec!["i128"]);
-    assert_eq!(featdep.uses_default_features, false);
+    assert!(!featdep.uses_default_features);
 
     let renamed = all
         .dependencies
@@ -297,28 +297,28 @@ fn all_the_fields() {
         vec!["cdylib", "rlib", "staticlib"]
     );
     assert_eq!(lib.required_features.len(), 0);
-    assert_eq!(lib.edition, "2018");
-    assert_eq!(lib.doctest, true);
-    assert_eq!(lib.test, true);
-    assert_eq!(lib.doc, true);
+    assert_eq!(lib.edition, Edition::E2018);
+    assert!(lib.doctest);
+    assert!(lib.test);
+    assert!(lib.doc);
 
     let main = get_file_name!("main.rs");
     assert_eq!(main.crate_types, vec!["bin"]);
     assert_eq!(main.kind, vec!["bin"]);
-    assert_eq!(main.doctest, false);
-    assert_eq!(main.test, true);
-    assert_eq!(main.doc, true);
+    assert!(!main.doctest);
+    assert!(main.test);
+    assert!(main.doc);
 
     let otherbin = get_file_name!("otherbin.rs");
-    assert_eq!(otherbin.edition, "2015");
-    assert_eq!(otherbin.doc, false);
+    assert_eq!(otherbin.edition, Edition::E2015);
+    assert!(!otherbin.doc);
 
     let reqfeat = get_file_name!("reqfeat.rs");
     assert_eq!(reqfeat.required_features, vec!["feat2"]);
 
     let ex1 = get_file_name!("ex1.rs");
     assert_eq!(ex1.kind, vec!["example"]);
-    assert_eq!(ex1.test, false);
+    assert!(!ex1.test);
 
     let t1 = get_file_name!("t1.rs");
     assert_eq!(t1.kind, vec!["test"]);
@@ -356,7 +356,7 @@ fn all_the_fields() {
         all.documentation,
         Some("https://docs.rs/cargo_metadata/".to_string())
     );
-    assert_eq!(all.edition, "2018");
+    assert_eq!(all.edition, Edition::E2018);
     assert_eq!(
         all.metadata,
         json!({