From e46df982410d2752cc0b4b0b878c25d8eadd08b3 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 1 Dec 2023 13:31:30 -0600 Subject: [PATCH] refactor(schemas): Pull out mod for proposed schemas package Originally for #12801 we talked about a `cargo-util-manifest-schema` package - `util` in the name to not clash with plugins - manifest specific to keep the scope down The problem is we have types that aren't manifest specific, like - `PartialVersion` (currently slated for `cargo-util-semverext`) - `RustVersion` - `PackageIdSpec` - `SourceKind` (soon) Things get messy if we try to break things down into common packages. Instead, I think it'd be useful to have a schemas package that has mods for each type of schema, re-exporting what is needed. Normally, componentizing your package by the layer in the stack is a recipe for pain. I don't think that'll apply here because these are meant to be so low level. The other big concern could be compile times. My hope is it won't be too bad. So this moves the `util/toml` types into the module and we can add more in the future. --- src/bin/cargo/main.rs | 2 +- src/cargo/core/compiler/mod.rs | 4 +- src/cargo/core/manifest.rs | 2 +- src/cargo/core/profiles.rs | 10 +- src/cargo/core/workspace.rs | 5 +- src/cargo/lib.rs | 3 +- src/cargo/util/command_prelude.rs | 2 +- src/cargo/util/toml/mod.rs | 215 +++++++++--------- src/cargo/util/toml/targets.rs | 8 +- .../schema.rs => util_schemas/manifest.rs} | 0 src/cargo/util_schemas/mod.rs | 8 + tests/testsuite/config.rs | 6 +- tests/testsuite/profile_config.rs | 2 +- 13 files changed, 138 insertions(+), 129 deletions(-) rename src/cargo/{util/toml/schema.rs => util_schemas/manifest.rs} (100%) create mode 100644 src/cargo/util_schemas/mod.rs diff --git a/src/bin/cargo/main.rs b/src/bin/cargo/main.rs index a20ee9448e1..14a4206d659 100644 --- a/src/bin/cargo/main.rs +++ b/src/bin/cargo/main.rs @@ -2,9 +2,9 @@ use cargo::util::network::http::http_handle; use cargo::util::network::http::needs_custom_http_transport; -use cargo::util::toml::schema::StringOrVec; use cargo::util::CliError; use cargo::util::{self, closest_msg, command_prelude, CargoResult, CliResult, Config}; +use cargo::util_schemas::manifest::StringOrVec; use cargo_util::{ProcessBuilder, ProcessError}; use std::collections::BTreeMap; use std::env; diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs index 6e97382fd65..14aa9814831 100644 --- a/src/cargo/core/compiler/mod.rs +++ b/src/cargo/core/compiler/mod.rs @@ -93,9 +93,9 @@ use crate::core::{Feature, PackageId, Target, Verbosity}; use crate::util::errors::{CargoResult, VerboseError}; use crate::util::interning::InternedString; use crate::util::machine_message::{self, Message}; -use crate::util::toml::schema::TomlDebugInfo; -use crate::util::toml::schema::TomlTrimPaths; use crate::util::{add_path_args, internal, iter_join_onto, profile}; +use crate::util_schemas::manifest::TomlDebugInfo; +use crate::util_schemas::manifest::TomlTrimPaths; use cargo_util::{paths, ProcessBuilder, ProcessError}; use rustfix::diagnostics::Applicability; diff --git a/src/cargo/core/manifest.rs b/src/cargo/core/manifest.rs index 66af40c10a3..54c724c23de 100644 --- a/src/cargo/core/manifest.rs +++ b/src/cargo/core/manifest.rs @@ -18,8 +18,8 @@ use crate::core::{Dependency, PackageId, PackageIdSpec, SourceId, Summary}; use crate::core::{Edition, Feature, Features, WorkspaceConfig}; use crate::util::errors::*; use crate::util::interning::InternedString; -use crate::util::toml::schema::{TomlManifest, TomlProfiles}; use crate::util::{short_hash, Config, Filesystem, RustVersion}; +use crate::util_schemas::manifest::{TomlManifest, TomlProfiles}; pub enum EitherManifest { Real(Manifest), diff --git a/src/cargo/core/profiles.rs b/src/cargo/core/profiles.rs index f0ecb663ec6..34365008ee5 100644 --- a/src/cargo/core/profiles.rs +++ b/src/cargo/core/profiles.rs @@ -27,13 +27,13 @@ use crate::core::resolver::features::FeaturesFor; use crate::core::Feature; use crate::core::{PackageId, PackageIdSpec, Resolve, Shell, Target, Workspace}; use crate::util::interning::InternedString; -use crate::util::toml::schema::TomlTrimPaths; -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 crate::util_schemas::manifest::TomlTrimPaths; +use crate::util_schemas::manifest::TomlTrimPathsValue; +use crate::util_schemas::manifest::{ + ProfilePackageSpec, StringOrBool, TomlDebugInfo, TomlProfile, TomlProfiles, +}; use anyhow::{bail, Context as _}; use std::collections::{BTreeMap, HashMap, HashSet}; use std::hash::Hash; diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index 21740cce862..3467fe18ee8 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -22,11 +22,10 @@ use crate::sources::{PathSource, CRATES_IO_INDEX, CRATES_IO_REGISTRY}; use crate::util::edit_distance; use crate::util::errors::{CargoResult, ManifestError}; use crate::util::interning::InternedString; -use crate::util::toml::{ - read_manifest, schema::TomlDependency, schema::TomlProfiles, InheritableFields, -}; +use crate::util::toml::{read_manifest, InheritableFields}; use crate::util::RustVersion; use crate::util::{config::ConfigRelativePath, Config, Filesystem, IntoUrl}; +use crate::util_schemas::manifest::{TomlDependency, TomlProfiles}; use cargo_util::paths; use cargo_util::paths::normalize_path; use pathdiff::diff_paths; diff --git a/src/cargo/lib.rs b/src/cargo/lib.rs index b4114017257..6d7468ca3b0 100644 --- a/src/cargo/lib.rs +++ b/src/cargo/lib.rs @@ -98,7 +98,7 @@ //! Files that interact with cargo include //! //! - Package -//! - `Cargo.toml`: User-written project manifest, loaded with [`util::toml::schema::TomlManifest`] and then +//! - `Cargo.toml`: User-written project manifest, loaded with [`util_schemas::manifest::TomlManifest`] and then //! translated to [`core::manifest::Manifest`] which maybe stored in a [`core::Package`]. //! - This is editable with [`util::toml_mut::manifest::LocalManifest`] //! - `Cargo.lock`: Generally loaded with [`ops::resolve_ws`] or a variant of it into a [`core::resolver::Resolve`] @@ -152,6 +152,7 @@ pub mod core; pub mod ops; pub mod sources; pub mod util; +pub mod util_schemas; pub mod util_semver; mod version; diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index 373995a9dfa..3e236a6f735 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -7,11 +7,11 @@ use crate::util::important_paths::find_root_manifest_for_wd; use crate::util::interning::InternedString; use crate::util::is_rustup; use crate::util::restricted_names; -use crate::util::toml::schema::StringOrVec; use crate::util::{ print_available_benches, print_available_binaries, print_available_examples, print_available_packages, print_available_tests, }; +use crate::util_schemas::manifest::StringOrVec; use crate::CargoResult; use anyhow::bail; use cargo_util::paths; diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index f15838adae5..d8405ec3bc8 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -28,11 +28,10 @@ use crate::util::{ self, config::ConfigRelativePath, validate_package_name, Config, IntoUrl, OptVersionReq, RustVersion, }; +use crate::util_schemas::manifest; mod embedded; -pub mod schema; mod targets; -use self::schema::TomlDependency; use self::targets::targets; /// Loads a `Cargo.toml` from a file on disk. @@ -98,7 +97,7 @@ fn read_manifest_from_str( let mut unused = BTreeSet::new(); let deserializer = toml::de::Deserializer::new(contents); - let manifest: schema::TomlManifest = serde_ignored::deserialize(deserializer, |path| { + let manifest: manifest::TomlManifest = serde_ignored::deserialize(deserializer, |path| { let mut key = String::new(); stringify(&mut key, &path); unused.insert(key); @@ -183,10 +182,10 @@ fn warn_on_deprecated(new_path: &str, name: &str, kind: &str, warnings: &mut Vec // - Path and git components of dependency specifications are removed. // - License path is updated to point within the package. pub fn prepare_for_publish( - me: &schema::TomlManifest, + me: &manifest::TomlManifest, ws: &Workspace<'_>, package_root: &Path, -) -> CargoResult { +) -> CargoResult { let config = ws.config(); let mut package = me.package().unwrap().clone(); package.workspace = None; @@ -219,7 +218,7 @@ pub fn prepare_for_publish( if abs_license_path.strip_prefix(package_root).is_err() { // This path points outside of the package root. `cargo package` // will copy it into the root, so adjust the path to this location. - package.license_file = Some(schema::InheritableField::Value( + package.license_file = Some(manifest::InheritableField::Value( license_path .file_name() .unwrap() @@ -235,14 +234,14 @@ pub fn prepare_for_publish( .as_value() .context("readme should have been resolved before `prepare_for_publish()`")?; match readme { - schema::StringOrBool::String(readme) => { + manifest::StringOrBool::String(readme) => { let readme_path = Path::new(&readme); let abs_readme_path = paths::normalize_path(&package_root.join(readme_path)); if abs_readme_path.strip_prefix(package_root).is_err() { // This path points outside of the package root. `cargo package` // will copy it into the root, so adjust the path to this location. - package.readme = Some(schema::InheritableField::Value( - schema::StringOrBool::String( + package.readme = Some(manifest::InheritableField::Value( + manifest::StringOrBool::String( readme_path .file_name() .unwrap() @@ -253,11 +252,11 @@ pub fn prepare_for_publish( )); } } - schema::StringOrBool::Bool(_) => {} + manifest::StringOrBool::Bool(_) => {} } } - let all = |_d: &schema::TomlDependency| true; - return Ok(schema::TomlManifest { + let all = |_d: &manifest::TomlDependency| true; + return Ok(manifest::TomlManifest { package: Some(package), project: None, profile: me.profile.clone(), @@ -270,7 +269,7 @@ pub fn prepare_for_publish( dev_dependencies: map_deps( config, me.dev_dependencies(), - schema::TomlDependency::is_version_specified, + manifest::TomlDependency::is_version_specified, )?, dev_dependencies2: None, build_dependencies: map_deps(config, me.build_dependencies(), all)?, @@ -282,12 +281,12 @@ pub fn prepare_for_publish( .map(|(k, v)| { Ok(( k.clone(), - schema::TomlPlatform { + manifest::TomlPlatform { dependencies: map_deps(config, v.dependencies.as_ref(), all)?, dev_dependencies: map_deps( config, v.dev_dependencies(), - schema::TomlDependency::is_version_specified, + manifest::TomlDependency::is_version_specified, )?, dev_dependencies2: None, build_dependencies: map_deps(config, v.build_dependencies(), all)?, @@ -311,14 +310,14 @@ pub fn prepare_for_publish( fn map_deps( config: &Config, - deps: Option<&BTreeMap>, - filter: impl Fn(&schema::TomlDependency) -> bool, - ) -> CargoResult>> { + deps: Option<&BTreeMap>, + filter: impl Fn(&manifest::TomlDependency) -> bool, + ) -> CargoResult>> { let Some(deps) = deps else { return Ok(None) }; let deps = deps .iter() .filter(|(_k, v)| { - if let schema::InheritableDependency::Value(def) = v { + if let manifest::InheritableDependency::Value(def) = v { filter(def) } else { false @@ -331,10 +330,10 @@ pub fn prepare_for_publish( fn map_dependency( config: &Config, - dep: &schema::InheritableDependency, - ) -> CargoResult { + dep: &manifest::InheritableDependency, + ) -> CargoResult { let dep = match dep { - schema::InheritableDependency::Value(schema::TomlDependency::Detailed(d)) => { + manifest::InheritableDependency::Value(manifest::TomlDependency::Detailed(d)) => { let mut d = d.clone(); // Path dependencies become crates.io deps. d.path.take(); @@ -349,21 +348,21 @@ pub fn prepare_for_publish( } Ok(d) } - schema::InheritableDependency::Value(schema::TomlDependency::Simple(s)) => { - Ok(schema::TomlDetailedDependency { + manifest::InheritableDependency::Value(manifest::TomlDependency::Simple(s)) => { + Ok(manifest::TomlDetailedDependency { version: Some(s.clone()), ..Default::default() }) } _ => unreachable!(), }; - dep.map(schema::TomlDependency::Detailed) - .map(schema::InheritableDependency::Value) + dep.map(manifest::TomlDependency::Detailed) + .map(manifest::InheritableDependency::Value) } } pub fn to_real_manifest( - me: schema::TomlManifest, + me: manifest::TomlManifest, embedded: bool, source_id: SourceId, package_root: &Path, @@ -499,7 +498,7 @@ pub fn to_real_manifest( .map(|version| field_inherit_with(version, "version", || inherit()?.version())) .transpose()?; - package.version = version.clone().map(schema::InheritableField::Value); + package.version = version.clone().map(manifest::InheritableField::Value); let pkgid = PackageId::pure( package.name.as_str().into(), @@ -513,7 +512,7 @@ pub fn to_real_manifest( let edition: Edition = field_inherit_with(edition, "edition", || inherit()?.edition())? .parse() .with_context(|| "failed to parse the `edition` key")?; - package.edition = Some(schema::InheritableField::Value(edition.to_string())); + package.edition = Some(manifest::InheritableField::Value(edition.to_string())); edition } else { Edition::Edition2015 @@ -633,11 +632,11 @@ pub fn to_real_manifest( fn process_dependencies( cx: &mut Context<'_, '_>, - new_deps: Option<&BTreeMap>, + new_deps: Option<&BTreeMap>, kind: Option, workspace_config: &WorkspaceConfig, inherit_cell: &LazyCell, - ) -> CargoResult>> { + ) -> CargoResult>> { let Some(dependencies) = new_deps else { return Ok(None); }; @@ -648,7 +647,7 @@ pub fn to_real_manifest( }) }; - let mut deps: BTreeMap = BTreeMap::new(); + let mut deps: BTreeMap = BTreeMap::new(); for (n, v) in dependencies.iter() { let resolved = dependency_inherit_with(v.clone(), n, inheritable, cx)?; let dep = dep_to_dependency(&resolved, n, cx, kind)?; @@ -667,7 +666,7 @@ pub fn to_real_manifest( cx.deps.push(dep); deps.insert( n.to_string(), - schema::InheritableDependency::Value(resolved.clone()), + manifest::InheritableDependency::Value(resolved.clone()), ); } Ok(Some(deps)) @@ -710,10 +709,10 @@ pub fn to_real_manifest( .map(|mw| lints_inherit_with(mw, || inherit()?.lints())) .transpose()?; let lints = verify_lints(lints)?; - let default = schema::TomlLints::default(); + let default = manifest::TomlLints::default(); let rustflags = lints_to_rustflags(lints.as_ref().unwrap_or(&default)); - let mut target: BTreeMap = BTreeMap::new(); + let mut target: BTreeMap = BTreeMap::new(); for (name, platform) in me.target.iter().flatten() { cx.platform = { let platform: Platform = name.parse()?; @@ -751,7 +750,7 @@ pub fn to_real_manifest( )?; target.insert( name.clone(), - schema::TomlPlatform { + manifest::TomlPlatform { dependencies: deps, build_dependencies: build_deps, build_dependencies2: None, @@ -890,54 +889,54 @@ pub fn to_real_manifest( package.description = metadata .description .clone() - .map(|description| schema::InheritableField::Value(description)); + .map(|description| manifest::InheritableField::Value(description)); package.homepage = metadata .homepage .clone() - .map(|homepage| schema::InheritableField::Value(homepage)); + .map(|homepage| manifest::InheritableField::Value(homepage)); package.documentation = metadata .documentation .clone() - .map(|documentation| schema::InheritableField::Value(documentation)); + .map(|documentation| manifest::InheritableField::Value(documentation)); package.readme = metadata .readme .clone() - .map(|readme| schema::InheritableField::Value(schema::StringOrBool::String(readme))); + .map(|readme| manifest::InheritableField::Value(manifest::StringOrBool::String(readme))); package.authors = package .authors .as_ref() - .map(|_| schema::InheritableField::Value(metadata.authors.clone())); + .map(|_| manifest::InheritableField::Value(metadata.authors.clone())); package.license = metadata .license .clone() - .map(|license| schema::InheritableField::Value(license)); + .map(|license| manifest::InheritableField::Value(license)); package.license_file = metadata .license_file .clone() - .map(|license_file| schema::InheritableField::Value(license_file)); + .map(|license_file| manifest::InheritableField::Value(license_file)); package.repository = metadata .repository .clone() - .map(|repository| schema::InheritableField::Value(repository)); + .map(|repository| manifest::InheritableField::Value(repository)); package.keywords = package .keywords .as_ref() - .map(|_| schema::InheritableField::Value(metadata.keywords.clone())); + .map(|_| manifest::InheritableField::Value(metadata.keywords.clone())); package.categories = package .categories .as_ref() - .map(|_| schema::InheritableField::Value(metadata.categories.clone())); + .map(|_| manifest::InheritableField::Value(metadata.categories.clone())); package.rust_version = rust_version .clone() - .map(|rv| schema::InheritableField::Value(rv)); + .map(|rv| manifest::InheritableField::Value(rv)); package.exclude = package .exclude .as_ref() - .map(|_| schema::InheritableField::Value(exclude.clone())); + .map(|_| manifest::InheritableField::Value(exclude.clone())); package.include = package .include .as_ref() - .map(|_| schema::InheritableField::Value(include.clone())); + .map(|_| manifest::InheritableField::Value(include.clone())); let profiles = me.profile.clone(); if let Some(profiles) = &profiles { @@ -950,12 +949,14 @@ pub fn to_real_manifest( .clone() .map(|publish| field_inherit_with(publish, "publish", || inherit()?.publish()).unwrap()); - package.publish = publish.clone().map(|p| schema::InheritableField::Value(p)); + package.publish = publish + .clone() + .map(|p| manifest::InheritableField::Value(p)); let publish = match publish { - Some(schema::VecStringOrBool::VecString(ref vecstring)) => Some(vecstring.clone()), - Some(schema::VecStringOrBool::Bool(false)) => Some(vec![]), - Some(schema::VecStringOrBool::Bool(true)) => None, + Some(manifest::VecStringOrBool::VecString(ref vecstring)) => Some(vecstring.clone()), + Some(manifest::VecStringOrBool::Bool(false)) => Some(vec![]), + Some(manifest::VecStringOrBool::Bool(true)) => None, None => version.is_none().then_some(vec![]), }; @@ -996,7 +997,7 @@ pub fn to_real_manifest( .transpose()? .map(CompileKind::Target); let custom_metadata = package.metadata.clone(); - let resolved_toml = schema::TomlManifest { + let resolved_toml = manifest::TomlManifest { cargo_features: me.cargo_features.clone(), package: Some(package.clone()), project: None, @@ -1019,8 +1020,8 @@ pub fn to_real_manifest( badges: me .badges .as_ref() - .map(|_| schema::InheritableField::Value(metadata.badges.clone())), - lints: lints.map(|lints| schema::InheritableLints { + .map(|_| manifest::InheritableField::Value(metadata.badges.clone())), + lints: lints.map(|lints| manifest::InheritableLints { workspace: false, lints, }), @@ -1075,7 +1076,7 @@ pub fn to_real_manifest( } fn to_virtual_manifest( - me: schema::TomlManifest, + me: manifest::TomlManifest, source_id: SourceId, root: &Path, config: &Config, @@ -1194,7 +1195,7 @@ fn to_virtual_manifest( } fn replace( - me: &schema::TomlManifest, + me: &manifest::TomlManifest, cx: &mut Context<'_, '_>, ) -> CargoResult> { if me.patch.is_some() && me.replace.is_some() { @@ -1242,7 +1243,7 @@ fn replace( } fn patch( - me: &schema::TomlManifest, + me: &manifest::TomlManifest, cx: &mut Context<'_, '_>, ) -> CargoResult>> { let mut patch = HashMap::new(); @@ -1289,7 +1290,7 @@ struct Context<'a, 'b> { features: &'a Features, } -fn verify_lints(lints: Option) -> CargoResult> { +fn verify_lints(lints: Option) -> CargoResult> { let Some(lints) = lints else { return Ok(None); }; @@ -1320,16 +1321,16 @@ fn verify_lints(lints: Option) -> CargoResult Vec { +fn lints_to_rustflags(lints: &manifest::TomlLints) -> Vec { let mut rustflags = lints .iter() .flat_map(|(tool, lints)| { lints.iter().map(move |(name, config)| { let flag = match config.level() { - schema::TomlLintLevel::Forbid => "--forbid", - schema::TomlLintLevel::Deny => "--deny", - schema::TomlLintLevel::Warn => "--warn", - schema::TomlLintLevel::Allow => "--allow", + manifest::TomlLintLevel::Forbid => "--forbid", + manifest::TomlLintLevel::Deny => "--deny", + manifest::TomlLintLevel::Warn => "--warn", + manifest::TomlLintLevel::Allow => "--allow", }; let option = if tool == "rust" { @@ -1393,17 +1394,17 @@ fn inheritable_from_path( } } -/// Returns the name of the README file for a [`schema::TomlPackage`]. +/// Returns the name of the README file for a [`manifest::TomlPackage`]. fn readme_for_package( package_root: &Path, - readme: Option<&schema::StringOrBool>, + readme: Option<&manifest::StringOrBool>, ) -> Option { match &readme { None => default_readme_from_package_root(package_root), Some(value) => match value { - schema::StringOrBool::Bool(false) => None, - schema::StringOrBool::Bool(true) => Some("README.md".to_string()), - schema::StringOrBool::String(v) => Some(v.clone()), + manifest::StringOrBool::Bool(false) => None, + manifest::StringOrBool::Bool(true) => Some("README.md".to_string()), + manifest::StringOrBool::String(v) => Some(v.clone()), }, } } @@ -1466,9 +1467,9 @@ macro_rules! package_field_getter { /// A group of fields that are inheritable by members of the workspace #[derive(Clone, Debug, Default)] pub struct InheritableFields { - package: Option, - dependencies: Option>, - lints: Option, + package: Option, + dependencies: Option>, + lints: Option, // Bookkeeping to help when resolving values from above _ws_root: PathBuf, @@ -1488,7 +1489,7 @@ impl InheritableFields { ("include", include -> Vec), ("keywords", keywords -> Vec), ("license", license -> String), - ("publish", publish -> schema::VecStringOrBool), + ("publish", publish -> manifest::VecStringOrBool), ("repository", repository -> String), ("rust-version", rust_version -> RustVersion), ("version", version -> semver::Version), @@ -1499,7 +1500,7 @@ impl InheritableFields { &self, name: &str, package_root: &Path, - ) -> CargoResult { + ) -> CargoResult { let Some(deps) = &self.dependencies else { bail!("`workspace.dependencies` was not defined"); }; @@ -1507,7 +1508,7 @@ impl InheritableFields { bail!("`dependency.{name}` was not found in `workspace.dependencies`"); }; let mut dep = dep.clone(); - if let schema::TomlDependency::Detailed(detailed) = &mut dep { + if let manifest::TomlDependency::Detailed(detailed) = &mut dep { if let Some(rel_path) = &detailed.path { detailed.path = Some(resolve_relative_path( name, @@ -1521,7 +1522,7 @@ impl InheritableFields { } /// Gets the field `workspace.lint`. - fn lints(&self) -> CargoResult { + fn lints(&self) -> CargoResult { let Some(val) = &self.lints else { bail!("`workspace.lints` was not defined"); }; @@ -1537,7 +1538,7 @@ impl InheritableFields { } /// Gets the field `workspace.package.readme`. - fn readme(&self, package_root: &Path) -> CargoResult { + fn readme(&self, package_root: &Path) -> CargoResult { let Some(readme) = readme_for_package( self._ws_root.as_path(), self.package.as_ref().and_then(|p| p.readme.as_ref()), @@ -1545,7 +1546,7 @@ impl InheritableFields { bail!("`workspace.package.readme` was not defined"); }; resolve_relative_path("readme", &self._ws_root, package_root, &readme) - .map(schema::StringOrBool::String) + .map(manifest::StringOrBool::String) } fn ws_root(&self) -> &PathBuf { @@ -1554,13 +1555,13 @@ impl InheritableFields { } fn field_inherit_with<'a, T>( - field: schema::InheritableField, + field: manifest::InheritableField, label: &str, get_ws_inheritable: impl FnOnce() -> CargoResult, ) -> CargoResult { match field { - schema::InheritableField::Value(value) => Ok(value), - schema::InheritableField::Inherit(_) => get_ws_inheritable().with_context(|| { + manifest::InheritableField::Value(value) => Ok(value), + manifest::InheritableField::Inherit(_) => get_ws_inheritable().with_context(|| { format!( "error inheriting `{label}` from workspace root manifest's `workspace.package.{label}`", ) @@ -1569,9 +1570,9 @@ fn field_inherit_with<'a, T>( } fn lints_inherit_with( - lints: schema::InheritableLints, - get_ws_inheritable: impl FnOnce() -> CargoResult, -) -> CargoResult { + lints: manifest::InheritableLints, + get_ws_inheritable: impl FnOnce() -> CargoResult, +) -> CargoResult { if lints.workspace { if !lints.lints.is_empty() { anyhow::bail!("cannot override `workspace.lints` in `lints`, either remove the overrides or `lints.workspace = true` and manually specify the lints"); @@ -1585,14 +1586,14 @@ fn lints_inherit_with( } fn dependency_inherit_with<'a>( - dependency: schema::InheritableDependency, + dependency: manifest::InheritableDependency, name: &str, inheritable: impl FnOnce() -> CargoResult<&'a InheritableFields>, cx: &mut Context<'_, '_>, -) -> CargoResult { +) -> CargoResult { match dependency { - schema::InheritableDependency::Value(value) => Ok(value), - schema::InheritableDependency::Inherit(w) => { + manifest::InheritableDependency::Value(value) => Ok(value), + manifest::InheritableDependency::Inherit(w) => { inner_dependency_inherit_with(w, name, inheritable, cx).with_context(|| { format!( "error inheriting `{name}` from workspace root manifest's `workspace.dependencies.{name}`", @@ -1603,11 +1604,11 @@ fn dependency_inherit_with<'a>( } fn inner_dependency_inherit_with<'a>( - dependency: schema::TomlInheritedDependency, + dependency: manifest::TomlInheritedDependency, name: &str, inheritable: impl FnOnce() -> CargoResult<&'a InheritableFields>, cx: &mut Context<'_, '_>, -) -> CargoResult { +) -> CargoResult { fn default_features_msg(label: &str, ws_def_feat: Option, cx: &mut Context<'_, '_>) { let ws_def_feat = match ws_def_feat { Some(true) => "true", @@ -1625,7 +1626,7 @@ fn inner_dependency_inherit_with<'a>( } inheritable()?.get_dependency(name, cx.root).map(|d| { match d { - schema::TomlDependency::Simple(s) => { + manifest::TomlDependency::Simple(s) => { if let Some(false) = dependency.default_features() { default_features_msg(name, None, cx); } @@ -1633,7 +1634,7 @@ fn inner_dependency_inherit_with<'a>( || dependency.features.is_some() || dependency.public.is_some() { - schema::TomlDependency::Detailed(schema::TomlDetailedDependency { + manifest::TomlDependency::Detailed(manifest::TomlDetailedDependency { version: Some(s), optional: dependency.optional, features: dependency.features.clone(), @@ -1641,10 +1642,10 @@ fn inner_dependency_inherit_with<'a>( ..Default::default() }) } else { - schema::TomlDependency::Simple(s) + manifest::TomlDependency::Simple(s) } } - schema::TomlDependency::Detailed(d) => { + manifest::TomlDependency::Detailed(d) => { let mut d = d.clone(); match (dependency.default_features(), d.default_features()) { // member: default-features = true and @@ -1683,14 +1684,14 @@ fn inner_dependency_inherit_with<'a>( (None, None) => None, }; d.optional = dependency.optional; - schema::TomlDependency::Detailed(d) + manifest::TomlDependency::Detailed(d) } } }) } pub(crate) fn to_dependency( - dep: &schema::TomlDependency

, + dep: &manifest::TomlDependency

, name: &str, source_id: SourceId, nested_paths: &mut Vec, @@ -1719,14 +1720,14 @@ pub(crate) fn to_dependency( } fn dep_to_dependency( - orig: &schema::TomlDependency

, + orig: &manifest::TomlDependency

, name: &str, cx: &mut Context<'_, '_>, kind: Option, ) -> CargoResult { match *orig { - schema::TomlDependency::Simple(ref version) => detailed_dep_to_dependency( - &schema::TomlDetailedDependency::

{ + manifest::TomlDependency::Simple(ref version) => detailed_dep_to_dependency( + &manifest::TomlDetailedDependency::

{ version: Some(version.clone()), ..Default::default() }, @@ -1734,14 +1735,14 @@ fn dep_to_dependency( cx, kind, ), - schema::TomlDependency::Detailed(ref details) => { + manifest::TomlDependency::Detailed(ref details) => { detailed_dep_to_dependency(details, name, cx, kind) } } } fn detailed_dep_to_dependency( - orig: &schema::TomlDetailedDependency

, + orig: &manifest::TomlDetailedDependency

, name_in_toml: &str, cx: &mut Context<'_, '_>, kind: Option, @@ -1984,7 +1985,7 @@ fn detailed_dep_to_dependency( /// It's a bit unfortunate both `-Z` flags and `cargo-features` are required, /// because profiles can now be set in either `Cargo.toml` or `config.toml`. fn validate_profiles( - profiles: &schema::TomlProfiles, + profiles: &manifest::TomlProfiles, cli_unstable: &CliUnstable, features: &Features, warnings: &mut Vec, @@ -1997,7 +1998,7 @@ fn validate_profiles( /// Checks stytax validity and unstable feature gate for a given profile. pub fn validate_profile( - root: &schema::TomlProfile, + root: &manifest::TomlProfile, name: &str, cli_unstable: &CliUnstable, features: &Features, @@ -2071,7 +2072,7 @@ pub fn validate_profile( } } - if let Some(schema::StringOrBool::String(arg)) = &root.lto { + if let Some(manifest::StringOrBool::String(arg)) = &root.lto { if arg == "true" || arg == "false" { bail!( "`lto` setting of string `\"{arg}\"` for `{name}` profile is not \ @@ -2088,7 +2089,7 @@ pub fn validate_profile( /// /// This is a shallow check, which is reused for the profile itself and any overrides. fn validate_profile_layer( - profile: &schema::TomlProfile, + profile: &manifest::TomlProfile, name: &str, cli_unstable: &CliUnstable, features: &Features, @@ -2132,7 +2133,7 @@ fn validate_profile_layer( } /// Validation that is specific to an override. -fn validate_profile_override(profile: &schema::TomlProfile, which: &str) -> CargoResult<()> { +fn validate_profile_override(profile: &manifest::TomlProfile, which: &str) -> CargoResult<()> { if profile.package.is_some() { bail!("package-specific profiles cannot be nested"); } diff --git a/src/cargo/util/toml/targets.rs b/src/cargo/util/toml/targets.rs index 4f8383a94c7..3659fd74c37 100644 --- a/src/cargo/util/toml/targets.rs +++ b/src/cargo/util/toml/targets.rs @@ -14,16 +14,16 @@ use std::collections::HashSet; use std::fs::{self, DirEntry}; use std::path::{Path, PathBuf}; -use super::schema::{ - PathValue, StringOrBool, StringOrVec, TomlBenchTarget, TomlBinTarget, TomlExampleTarget, - TomlLibTarget, TomlManifest, TomlTarget, TomlTestTarget, -}; use crate::core::compiler::rustdoc::RustdocScrapeExamples; use crate::core::compiler::CrateType; use crate::core::{Edition, Feature, Features, Target}; use crate::util::errors::CargoResult; use crate::util::restricted_names; use crate::util::toml::warn_on_deprecated; +use crate::util_schemas::manifest::{ + PathValue, StringOrBool, StringOrVec, TomlBenchTarget, TomlBinTarget, TomlExampleTarget, + TomlLibTarget, TomlManifest, TomlTarget, TomlTestTarget, +}; use anyhow::Context as _; diff --git a/src/cargo/util/toml/schema.rs b/src/cargo/util_schemas/manifest.rs similarity index 100% rename from src/cargo/util/toml/schema.rs rename to src/cargo/util_schemas/manifest.rs diff --git a/src/cargo/util_schemas/mod.rs b/src/cargo/util_schemas/mod.rs new file mode 100644 index 00000000000..dd0e15b0af4 --- /dev/null +++ b/src/cargo/util_schemas/mod.rs @@ -0,0 +1,8 @@ +//! Low-level Cargo format schemas +//! +//! This is types with logic mostly focused on `serde` and `FromStr` for use in reading files and +//! parsing command-lines. +//! Any logic for getting final semantics from these will likely need other tools to process, like +//! `cargo metadata`. + +pub mod manifest; diff --git a/tests/testsuite/config.rs b/tests/testsuite/config.rs index e5078bd8ed0..bcd1260205a 100644 --- a/tests/testsuite/config.rs +++ b/tests/testsuite/config.rs @@ -2,9 +2,9 @@ use cargo::core::{PackageIdSpec, Shell}; use cargo::util::config::{self, Config, Definition, JobsConfig, SslVersionConfig, StringList}; -use cargo::util::toml::schema::TomlTrimPaths; -use cargo::util::toml::schema::TomlTrimPathsValue; -use cargo::util::toml::schema::{self as cargo_toml, TomlDebugInfo, VecStringOrBool as VSOB}; +use cargo::util_schemas::manifest::TomlTrimPaths; +use cargo::util_schemas::manifest::TomlTrimPathsValue; +use cargo::util_schemas::manifest::{self as cargo_toml, TomlDebugInfo, VecStringOrBool as VSOB}; use cargo::CargoResult; use cargo_test_support::compare; use cargo_test_support::{panic_error, paths, project, symlink_supported, t}; diff --git a/tests/testsuite/profile_config.rs b/tests/testsuite/profile_config.rs index 710a0d8ef3f..bebac18b1ab 100644 --- a/tests/testsuite/profile_config.rs +++ b/tests/testsuite/profile_config.rs @@ -1,6 +1,6 @@ //! Tests for profiles defined in config files. -use cargo::util::toml::schema::TomlDebugInfo; +use cargo::util_schemas::manifest::TomlDebugInfo; use cargo_test_support::paths::CargoPathExt; use cargo_test_support::registry::Package; use cargo_test_support::{basic_lib_manifest, paths, project};