Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(toml): Simplify code to make schema split easier #12948

Merged
merged 9 commits into from
Nov 10, 2023
4 changes: 2 additions & 2 deletions src/cargo/util/toml/embedded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const DEFAULT_EDITION: crate::core::features::Edition =
crate::core::features::Edition::LATEST_STABLE;
const AUTO_FIELDS: &[&str] = &["autobins", "autoexamples", "autotests", "autobenches"];

pub fn expand_manifest(
pub(super) fn expand_manifest(
content: &str,
path: &std::path::Path,
config: &Config,
Expand Down Expand Up @@ -329,7 +329,7 @@ impl DocFragment {
}

#[derive(Clone, Copy, PartialEq, Debug)]
pub enum CommentKind {
enum CommentKind {
Line,
Block,
}
Expand Down
25 changes: 12 additions & 13 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::util::{
RustVersion,
};

pub mod embedded;
mod embedded;
pub mod schema;
mod targets;
use self::targets::targets;
Expand Down Expand Up @@ -1446,7 +1446,7 @@ fn inheritable_from_path(
}

/// Returns the name of the README file for a [`schema::TomlPackage`].
pub fn readme_for_package(
fn readme_for_package(
package_root: &Path,
readme: Option<&schema::StringOrBool>,
) -> Option<String> {
Expand Down Expand Up @@ -1505,7 +1505,7 @@ macro_rules! inheritable_field_getter {
( $(($key:literal, $field:ident -> $ret:ty),)* ) => (
$(
#[doc = concat!("Gets the field `workspace.", $key, "`.")]
pub fn $field(&self) -> CargoResult<$ret> {
fn $field(&self) -> CargoResult<$ret> {
let Some(val) = &self.$field else {
bail!("`workspace.{}` was not defined", $key);
};
Expand All @@ -1518,7 +1518,6 @@ macro_rules! inheritable_field_getter {
impl schema::InheritableFields {
inheritable_field_getter! {
// Please keep this list lexicographically ordered.
("dependencies", dependencies -> BTreeMap<String, schema::TomlDependency>),
("lints", lints -> schema::TomlLints),
("package.authors", authors -> Vec<String>),
("package.badges", badges -> BTreeMap<String, BTreeMap<String, String>>),
Expand All @@ -1538,7 +1537,7 @@ impl schema::InheritableFields {
}

/// Gets a workspace dependency with the `name`.
pub fn get_dependency(
fn get_dependency(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I understand and agree the purpose of making them private, I am not sure if other projects depends on these and might be unhappy.

&self,
name: &str,
package_root: &Path,
Expand All @@ -1557,41 +1556,41 @@ impl schema::InheritableFields {
}

/// Gets the field `workspace.package.license-file`.
pub fn license_file(&self, package_root: &Path) -> CargoResult<String> {
fn license_file(&self, package_root: &Path) -> CargoResult<String> {
let Some(license_file) = &self.license_file else {
bail!("`workspace.package.license-file` was not defined");
};
resolve_relative_path("license-file", &self.ws_root, package_root, license_file)
}

/// Gets the field `workspace.package.readme`.
pub fn readme(&self, package_root: &Path) -> CargoResult<schema::StringOrBool> {
fn readme(&self, package_root: &Path) -> CargoResult<schema::StringOrBool> {
let Some(readme) = readme_for_package(self.ws_root.as_path(), self.readme.as_ref()) else {
bail!("`workspace.package.readme` was not defined");
};
resolve_relative_path("readme", &self.ws_root, package_root, &readme)
.map(schema::StringOrBool::String)
}

pub fn ws_root(&self) -> &PathBuf {
fn ws_root(&self) -> &PathBuf {
&self.ws_root
}

pub fn update_deps(&mut self, deps: Option<BTreeMap<String, schema::TomlDependency>>) {
fn update_deps(&mut self, deps: Option<BTreeMap<String, schema::TomlDependency>>) {
self.dependencies = deps;
}

pub fn update_lints(&mut self, lints: Option<schema::TomlLints>) {
fn update_lints(&mut self, lints: Option<schema::TomlLints>) {
self.lints = lints;
}

pub fn update_ws_path(&mut self, ws_root: PathBuf) {
fn update_ws_path(&mut self, ws_root: PathBuf) {
self.ws_root = ws_root;
}
}

impl schema::TomlPackage {
pub fn to_package_id(&self, source_id: SourceId, version: semver::Version) -> PackageId {
fn to_package_id(&self, source_id: SourceId, version: semver::Version) -> PackageId {
PackageId::pure(self.name.as_str().into(), version, source_id)
}
}
Expand Down Expand Up @@ -2084,7 +2083,7 @@ impl schema::TomlProfiles {
///
/// 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`.
pub fn validate(
fn validate(
&self,
cli_unstable: &CliUnstable,
features: &Features,
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/util/toml/targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const DEFAULT_BENCH_DIR_NAME: &'static str = "benches";
const DEFAULT_EXAMPLE_DIR_NAME: &'static str = "examples";
const DEFAULT_BIN_DIR_NAME: &'static str = "bin";

pub fn targets(
pub(super) fn targets(
features: &Features,
manifest: &TomlManifest,
package_name: &str,
Expand Down