Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use cargo_util_schemas::{PackageName, FeatureName} #260

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -12,6 +12,7 @@ rust-version = "1.56.0"
[dependencies]
camino = { version = "1.0.7", features = ["serde1"] }
cargo-platform = "0.1.2"
cargo-util-schemas = "0.2.0"
derive_builder = { version = "0.12", optional = true }
semver = { version = "1.0.7", features = ["serde"] }
serde = { version = "1.0.136", features = ["derive"] }
Expand Down
13 changes: 7 additions & 6 deletions src/lib.rs
Expand Up @@ -94,6 +94,7 @@ pub use camino;
pub use semver;
use semver::Version;

use cargo_util_schemas::manifest::{FeatureName, PackageName};
#[cfg(feature = "builder")]
pub use dependency::DependencyBuilder;
pub use dependency::{Dependency, DependencyKind};
Expand Down Expand Up @@ -292,7 +293,7 @@ pub struct Node {

/// Features enabled on the crate
#[serde(default)]
pub features: Vec<String>,
pub features: Vec<FeatureName>,
}

#[derive(Clone, Serialize, Deserialize, Debug)]
Expand All @@ -303,7 +304,7 @@ pub struct Node {
pub struct NodeDep {
/// The name of the dependency's library target.
/// If the crate was renamed, it is the new name.
pub name: String,
pub name: String, // TODO(aatifsyed): should this be PackageName?
aatifsyed marked this conversation as resolved.
Show resolved Hide resolved
/// Package ID (opaque unique identifier)
pub pkg: PackageId,
/// The kinds of dependencies.
Expand Down Expand Up @@ -347,7 +348,7 @@ pub struct DepKindInfo {
pub struct Package {
/// The [`name` field](https://doc.rust-lang.org/cargo/reference/manifest.html#the-name-field) as given in the `Cargo.toml`
// (We say "given in" instead of "specified in" since the `name` key cannot be inherited from the workspace.)
pub name: String,
pub name: PackageName,
/// The [`version` field](https://doc.rust-lang.org/cargo/reference/manifest.html#the-version-field) as specified in the `Cargo.toml`
pub version: Version,
/// The [`authors` field](https://doc.rust-lang.org/cargo/reference/manifest.html#the-authors-field) as specified in the `Cargo.toml`
Expand All @@ -357,7 +358,7 @@ pub struct Package {
pub id: PackageId,
/// The source of the package, e.g.
/// crates.io or `None` for local projects.
pub source: Option<Source>,
pub source: Option<Source>, // TODO(aatifsyed): should this be RegistryName?
aatifsyed marked this conversation as resolved.
Show resolved Hide resolved
/// The [`description` field](https://doc.rust-lang.org/cargo/reference/manifest.html#the-description-field) as specified in the `Cargo.toml`
pub description: Option<String>,
/// List of dependencies of this particular package
Expand All @@ -371,7 +372,7 @@ pub struct Package {
/// Targets provided by the crate (lib, bin, example, test, ...)
pub targets: Vec<Target>,
/// Features provided by the crate, mapped to the features required by that feature.
pub features: BTreeMap<String, Vec<String>>,
pub features: BTreeMap<FeatureName, Vec<String>>,
/// Path containing the `Cargo.toml`
pub manifest_path: Utf8PathBuf,
/// The [`categories` field](https://doc.rust-lang.org/cargo/reference/manifest.html#the-categories-field) as specified in the `Cargo.toml`
Expand Down Expand Up @@ -525,7 +526,7 @@ pub struct Target {
#[serde(rename = "required-features")]
/// This target is built only if these features are enabled.
/// It doesn't apply to `lib` targets.
pub required_features: Vec<String>,
pub required_features: Vec<FeatureName>,
/// Path to the main source file of the target
pub src_path: Utf8PathBuf,
/// Rust edition for this target
Expand Down
4 changes: 2 additions & 2 deletions tests/selftest.rs
Expand Up @@ -17,7 +17,7 @@ fn metadata() {
let metadata = MetadataCommand::new().no_deps().exec().unwrap();

let this = &metadata.packages[0];
assert_eq!(this.name, "cargo_metadata");
assert_eq!(this.name.as_str(), "cargo_metadata");
assert_eq!(this.targets.len(), 3);

let lib = this
Expand Down Expand Up @@ -130,7 +130,7 @@ fn metadata_deps() {
.expect("Did not find ourselves");
let this = &metadata[this_id];

assert_eq!(this.name, "cargo_metadata");
assert_eq!(this.name.as_str(), "cargo_metadata");

let workspace_packages = metadata.workspace_packages();
assert_eq!(workspace_packages.len(), 1);
Expand Down
45 changes: 35 additions & 10 deletions tests/test_samples.rs
Expand Up @@ -8,6 +8,7 @@ use cargo_metadata::{
workspace_default_members_is_missing, ArtifactDebuginfo, CargoOpt, DependencyKind, Edition,
Message, Metadata, MetadataCommand,
};
use cargo_util_schemas::manifest::FeatureName;

/// Output from oldest version ever supported (1.24).
///
Expand Down Expand Up @@ -71,7 +72,7 @@ fn old_minimal() {
let meta: Metadata = serde_json::from_str(JSON_OLD_MINIMAL).unwrap();
assert_eq!(meta.packages.len(), 1);
let pkg = &meta.packages[0];
assert_eq!(pkg.name, "foo");
assert_eq!(pkg.name.as_str(), "foo");
assert_eq!(pkg.version, semver::Version::parse("0.1.0").unwrap());
assert_eq!(pkg.authors.len(), 0);
assert_eq!(pkg.id.to_string(), "foo 0.1.0 (path+file:///foo)");
Expand Down Expand Up @@ -143,6 +144,14 @@ macro_rules! sorted {
}};
}

macro_rules! features {
($($feat:expr),* $(,)?) => {
::std::vec![
$(::cargo_util_schemas::manifest::FeatureName::new(String::from($feat)).unwrap()),*
]
};
}

fn cargo_version() -> semver::Version {
let output = std::process::Command::new("cargo")
.arg("-V")
Expand Down Expand Up @@ -214,7 +223,11 @@ fn all_the_fields() {
}

assert_eq!(meta.packages.len(), 9);
let all = meta.packages.iter().find(|p| p.name == "all").unwrap();
let all = meta
.packages
.iter()
.find(|p| p.name.as_str() == "all")
.unwrap();
assert_eq!(all.version, semver::Version::parse("0.1.0").unwrap());
assert_eq!(all.authors, vec!["Jane Doe <user@example.com>"]);
assert!(all.id.to_string().contains("all"));
Expand Down Expand Up @@ -335,7 +348,7 @@ fn all_the_fields() {
assert!(!otherbin.doc);

let reqfeat = get_file_name!("reqfeat.rs");
assert_eq!(reqfeat.required_features, vec!["feat2"]);
assert_eq!(reqfeat.required_features, features!["feat2"]);

let ex1 = get_file_name!("ex1.rs");
assert_eq!(ex1.kind, vec!["example".into()]);
Expand Down Expand Up @@ -410,14 +423,14 @@ fn all_the_fields() {
.iter()
.find(|n| n.id.to_string().contains("bitflags"))
.unwrap();
assert_eq!(bitflags.features, vec!["default"]);
assert_eq!(bitflags.features, features!["default"]);

let featdep = resolve
.nodes
.iter()
.find(|n| n.id.to_string().contains("featdep"))
.unwrap();
assert_eq!(featdep.features, vec!["i128"]);
assert_eq!(featdep.features, features!["i128"]);

let all = resolve
.nodes
Expand All @@ -442,7 +455,10 @@ fn all_the_fields() {
.find(|d| d.name == "different_name")
.unwrap();
assert!(namedep.pkg.to_string().contains("namedep"));
assert_eq!(sorted!(all.features), vec!["bitflags", "default", "feat1"]);
assert_eq!(
sorted!(all.features),
features!["bitflags", "default", "feat1"]
);

let bdep = all.deps.iter().find(|d| d.name == "bdep").unwrap();
assert_eq!(bdep.dep_kinds.len(), 1);
Expand Down Expand Up @@ -553,7 +569,11 @@ fn current_dir() {
.current_dir("tests/all/namedep")
.exec()
.unwrap();
let namedep = meta.packages.iter().find(|p| p.name == "namedep").unwrap();
let namedep = meta
.packages
.iter()
.find(|p| p.name.as_str() == "namedep")
.unwrap();
assert!(namedep.name.starts_with("namedep"));
}

Expand Down Expand Up @@ -597,7 +617,11 @@ fn advanced_feature_configuration() {
.find(|n| !n.features.is_empty())
.unwrap();

all.features.clone()
all.features
.clone()
.into_iter()
.map(FeatureName::into_inner)
.collect()
}

// Default behavior; tested above
Expand Down Expand Up @@ -659,7 +683,7 @@ fn basic_workspace_root_package_exists() {
.manifest_path("tests/basic_workspace/Cargo.toml")
.exec()
.unwrap();
assert_eq!(meta.root_package().unwrap().name, "ex_bin");
assert_eq!(meta.root_package().unwrap().name.as_str(), "ex_bin");
// Now with no_deps, it should still work exactly the same
let meta = MetadataCommand::new()
.manifest_path("tests/basic_workspace/Cargo.toml")
Expand All @@ -669,7 +693,8 @@ fn basic_workspace_root_package_exists() {
assert_eq!(
meta.root_package()
.expect("workspace root still exists when no_deps used")
.name,
.name
.as_str(),
"ex_bin"
);
}
Expand Down