mirror of
https://github.com/rust-lang/cargo.git
synced 2025-09-28 11:20:36 +00:00
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.
This commit is contained in:
parent
6829bf7ae0
commit
ac7d8c17ff
@ -52,52 +52,28 @@ struct Inner {
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct SerializedDependency<'a> {
|
||||
name: &'a str,
|
||||
pub struct SerializedDependency {
|
||||
name: InternedString,
|
||||
source: SourceId,
|
||||
req: String,
|
||||
kind: DepKind,
|
||||
rename: Option<&'a str>,
|
||||
rename: Option<InternedString>,
|
||||
|
||||
optional: bool,
|
||||
uses_default_features: bool,
|
||||
features: &'a [InternedString],
|
||||
features: Vec<InternedString>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
artifact: Option<&'a Artifact>,
|
||||
target: Option<&'a Platform>,
|
||||
artifact: Option<Artifact>,
|
||||
target: Option<Platform>,
|
||||
/// The registry URL this dependency is from.
|
||||
/// If None, then it comes from the default registry (crates.io).
|
||||
registry: Option<&'a str>,
|
||||
registry: Option<String>,
|
||||
|
||||
/// The file system path for a local path dependency.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
path: Option<PathBuf>,
|
||||
}
|
||||
|
||||
impl ser::Serialize for Dependency {
|
||||
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: ser::Serializer,
|
||||
{
|
||||
let registry_id = self.registry_id();
|
||||
SerializedDependency {
|
||||
name: &*self.package_name(),
|
||||
source: self.source_id(),
|
||||
req: self.version_req().to_string(),
|
||||
kind: self.kind(),
|
||||
optional: self.is_optional(),
|
||||
uses_default_features: self.uses_default_features(),
|
||||
features: self.features(),
|
||||
target: self.platform(),
|
||||
rename: self.explicit_name_in_toml().map(|s| s.as_str()),
|
||||
registry: registry_id.as_ref().map(|sid| sid.url().as_str()),
|
||||
path: self.source_id().local_path(),
|
||||
artifact: self.artifact(),
|
||||
}
|
||||
.serialize(s)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Ord, PartialOrd, Clone, Debug, Copy)]
|
||||
pub enum DepKind {
|
||||
Normal,
|
||||
@ -182,6 +158,23 @@ impl Dependency {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn serialized(&self) -> SerializedDependency {
|
||||
SerializedDependency {
|
||||
name: self.package_name(),
|
||||
source: self.source_id(),
|
||||
req: self.version_req().to_string(),
|
||||
kind: self.kind(),
|
||||
optional: self.is_optional(),
|
||||
uses_default_features: self.uses_default_features(),
|
||||
features: self.features().to_vec(),
|
||||
target: self.inner.platform.clone(),
|
||||
rename: self.explicit_name_in_toml(),
|
||||
registry: self.registry_id().as_ref().map(|sid| sid.url().to_string()),
|
||||
path: self.source_id().local_path(),
|
||||
artifact: self.inner.artifact.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn version_req(&self) -> &OptVersionReq {
|
||||
&self.inner.req
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
pub use self::dependency::Dependency;
|
||||
pub use self::dependency::{Dependency, SerializedDependency};
|
||||
pub use self::features::{CliUnstable, Edition, Feature, Features};
|
||||
pub use self::manifest::{EitherManifest, VirtualManifest};
|
||||
pub use self::manifest::{Manifest, Target, TargetKind};
|
||||
|
@ -22,7 +22,9 @@ use crate::core::compiler::{CompileKind, RustcTargetData};
|
||||
use crate::core::dependency::DepKind;
|
||||
use crate::core::resolver::features::ForceAllTargets;
|
||||
use crate::core::resolver::{HasDevUnits, Resolve};
|
||||
use crate::core::{Dependency, Manifest, PackageId, PackageIdSpec, SourceId, Target};
|
||||
use crate::core::{
|
||||
Dependency, Manifest, PackageId, PackageIdSpec, SerializedDependency, SourceId, Target,
|
||||
};
|
||||
use crate::core::{Summary, Workspace};
|
||||
use crate::sources::source::{MaybePackage, SourceMap};
|
||||
use crate::util::cache_lock::{CacheLock, CacheLockMode};
|
||||
@ -73,7 +75,7 @@ pub struct SerializedPackage {
|
||||
license_file: Option<String>,
|
||||
description: Option<String>,
|
||||
source: SourceId,
|
||||
dependencies: Vec<Dependency>,
|
||||
dependencies: Vec<SerializedDependency>,
|
||||
targets: Vec<Target>,
|
||||
features: BTreeMap<InternedString, Vec<InternedString>>,
|
||||
manifest_path: PathBuf,
|
||||
@ -224,7 +226,11 @@ impl Package {
|
||||
license_file: manmeta.license_file.clone(),
|
||||
description: manmeta.description.clone(),
|
||||
source: summary.source_id(),
|
||||
dependencies: summary.dependencies().to_vec(),
|
||||
dependencies: summary
|
||||
.dependencies()
|
||||
.iter()
|
||||
.map(Dependency::serialized)
|
||||
.collect(),
|
||||
targets,
|
||||
features,
|
||||
manifest_path: self.manifest_path().to_path_buf(),
|
||||
|
Loading…
x
Reference in New Issue
Block a user