Skip to content

Commit

Permalink
Auto merge of #13080 - epage:toml, r=weihanglo
Browse files Browse the repository at this point in the history
refactor(toml): Decouple logic from schema

### What does this PR try to resolve?

This tries to decouple cargo logic from the schemas so we can split the schemas out into a separate crate for #12801

Profile layering was one of the few pieces of logic I kept in the schema, assuming its general enough / decoupled enough from cargo policies.

### How should we test and review this PR?

Each step is broken down into its own commit for easier analysis

### Additional information
  • Loading branch information
bors committed Dec 1, 2023
2 parents 78109c0 + 9bb7c97 commit b97cffe
Show file tree
Hide file tree
Showing 7 changed files with 1,580 additions and 1,618 deletions.
4 changes: 2 additions & 2 deletions src/cargo/core/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub const SEE_CHANNELS: &str =
/// - Update [`CLI_VALUES`] to include the new edition.
/// - Set [`LATEST_UNSTABLE`] to Some with the new edition.
/// - Add an unstable feature to the [`features!`] macro invocation below for the new edition.
/// - Gate on that new feature in [`TomlManifest::to_real_manifest`].
/// - Gate on that new feature in [`toml::to_real_manifest`].
/// - Update the shell completion files.
/// - Update any failing tests (hopefully there are very few).
/// - Update unstable.md to add a new section for this new edition (see [this example]).
Expand All @@ -195,7 +195,7 @@ pub const SEE_CHANNELS: &str =
/// [`LATEST_STABLE`]: Edition::LATEST_STABLE
/// [this example]: https://github.com/rust-lang/cargo/blob/3ebb5f15a940810f250b68821149387af583a79e/src/doc/src/reference/unstable.md?plain=1#L1238-L1264
/// [`is_stable`]: Edition::is_stable
/// [`TomlManifest::to_real_manifest`]: crate::util::toml::schema::TomlManifest::to_real_manifest
/// [`toml::to_real_manifest`]: crate::util::toml::to_real_manifest
/// [`features!`]: macro.features.html
#[derive(Clone, Copy, Debug, Hash, PartialOrd, Ord, Eq, PartialEq, Serialize, Deserialize)]
pub enum Edition {
Expand Down
6 changes: 2 additions & 4 deletions src/cargo/core/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use crate::util::network::http::http_handle_and_timeout;
use crate::util::network::http::HttpTimeout;
use crate::util::network::retry::{Retry, RetryResult};
use crate::util::network::sleep::SleepTracker;
use crate::util::toml::prepare_for_publish;
use crate::util::RustVersion;
use crate::util::{self, internal, Config, Progress, ProgressStyle};

Expand Down Expand Up @@ -197,10 +198,7 @@ impl Package {
}

pub fn to_registry_toml(&self, ws: &Workspace<'_>) -> CargoResult<String> {
let manifest = self
.manifest()
.original()
.prepare_for_publish(ws, self.root())?;
let manifest = prepare_for_publish(self.manifest().original(), ws, self.root())?;
let toml = toml::to_string_pretty(&manifest)?;
Ok(format!("{}\n{}", MANIFEST_PREAMBLE, toml))
}
Expand Down
26 changes: 13 additions & 13 deletions src/cargo/core/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use crate::util::toml::schema::TomlTrimPathsValue;
use crate::util::toml::schema::{
ProfilePackageSpec, StringOrBool, TomlDebugInfo, TomlProfile, TomlProfiles,
};
use crate::util::toml::validate_profile;
use crate::util::{closest_msg, config, CargoResult, Config};
use anyhow::{bail, Context as _};
use std::collections::{BTreeMap, HashMap, HashSet};
Expand Down Expand Up @@ -1235,20 +1236,19 @@ fn get_config_profile(ws: &Workspace<'_>, name: &str) -> CargoResult<Option<Toml
return Ok(None);
};
let mut warnings = Vec::new();
profile
.val
.validate(
name,
ws.config().cli_unstable(),
ws.unstable_features(),
&mut warnings,
validate_profile(
&profile.val,
name,
ws.config().cli_unstable(),
ws.unstable_features(),
&mut warnings,
)
.with_context(|| {
format!(
"config profile `{}` is not valid (defined in `{}`)",
name, profile.definition
)
.with_context(|| {
format!(
"config profile `{}` is not valid (defined in `{}`)",
name, profile.definition
)
})?;
})?;
for warning in warnings {
ws.config().shell().warn(warning)?;
}
Expand Down
3 changes: 2 additions & 1 deletion src/cargo/core/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,8 @@ impl<'cfg> Workspace<'cfg> {
url,
deps.iter()
.map(|(name, dep)| {
dep.to_dependency_split(
crate::util::toml::to_dependency(
dep,
name,
source,
&mut nested_paths,
Expand Down
9 changes: 3 additions & 6 deletions src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::sources::PathSource;
use crate::util::cache_lock::CacheLockMode;
use crate::util::config::JobsConfig;
use crate::util::errors::CargoResult;
use crate::util::toml::schema::TomlManifest;
use crate::util::toml::{prepare_for_publish, to_real_manifest};
use crate::util::{self, human_readable_bytes, restricted_names, Config, FileLock};
use crate::{drop_println, ops};
use anyhow::Context as _;
Expand Down Expand Up @@ -454,14 +454,11 @@ fn build_lock(ws: &Workspace<'_>, orig_pkg: &Package) -> CargoResult<String> {
let orig_resolve = ops::load_pkg_lockfile(ws)?;

// Convert Package -> TomlManifest -> Manifest -> Package
let toml_manifest = orig_pkg
.manifest()
.original()
.prepare_for_publish(ws, orig_pkg.root())?;
let toml_manifest = prepare_for_publish(orig_pkg.manifest().original(), ws, orig_pkg.root())?;
let package_root = orig_pkg.root();
let source_id = orig_pkg.package_id().source_id();
let (manifest, _nested_paths) =
TomlManifest::to_real_manifest(toml_manifest, false, source_id, package_root, config)?;
to_real_manifest(toml_manifest, false, source_id, package_root, config)?;
let new_pkg = Package::new(manifest, orig_pkg.manifest_path());

let max_rust_version = new_pkg.rust_version().cloned();
Expand Down
Loading

0 comments on commit b97cffe

Please sign in to comment.