Skip to content

Commit 8578718

Browse files
committed
refactor: intorduce Dependency::serialized replacing Serialize impl
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 will enable us to influence the serialization format with additional arguments. 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 b439062 commit 8578718

File tree

3 files changed

+34
-35
lines changed

3 files changed

+34
-35
lines changed

src/cargo/core/dependency.rs

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -52,52 +52,28 @@ 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>,
7575
}
7676

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-
}
99-
}
100-
10177
#[derive(PartialEq, Eq, Hash, Ord, PartialOrd, Clone, Debug, Copy)]
10278
pub enum DepKind {
10379
Normal,
@@ -182,6 +158,23 @@ impl Dependency {
182158
}
183159
}
184160

161+
pub fn serialized(&self) -> SerializedDependency {
162+
SerializedDependency {
163+
name: self.package_name(),
164+
source: self.source_id(),
165+
req: self.version_req().to_string(),
166+
kind: self.kind(),
167+
optional: self.is_optional(),
168+
uses_default_features: self.uses_default_features(),
169+
features: self.features().to_vec(),
170+
target: self.inner.platform.clone(),
171+
rename: self.explicit_name_in_toml(),
172+
registry: self.registry_id().as_ref().map(|sid| sid.url().to_string()),
173+
path: self.source_id().local_path(),
174+
artifact: self.inner.artifact.clone(),
175+
}
176+
}
177+
185178
pub fn version_req(&self) -> &OptVersionReq {
186179
&self.inner.req
187180
}

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: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ 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+
Dependency, Manifest, PackageId, PackageIdSpec, SerializedDependency, SourceId, Target,
27+
};
2628
use crate::core::{Summary, Workspace};
2729
use crate::sources::source::{MaybePackage, SourceMap};
2830
use crate::util::cache_lock::{CacheLock, CacheLockMode};
@@ -73,7 +75,7 @@ pub struct SerializedPackage {
7375
license_file: Option<String>,
7476
description: Option<String>,
7577
source: SourceId,
76-
dependencies: Vec<Dependency>,
78+
dependencies: Vec<SerializedDependency>,
7779
targets: Vec<Target>,
7880
features: BTreeMap<InternedString, Vec<InternedString>>,
7981
manifest_path: PathBuf,
@@ -224,7 +226,11 @@ impl Package {
224226
license_file: manmeta.license_file.clone(),
225227
description: manmeta.description.clone(),
226228
source: summary.source_id(),
227-
dependencies: summary.dependencies().to_vec(),
229+
dependencies: summary
230+
.dependencies()
231+
.iter()
232+
.map(Dependency::serialized)
233+
.collect(),
228234
targets,
229235
features,
230236
manifest_path: self.manifest_path().to_path_buf(),

0 commit comments

Comments
 (0)