From daa9df7745d3029789cec468bf2bb67d8f250f5b Mon Sep 17 00:00:00 2001 From: maciektr Date: Sun, 5 Nov 2023 12:57:34 +0100 Subject: [PATCH] Add edition field to PackageMetadata --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + scarb-metadata/src/lib.rs | 4 ++++ scarb/Cargo.toml | 3 ++- scarb/src/ops/metadata.rs | 4 ++++ scarb/tests/metadata.rs | 26 ++++++++++++++++++++++++++ 6 files changed, 47 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index d49f08110..a612fbda3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4421,6 +4421,7 @@ dependencies = [ "serde_json", "serde_repr", "serde_test", + "serde_variant", "sha2", "similar-asserts", "smallvec", @@ -4767,6 +4768,15 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_variant" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47a8ec0b2fd0506290348d9699c0e3eb2e3e8c0498b5a9a6158b3bd4d6970076" +dependencies = [ + "serde", +] + [[package]] name = "sha1_smol" version = "1.0.0" diff --git a/Cargo.toml b/Cargo.toml index 42c795464..90707493d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -82,6 +82,7 @@ serde-value = "0.7" serde_json = "1" serde_repr = "0.1" serde_test = "1" +serde_variant = "0.1" sha2 = "0.10" similar-asserts = { version = "1", features = ["serde"] } smallvec = "1" diff --git a/scarb-metadata/src/lib.rs b/scarb-metadata/src/lib.rs index 55cee2130..2c5fe4dbc 100644 --- a/scarb-metadata/src/lib.rs +++ b/scarb-metadata/src/lib.rs @@ -208,6 +208,10 @@ pub struct PackageMetadata { /// Package version as given in `Scarb.toml`. pub version: Version, + /// Package edition as given in `Scarb.toml`. + #[serde(skip_serializing_if = "Option::is_none")] + pub edition: Option, + /// The source of the package. pub source: SourceId, diff --git a/scarb/Cargo.toml b/scarb/Cargo.toml index a71a4efc7..85d8f176f 100644 --- a/scarb/Cargo.toml +++ b/scarb/Cargo.toml @@ -55,8 +55,9 @@ serde-untagged.workspace = true serde-value.workspace = true serde.workspace = true serde_json.workspace = true -sha2.workspace = true serde_repr.workspace = true +serde_variant.workspace = true +sha2.workspace = true smallvec.workspace = true smol_str.workspace = true tar.workspace = true diff --git a/scarb/src/ops/metadata.rs b/scarb/src/ops/metadata.rs index 68058433b..51d270dc6 100644 --- a/scarb/src/ops/metadata.rs +++ b/scarb/src/ops/metadata.rs @@ -3,6 +3,7 @@ use std::collections::{BTreeMap, HashMap}; use anyhow::{bail, Result}; use itertools::Itertools; use semver::{Version, VersionReq}; +use serde_variant::to_variant_name; use smol_str::SmolStr; use scarb_metadata as m; @@ -125,10 +126,13 @@ fn collect_package_metadata(package: &Package) -> m::PackageMetadata { .build() .unwrap(); + let edition = to_variant_name(&package.manifest.edition).unwrap(); + m::PackageMetadataBuilder::default() .id(wrap_package_id(package.id)) .name(package.id.name.clone()) .version(package.id.version.clone()) + .edition(Some(edition.to_string())) .source(wrap_source_id(package.id.source_id)) .manifest_path(package.manifest_path()) .root(package.root()) diff --git a/scarb/tests/metadata.rs b/scarb/tests/metadata.rs index 72a4c9637..319d48517 100644 --- a/scarb/tests/metadata.rs +++ b/scarb/tests/metadata.rs @@ -1112,3 +1112,29 @@ fn infer_readme_workspace() { assert_eq!(packages.get("t5").unwrap().manifest_metadata.readme, None); assert_eq!(packages.get("t6").unwrap().manifest_metadata.readme, None); } + +#[test] +fn includes_edition() { + let t = assert_fs::TempDir::new().unwrap(); + ProjectBuilder::start() + .name("hello") + .version("0.1.0") + .edition("2023_10") + .build(&t); + + let metadata = Scarb::quick_snapbox() + .arg("--json") + .arg("metadata") + .arg("--format-version") + .arg("1") + .current_dir(&t) + .stdout_json::(); + + for package in metadata.packages { + if package.name == "hello" { + assert_eq!(package.edition, Some("2023_10".to_string())); + return; + } + } + panic!("Package not found in metadata!"); +}