Skip to content

Commit a60a6fe

Browse files
committed
Include public/private dependency status in cargo metadata
This change introduces a new method, `Dependency::serialized` which replaces the direct `Serialize` implementation on `Dependency`. This matches the pattern used by `Package` with its `Package::serialized`, and enables us to influence the serialization format with unstable flags. I replaced borrowed types in `SerializedDependency` with owned variants to satisfy the borrow checker. This matches `SerializedPackage` and shouldn't be an issue since `Dependency` is cheap to copy.
1 parent 8995f54 commit a60a6fe

File tree

6 files changed

+372
-39
lines changed

6 files changed

+372
-39
lines changed

src/bin/cargo/commands/read_manifest.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Deprecated, use `<cyan,bold>cargo metadata --no-deps</>` instead.\
1515

1616
pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
1717
let ws = args.workspace(gctx)?;
18-
gctx.shell().print_json(&ws.current()?.serialized())?;
18+
gctx.shell()
19+
.print_json(&ws.current()?.serialized(gctx.cli_unstable()))?;
1920
Ok(())
2021
}

src/cargo/core/dependency.rs

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::sync::Arc;
99
use tracing::trace;
1010

1111
use crate::core::compiler::{CompileKind, CompileTarget};
12-
use crate::core::{PackageId, SourceId, Summary};
12+
use crate::core::{CliUnstable, PackageId, SourceId, Summary};
1313
use crate::util::errors::CargoResult;
1414
use crate::util::interning::InternedString;
1515
use crate::util::OptVersionReq;
@@ -52,50 +52,32 @@ struct Inner {
5252
}
5353

5454
#[derive(Serialize)]
55-
struct SerializedDependency<'a> {
56-
name: &'a str,
55+
pub struct SerializedDependency {
56+
name: InternedString,
5757
source: SourceId,
5858
req: String,
5959
kind: DepKind,
60-
rename: Option<&'a str>,
60+
rename: Option<InternedString>,
6161

6262
optional: bool,
6363
uses_default_features: bool,
64-
features: &'a [InternedString],
64+
features: Vec<InternedString>,
6565
#[serde(skip_serializing_if = "Option::is_none")]
66-
artifact: Option<&'a Artifact>,
67-
target: Option<&'a Platform>,
66+
artifact: Option<Artifact>,
67+
target: Option<Platform>,
6868
/// The registry URL this dependency is from.
6969
/// If None, then it comes from the default registry (crates.io).
70-
registry: Option<&'a str>,
70+
registry: Option<String>,
7171

7272
/// The file system path for a local path dependency.
7373
#[serde(skip_serializing_if = "Option::is_none")]
7474
path: Option<PathBuf>,
75-
}
7675

77-
impl ser::Serialize for Dependency {
78-
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
79-
where
80-
S: ser::Serializer,
81-
{
82-
let registry_id = self.registry_id();
83-
SerializedDependency {
84-
name: &*self.package_name(),
85-
source: self.source_id(),
86-
req: self.version_req().to_string(),
87-
kind: self.kind(),
88-
optional: self.is_optional(),
89-
uses_default_features: self.uses_default_features(),
90-
features: self.features(),
91-
target: self.platform(),
92-
rename: self.explicit_name_in_toml().map(|s| s.as_str()),
93-
registry: registry_id.as_ref().map(|sid| sid.url().as_str()),
94-
path: self.source_id().local_path(),
95-
artifact: self.artifact(),
96-
}
97-
.serialize(s)
98-
}
76+
/// `public` flag is unset if `-Zpublic-dependency` is not enabled
77+
///
78+
/// Once that feature is stabilized, `public` will not need to be `Option`
79+
#[serde(skip_serializing_if = "Option::is_none")]
80+
public: Option<bool>,
9981
}
10082

10183
#[derive(PartialEq, Eq, Hash, Ord, PartialOrd, Clone, Debug, Copy)]
@@ -182,6 +164,26 @@ impl Dependency {
182164
}
183165
}
184166

167+
pub fn serialized(&self, unstable_flags: &CliUnstable) -> SerializedDependency {
168+
SerializedDependency {
169+
name: self.package_name(),
170+
source: self.source_id(),
171+
req: self.version_req().to_string(),
172+
kind: self.kind(),
173+
optional: self.is_optional(),
174+
uses_default_features: self.uses_default_features(),
175+
features: self.features().to_vec(),
176+
target: self.inner.platform.clone(),
177+
rename: self.explicit_name_in_toml(),
178+
registry: self.registry_id().as_ref().map(|sid| sid.url().to_string()),
179+
path: self.source_id().local_path(),
180+
artifact: self.inner.artifact.clone(),
181+
public: unstable_flags
182+
.public_dependency
183+
.then_some(self.inner.public),
184+
}
185+
}
186+
185187
pub fn version_req(&self) -> &OptVersionReq {
186188
&self.inner.req
187189
}

src/cargo/core/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub use self::dependency::Dependency;
1+
pub use self::dependency::{Dependency, SerializedDependency};
22
pub use self::features::{CliUnstable, Edition, Feature, Features};
33
pub use self::manifest::{EitherManifest, VirtualManifest};
44
pub use self::manifest::{Manifest, Target, TargetKind};

src/cargo/core/package.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ use crate::core::compiler::{CompileKind, RustcTargetData};
2222
use crate::core::dependency::DepKind;
2323
use crate::core::resolver::features::ForceAllTargets;
2424
use crate::core::resolver::{HasDevUnits, Resolve};
25-
use crate::core::{Dependency, Manifest, PackageId, PackageIdSpec, SourceId, Target};
25+
use crate::core::{
26+
CliUnstable, Dependency, Manifest, PackageId, PackageIdSpec, SerializedDependency, SourceId,
27+
Target,
28+
};
2629
use crate::core::{Summary, Workspace};
2730
use crate::sources::source::{MaybePackage, SourceMap};
2831
use crate::util::cache_lock::{CacheLock, CacheLockMode};
@@ -73,7 +76,7 @@ pub struct SerializedPackage {
7376
license_file: Option<String>,
7477
description: Option<String>,
7578
source: SourceId,
76-
dependencies: Vec<Dependency>,
79+
dependencies: Vec<SerializedDependency>,
7780
targets: Vec<Target>,
7881
features: BTreeMap<InternedString, Vec<InternedString>>,
7982
manifest_path: PathBuf,
@@ -188,7 +191,7 @@ impl Package {
188191
self.targets().iter().any(|t| t.is_example() || t.is_bin())
189192
}
190193

191-
pub fn serialized(&self) -> SerializedPackage {
194+
pub fn serialized(&self, unstable_flags: &CliUnstable) -> SerializedPackage {
192195
let summary = self.manifest().summary();
193196
let package_id = summary.package_id();
194197
let manmeta = self.manifest().metadata();
@@ -224,7 +227,11 @@ impl Package {
224227
license_file: manmeta.license_file.clone(),
225228
description: manmeta.description.clone(),
226229
source: summary.source_id(),
227-
dependencies: summary.dependencies().to_vec(),
230+
dependencies: summary
231+
.dependencies()
232+
.iter()
233+
.map(|dep| dep.serialized(unstable_flags))
234+
.collect(),
228235
targets,
229236
features,
230237
manifest_path: self.manifest_path().to_path_buf(),

src/cargo/ops/cargo_output_metadata.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ pub fn output_metadata(ws: &Workspace<'_>, opt: &OutputMetadataOptions) -> Cargo
3333
);
3434
}
3535
let (packages, resolve) = if opt.no_deps {
36-
let packages = ws.members().map(|pkg| pkg.serialized()).collect();
36+
let packages = ws
37+
.members()
38+
.map(|pkg| pkg.serialized(ws.gctx().cli_unstable()))
39+
.collect();
3740
(packages, None)
3841
} else {
3942
let (packages, resolve) = build_resolve_graph(ws, opt)?;
@@ -178,7 +181,7 @@ fn build_resolve_graph(
178181
let actual_packages = package_map
179182
.into_iter()
180183
.filter_map(|(pkg_id, pkg)| node_map.get(&pkg_id).map(|_| pkg))
181-
.map(|pkg| pkg.serialized())
184+
.map(|pkg| pkg.serialized(ws.gctx().cli_unstable()))
182185
.collect();
183186

184187
let mr = MetadataResolve {

0 commit comments

Comments
 (0)