Skip to content

Commit

Permalink
refactor(toml): Rename 'resolved' to 'normalized'
Browse files Browse the repository at this point in the history
In a discussion on an issue, it became confusing to talk about
"resolved" manifests and dependency resolution,
so I'm switching manifests to use the other term I considered,
"normalized".
  • Loading branch information
epage committed Aug 1, 2024
1 parent 354f9b2 commit 011fb34
Show file tree
Hide file tree
Showing 13 changed files with 274 additions and 255 deletions.
85 changes: 47 additions & 38 deletions crates/cargo-util-schemas/src/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ impl TomlManifest {
self.features.as_ref()
}

pub fn resolved_lints(&self) -> Result<Option<&TomlLints>, UnresolvedError> {
self.lints.as_ref().map(|l| l.resolved()).transpose()
pub fn normalized_lints(&self) -> Result<Option<&TomlLints>, UnresolvedError> {
self.lints.as_ref().map(|l| l.normalized()).transpose()
}
}

Expand Down Expand Up @@ -237,23 +237,26 @@ impl TomlPackage {
}
}

pub fn resolved_edition(&self) -> Result<Option<&String>, UnresolvedError> {
self.edition.as_ref().map(|v| v.resolved()).transpose()
pub fn normalized_edition(&self) -> Result<Option<&String>, UnresolvedError> {
self.edition.as_ref().map(|v| v.normalized()).transpose()
}

pub fn resolved_rust_version(&self) -> Result<Option<&RustVersion>, UnresolvedError> {
self.rust_version.as_ref().map(|v| v.resolved()).transpose()
pub fn normalized_rust_version(&self) -> Result<Option<&RustVersion>, UnresolvedError> {
self.rust_version
.as_ref()
.map(|v| v.normalized())
.transpose()
}

pub fn resolved_version(&self) -> Result<Option<&semver::Version>, UnresolvedError> {
self.version.as_ref().map(|v| v.resolved()).transpose()
pub fn normalized_version(&self) -> Result<Option<&semver::Version>, UnresolvedError> {
self.version.as_ref().map(|v| v.normalized()).transpose()
}

pub fn resolved_authors(&self) -> Result<Option<&Vec<String>>, UnresolvedError> {
self.authors.as_ref().map(|v| v.resolved()).transpose()
pub fn normalized_authors(&self) -> Result<Option<&Vec<String>>, UnresolvedError> {
self.authors.as_ref().map(|v| v.normalized()).transpose()
}

pub fn resolved_build(&self) -> Result<Option<&String>, UnresolvedError> {
pub fn normalized_build(&self) -> Result<Option<&String>, UnresolvedError> {
let readme = self.build.as_ref().ok_or(UnresolvedError)?;
match readme {
StringOrBool::Bool(false) => Ok(None),
Expand All @@ -262,60 +265,66 @@ impl TomlPackage {
}
}

pub fn resolved_exclude(&self) -> Result<Option<&Vec<String>>, UnresolvedError> {
self.exclude.as_ref().map(|v| v.resolved()).transpose()
pub fn normalized_exclude(&self) -> Result<Option<&Vec<String>>, UnresolvedError> {
self.exclude.as_ref().map(|v| v.normalized()).transpose()
}

pub fn resolved_include(&self) -> Result<Option<&Vec<String>>, UnresolvedError> {
self.include.as_ref().map(|v| v.resolved()).transpose()
pub fn normalized_include(&self) -> Result<Option<&Vec<String>>, UnresolvedError> {
self.include.as_ref().map(|v| v.normalized()).transpose()
}

pub fn resolved_publish(&self) -> Result<Option<&VecStringOrBool>, UnresolvedError> {
self.publish.as_ref().map(|v| v.resolved()).transpose()
pub fn normalized_publish(&self) -> Result<Option<&VecStringOrBool>, UnresolvedError> {
self.publish.as_ref().map(|v| v.normalized()).transpose()
}

pub fn resolved_description(&self) -> Result<Option<&String>, UnresolvedError> {
self.description.as_ref().map(|v| v.resolved()).transpose()
pub fn normalized_description(&self) -> Result<Option<&String>, UnresolvedError> {
self.description
.as_ref()
.map(|v| v.normalized())
.transpose()
}

pub fn resolved_homepage(&self) -> Result<Option<&String>, UnresolvedError> {
self.homepage.as_ref().map(|v| v.resolved()).transpose()
pub fn normalized_homepage(&self) -> Result<Option<&String>, UnresolvedError> {
self.homepage.as_ref().map(|v| v.normalized()).transpose()
}

pub fn resolved_documentation(&self) -> Result<Option<&String>, UnresolvedError> {
pub fn normalized_documentation(&self) -> Result<Option<&String>, UnresolvedError> {
self.documentation
.as_ref()
.map(|v| v.resolved())
.map(|v| v.normalized())
.transpose()
}

pub fn resolved_readme(&self) -> Result<Option<&String>, UnresolvedError> {
pub fn normalized_readme(&self) -> Result<Option<&String>, UnresolvedError> {
let readme = self.readme.as_ref().ok_or(UnresolvedError)?;
readme.resolved().and_then(|sb| match sb {
readme.normalized().and_then(|sb| match sb {
StringOrBool::Bool(false) => Ok(None),
StringOrBool::Bool(true) => Err(UnresolvedError),
StringOrBool::String(value) => Ok(Some(value)),
})
}

pub fn resolved_keywords(&self) -> Result<Option<&Vec<String>>, UnresolvedError> {
self.keywords.as_ref().map(|v| v.resolved()).transpose()
pub fn normalized_keywords(&self) -> Result<Option<&Vec<String>>, UnresolvedError> {
self.keywords.as_ref().map(|v| v.normalized()).transpose()
}

pub fn resolved_categories(&self) -> Result<Option<&Vec<String>>, UnresolvedError> {
self.categories.as_ref().map(|v| v.resolved()).transpose()
pub fn normalized_categories(&self) -> Result<Option<&Vec<String>>, UnresolvedError> {
self.categories.as_ref().map(|v| v.normalized()).transpose()
}

pub fn resolved_license(&self) -> Result<Option<&String>, UnresolvedError> {
self.license.as_ref().map(|v| v.resolved()).transpose()
pub fn normalized_license(&self) -> Result<Option<&String>, UnresolvedError> {
self.license.as_ref().map(|v| v.normalized()).transpose()
}

pub fn resolved_license_file(&self) -> Result<Option<&String>, UnresolvedError> {
self.license_file.as_ref().map(|v| v.resolved()).transpose()
pub fn normalized_license_file(&self) -> Result<Option<&String>, UnresolvedError> {
self.license_file
.as_ref()
.map(|v| v.normalized())
.transpose()
}

pub fn resolved_repository(&self) -> Result<Option<&String>, UnresolvedError> {
self.repository.as_ref().map(|v| v.resolved()).transpose()
pub fn normalized_repository(&self) -> Result<Option<&String>, UnresolvedError> {
self.repository.as_ref().map(|v| v.normalized()).transpose()
}
}

Expand All @@ -330,7 +339,7 @@ pub enum InheritableField<T> {
}

impl<T> InheritableField<T> {
pub fn resolved(&self) -> Result<&T, UnresolvedError> {
pub fn normalized(&self) -> Result<&T, UnresolvedError> {
self.as_value().ok_or(UnresolvedError)
}

Expand Down Expand Up @@ -634,7 +643,7 @@ impl InheritableDependency {
}
}

pub fn resolved(&self) -> Result<&TomlDependency, UnresolvedError> {
pub fn normalized(&self) -> Result<&TomlDependency, UnresolvedError> {
match self {
InheritableDependency::Value(d) => Ok(d),
InheritableDependency::Inherit(_) => Err(UnresolvedError),
Expand Down Expand Up @@ -1440,7 +1449,7 @@ pub struct InheritableLints {
}

impl InheritableLints {
pub fn resolved(&self) -> Result<&TomlLints, UnresolvedError> {
pub fn normalized(&self) -> Result<&TomlLints, UnresolvedError> {
if self.workspace {
Err(UnresolvedError)
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/fingerprint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1426,7 +1426,7 @@ fn calculate_normal(
// HACK(#13975): duplicating the lookup logic here until `--check-cfg` is supported
// on Cargo's MSRV and we can centralize the logic in `lints_to_rustflags`
let mut lint_check_cfg = Vec::new();
if let Ok(Some(lints)) = unit.pkg.manifest().resolved_toml().resolved_lints() {
if let Ok(Some(lints)) = unit.pkg.manifest().normalized_toml().normalized_lints() {
if let Some(rust_lints) = lints.get("rust") {
if let Some(unexpected_cfgs) = rust_lints.get("unexpected_cfgs") {
if let Some(config) = unexpected_cfgs.config() {
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1355,7 +1355,7 @@ fn check_cfg_args(unit: &Unit) -> CargoResult<Vec<OsString>> {
];

// Also include the custom arguments specified in `[lints.rust.unexpected_cfgs.check_cfg]`
if let Ok(Some(lints)) = unit.pkg.manifest().resolved_toml().resolved_lints() {
if let Ok(Some(lints)) = unit.pkg.manifest().normalized_toml().normalized_lints() {
if let Some(rust_lints) = lints.get("rust") {
if let Some(unexpected_cfgs) = rust_lints.get("unexpected_cfgs") {
if let Some(config) = unexpected_cfgs.config() {
Expand Down
30 changes: 15 additions & 15 deletions src/cargo/core/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub struct Manifest {
contents: Rc<String>,
document: Rc<toml_edit::ImDocument<String>>,
original_toml: Rc<TomlManifest>,
resolved_toml: Rc<TomlManifest>,
normalized_toml: Rc<TomlManifest>,
summary: Summary,

// this form of manifest:
Expand Down Expand Up @@ -110,7 +110,7 @@ pub struct VirtualManifest {
contents: Rc<String>,
document: Rc<toml_edit::ImDocument<String>>,
original_toml: Rc<TomlManifest>,
resolved_toml: Rc<TomlManifest>,
normalized_toml: Rc<TomlManifest>,

// this form of manifest:
replace: Vec<(PackageIdSpec, Dependency)>,
Expand Down Expand Up @@ -422,7 +422,7 @@ impl Manifest {
contents: Rc<String>,
document: Rc<toml_edit::ImDocument<String>>,
original_toml: Rc<TomlManifest>,
resolved_toml: Rc<TomlManifest>,
normalized_toml: Rc<TomlManifest>,
summary: Summary,

default_kind: Option<CompileKind>,
Expand Down Expand Up @@ -451,7 +451,7 @@ impl Manifest {
contents,
document,
original_toml,
resolved_toml,
normalized_toml,
summary,

default_kind,
Expand Down Expand Up @@ -483,9 +483,9 @@ impl Manifest {
pub fn contents(&self) -> &str {
self.contents.as_str()
}
/// See [`Manifest::resolved_toml`] for what "resolved" means
pub fn to_resolved_contents(&self) -> CargoResult<String> {
let toml = toml::to_string_pretty(self.resolved_toml())?;
/// See [`Manifest::normalized_toml`] for what "normalized" means
pub fn to_normalized_contents(&self) -> CargoResult<String> {
let toml = toml::to_string_pretty(self.normalized_toml())?;
Ok(format!("{}\n{}", MANIFEST_PREAMBLE, toml))
}
/// Collection of spans for the original TOML
Expand All @@ -502,8 +502,8 @@ impl Manifest {
/// useful for the operation of cargo, including
/// - workspace inheritance
/// - target discovery
pub fn resolved_toml(&self) -> &TomlManifest {
&self.resolved_toml
pub fn normalized_toml(&self) -> &TomlManifest {
&self.normalized_toml
}
pub fn summary(&self) -> &Summary {
&self.summary
Expand Down Expand Up @@ -553,7 +553,7 @@ impl Manifest {
&self.warnings
}
pub fn profiles(&self) -> Option<&TomlProfiles> {
self.resolved_toml.profile.as_ref()
self.normalized_toml.profile.as_ref()
}
pub fn publish(&self) -> &Option<Vec<String>> {
&self.publish
Expand Down Expand Up @@ -664,7 +664,7 @@ impl VirtualManifest {
contents: Rc<String>,
document: Rc<toml_edit::ImDocument<String>>,
original_toml: Rc<TomlManifest>,
resolved_toml: Rc<TomlManifest>,
normalized_toml: Rc<TomlManifest>,
replace: Vec<(PackageIdSpec, Dependency)>,
patch: HashMap<Url, Vec<Dependency>>,
workspace: WorkspaceConfig,
Expand All @@ -675,7 +675,7 @@ impl VirtualManifest {
contents,
document,
original_toml,
resolved_toml,
normalized_toml,
replace,
patch,
workspace,
Expand All @@ -698,8 +698,8 @@ impl VirtualManifest {
&self.original_toml
}
/// The [`TomlManifest`] with all fields expanded
pub fn resolved_toml(&self) -> &TomlManifest {
&self.resolved_toml
pub fn normalized_toml(&self) -> &TomlManifest {
&self.normalized_toml
}

pub fn replace(&self) -> &[(PackageIdSpec, Dependency)] {
Expand All @@ -715,7 +715,7 @@ impl VirtualManifest {
}

pub fn profiles(&self) -> Option<&TomlProfiles> {
self.resolved_toml.profile.as_ref()
self.normalized_toml.profile.as_ref()
}

pub fn warnings_mut(&mut self) -> &mut Warnings {
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/core/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,7 @@ impl<'gctx> Workspace<'gctx> {
);
self.gctx.shell().warn(&msg)
};
if manifest.resolved_toml().has_profiles() {
if manifest.normalized_toml().has_profiles() {
emit_warning("profiles")?;
}
if !manifest.replace().is_empty() {
Expand Down Expand Up @@ -1191,7 +1191,7 @@ impl<'gctx> Workspace<'gctx> {
let mut error_count = 0;
let toml_lints = pkg
.manifest()
.resolved_toml()
.normalized_toml()
.lints
.clone()
.map(|lints| lints.lints)
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ fn tar(
}
FileContents::Generated(generated_kind) => {
let contents = match generated_kind {
GeneratedFile::Manifest => publish_pkg.manifest().to_resolved_contents()?,
GeneratedFile::Manifest => publish_pkg.manifest().to_normalized_contents()?,
GeneratedFile::Lockfile => build_lock(ws, &publish_pkg, local_reg)?,
GeneratedFile::VcsInfo(ref s) => serde_json::to_string_pretty(s)?,
};
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/ops/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ fn add_feature_for_unused_deps(
let manifest = pkg.manifest();

let activated_opt_deps = manifest
.resolved_toml()
.normalized_toml()
.features()
.map(|map| {
map.values()
Expand Down Expand Up @@ -479,7 +479,7 @@ fn add_feature_for_unused_deps(
// only way to guarantee an optional dependency is available for use.
//
// The way we avoid implicitly creating features in Edition2024 is we remove the
// dependency from `resolved_toml` if there is no `dep:` syntax as that is the only
// dependency from `normalized_toml` if there is no `dep:` syntax as that is the only
// syntax that suppresses the creation of the implicit feature.
for (feature_name, activations) in features.iter_mut() {
let Some(activations) = activations.as_array_mut() else {
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/registry/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ pub(crate) fn prepare_transmit(
}
}

let string_features = match manifest.resolved_toml().features() {
let string_features = match manifest.normalized_toml().features() {
Some(features) => features
.iter()
.map(|(feat, values)| {
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ fn cp_sources(
let cksum = if dst.file_name() == Some(OsStr::new("Cargo.toml"))
&& pkg.package_id().source_id().is_git()
{
let contents = pkg.manifest().to_resolved_contents()?;
let contents = pkg.manifest().to_normalized_contents()?;
copy_and_checksum(
&dst,
&mut dst_opts,
Expand Down
10 changes: 5 additions & 5 deletions src/cargo/sources/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -880,14 +880,14 @@ fn read_packages(

fn nested_paths(manifest: &Manifest) -> Vec<PathBuf> {
let mut nested_paths = Vec::new();
let resolved = manifest.resolved_toml();
let dependencies = resolved
let normalized = manifest.normalized_toml();
let dependencies = normalized
.dependencies
.iter()
.chain(resolved.build_dependencies())
.chain(resolved.dev_dependencies())
.chain(normalized.build_dependencies())
.chain(normalized.dev_dependencies())
.chain(
resolved
normalized
.target
.as_ref()
.into_iter()
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/util/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ pub fn check_im_a_teapot(
}

if manifest
.resolved_toml()
.normalized_toml()
.package()
.is_some_and(|p| p.im_a_teapot.is_some())
{
Expand Down Expand Up @@ -560,7 +560,7 @@ pub fn check_implicit_features(
}

let activated_opt_deps = manifest
.resolved_toml()
.normalized_toml()
.features()
.map(|map| {
map.values()
Expand Down
Loading

0 comments on commit 011fb34

Please sign in to comment.