Skip to content

Commit

Permalink
Use pub(crate) rather than pub (bazelbuild#2526)
Browse files Browse the repository at this point in the history
Now that we actually have some pub items, this will help us to avoid
accidentally leaking more (e.g. in
bazelbuild#2515 we are leaking
`CrateId`, `Select`, and `CrateDependency` to be public because they're
marked `pub` not `pub(crate)`.

I'm not sure I like this, but wanted send as an RFC to discuss.
  • Loading branch information
illicitonion authored Feb 28, 2024
1 parent 7c98e23 commit 422c3b2
Show file tree
Hide file tree
Showing 31 changed files with 500 additions and 481 deletions.
153 changes: 78 additions & 75 deletions crate_universe/src/config.rs

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions crate_universe/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Convert annotated metadata into a renderable context

pub mod crate_context;
pub(crate) mod crate_context;
mod platforms;

use std::collections::{BTreeMap, BTreeSet};
Expand All @@ -18,42 +18,42 @@ use crate::metadata::{Annotations, Dependency};
use crate::select::Select;
use crate::utils::target_triple::TargetTriple;

pub use self::crate_context::*;
pub(crate) use self::crate_context::*;

/// A struct containing information about a Cargo dependency graph in an easily to consume
/// format for rendering reproducible Bazel targets.
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) struct Context {
/// The collective checksum of all inputs to the context
pub checksum: Option<Digest>,
pub(crate) checksum: Option<Digest>,

/// The collection of all crates that make up the dependency graph
pub crates: BTreeMap<CrateId, CrateContext>,
pub(crate) crates: BTreeMap<CrateId, CrateContext>,

/// A subset of only crates with binary targets
pub binary_crates: BTreeSet<CrateId>,
pub(crate) binary_crates: BTreeSet<CrateId>,

/// A subset of workspace members mapping to their workspace
/// path relative to the workspace root
pub workspace_members: BTreeMap<CrateId, String>,
pub(crate) workspace_members: BTreeMap<CrateId, String>,

/// A mapping of `cfg` flags to platform triples supporting the configuration
pub conditions: BTreeMap<String, BTreeSet<TargetTriple>>,
pub(crate) conditions: BTreeMap<String, BTreeSet<TargetTriple>>,

/// A list of crates visible to any bazel module.
pub direct_deps: BTreeSet<CrateId>,
pub(crate) direct_deps: BTreeSet<CrateId>,

/// A list of crates visible to this bazel module.
pub direct_dev_deps: BTreeSet<CrateId>,
pub(crate) direct_dev_deps: BTreeSet<CrateId>,
}

impl Context {
pub fn try_from_path<T: AsRef<Path>>(path: T) -> Result<Self> {
pub(crate) fn try_from_path<T: AsRef<Path>>(path: T) -> Result<Self> {
let data = fs::read_to_string(path.as_ref())?;
Ok(serde_json::from_str(&data)?)
}

pub fn new(annotations: Annotations) -> Result<Self> {
pub(crate) fn new(annotations: Annotations) -> Result<Self> {
// Build a map of crate contexts
let crates: BTreeMap<CrateId, CrateContext> = annotations
.metadata
Expand Down Expand Up @@ -192,7 +192,7 @@ impl Context {
}

/// Create a set of all direct dependencies of workspace member crates.
pub fn workspace_member_deps(&self) -> BTreeSet<CrateDependency> {
pub(crate) fn workspace_member_deps(&self) -> BTreeSet<CrateDependency> {
self.workspace_members
.keys()
.map(move |id| &self.crates[id])
Expand All @@ -208,15 +208,15 @@ impl Context {
.collect()
}

pub fn has_duplicate_workspace_member_dep(&self, dep: &CrateDependency) -> bool {
pub(crate) fn has_duplicate_workspace_member_dep(&self, dep: &CrateDependency) -> bool {
1 < self
.workspace_member_deps()
.into_iter()
.filter(|check| check.id.name == dep.id.name && check.alias == dep.alias)
.count()
}

pub fn has_duplicate_binary_crate(&self, bin: &CrateId) -> bool {
pub(crate) fn has_duplicate_binary_crate(&self, bin: &CrateId) -> bool {
1 < self
.binary_crates
.iter()
Expand Down
118 changes: 59 additions & 59 deletions crate_universe/src/context/crate_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@ pub struct CrateDependency {

#[derive(Debug, Default, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Clone)]
#[serde(default)]
pub struct TargetAttributes {
pub(crate) struct TargetAttributes {
/// The module name of the crate (notably, not the package name).
//
// This must be the first field of `TargetAttributes` to make it the
// lexicographically first thing the derived `Ord` implementation will sort
// by. The `Ord` impl controls the order of multiple rules of the same type
// in the same BUILD file. In particular, this makes packages with multiple
// bin crates generate those `rust_binary` targets in alphanumeric order.
pub crate_name: String,
pub(crate) crate_name: String,

/// The path to the crate's root source file, relative to the manifest.
pub crate_root: Option<String>,
pub(crate) crate_root: Option<String>,

/// A glob pattern of all source files required by the target
pub srcs: Glob,
pub(crate) srcs: Glob,
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Clone)]
pub enum Rule {
pub(crate) enum Rule {
/// `rust_library`
Library(TargetAttributes),

Expand All @@ -63,58 +63,58 @@ pub enum Rule {
/// [core rules of `rules_rust`](https://bazelbuild.github.io/rules_rust/defs.html).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct CommonAttributes {
pub(crate) struct CommonAttributes {
#[serde(skip_serializing_if = "Select::is_empty")]
pub compile_data: Select<BTreeSet<Label>>,
pub(crate) compile_data: Select<BTreeSet<Label>>,

#[serde(skip_serializing_if = "BTreeSet::is_empty")]
pub compile_data_glob: BTreeSet<String>,
pub(crate) compile_data_glob: BTreeSet<String>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub crate_features: Select<BTreeSet<String>>,
pub(crate) crate_features: Select<BTreeSet<String>>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub data: Select<BTreeSet<Label>>,
pub(crate) data: Select<BTreeSet<Label>>,

#[serde(skip_serializing_if = "BTreeSet::is_empty")]
pub data_glob: BTreeSet<String>,
pub(crate) data_glob: BTreeSet<String>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub deps: Select<BTreeSet<CrateDependency>>,
pub(crate) deps: Select<BTreeSet<CrateDependency>>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub extra_deps: Select<BTreeSet<Label>>,
pub(crate) extra_deps: Select<BTreeSet<Label>>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub deps_dev: Select<BTreeSet<CrateDependency>>,
pub(crate) deps_dev: Select<BTreeSet<CrateDependency>>,

pub edition: String,
pub(crate) edition: String,

#[serde(skip_serializing_if = "Option::is_none")]
pub linker_script: Option<String>,
pub(crate) linker_script: Option<String>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub proc_macro_deps: Select<BTreeSet<CrateDependency>>,
pub(crate) proc_macro_deps: Select<BTreeSet<CrateDependency>>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub extra_proc_macro_deps: Select<BTreeSet<Label>>,
pub(crate) extra_proc_macro_deps: Select<BTreeSet<Label>>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub proc_macro_deps_dev: Select<BTreeSet<CrateDependency>>,
pub(crate) proc_macro_deps_dev: Select<BTreeSet<CrateDependency>>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub rustc_env: Select<BTreeMap<String, String>>,
pub(crate) rustc_env: Select<BTreeMap<String, String>>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub rustc_env_files: Select<BTreeSet<String>>,
pub(crate) rustc_env_files: Select<BTreeSet<String>>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub rustc_flags: Select<Vec<String>>,
pub(crate) rustc_flags: Select<Vec<String>>,

pub version: String,
pub(crate) version: String,

#[serde(skip_serializing_if = "Vec::is_empty")]
pub tags: Vec<String>,
pub(crate) tags: Vec<String>,
}

impl Default for CommonAttributes {
Expand Down Expand Up @@ -147,21 +147,21 @@ impl Default for CommonAttributes {
// https://bazelbuild.github.io/rules_rust/cargo.html#cargo_build_script
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct BuildScriptAttributes {
pub(crate) struct BuildScriptAttributes {
#[serde(skip_serializing_if = "Select::is_empty")]
pub compile_data: Select<BTreeSet<Label>>,
pub(crate) compile_data: Select<BTreeSet<Label>>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub data: Select<BTreeSet<Label>>,
pub(crate) data: Select<BTreeSet<Label>>,

#[serde(skip_serializing_if = "BTreeSet::is_empty")]
pub data_glob: BTreeSet<String>,
pub(crate) data_glob: BTreeSet<String>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub deps: Select<BTreeSet<CrateDependency>>,
pub(crate) deps: Select<BTreeSet<CrateDependency>>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub extra_deps: Select<BTreeSet<Label>>,
pub(crate) extra_deps: Select<BTreeSet<Label>>,

// TODO: refactor a crate with a build.rs file from two into three bazel
// rules in order to deduplicate link_dep information. Currently as the
Expand All @@ -181,40 +181,40 @@ pub struct BuildScriptAttributes {
// normal dependencies. This could be handled a special rule, or just using
// a `filegroup`.
#[serde(skip_serializing_if = "Select::is_empty")]
pub link_deps: Select<BTreeSet<CrateDependency>>,
pub(crate) link_deps: Select<BTreeSet<CrateDependency>>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub extra_link_deps: Select<BTreeSet<Label>>,
pub(crate) extra_link_deps: Select<BTreeSet<Label>>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub build_script_env: Select<BTreeMap<String, String>>,
pub(crate) build_script_env: Select<BTreeMap<String, String>>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub rundir: Select<String>,
pub(crate) rundir: Select<String>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub extra_proc_macro_deps: Select<BTreeSet<Label>>,
pub(crate) extra_proc_macro_deps: Select<BTreeSet<Label>>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub proc_macro_deps: Select<BTreeSet<CrateDependency>>,
pub(crate) proc_macro_deps: Select<BTreeSet<CrateDependency>>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub rustc_env: Select<BTreeMap<String, String>>,
pub(crate) rustc_env: Select<BTreeMap<String, String>>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub rustc_flags: Select<Vec<String>>,
pub(crate) rustc_flags: Select<Vec<String>>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub rustc_env_files: Select<BTreeSet<String>>,
pub(crate) rustc_env_files: Select<BTreeSet<String>>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub tools: Select<BTreeSet<Label>>,
pub(crate) tools: Select<BTreeSet<Label>>,

#[serde(skip_serializing_if = "Option::is_none")]
pub links: Option<String>,
pub(crate) links: Option<String>,

#[serde(skip_serializing_if = "BTreeSet::is_empty")]
pub toolchains: BTreeSet<Label>,
pub(crate) toolchains: BTreeSet<Label>,
}

impl Default for BuildScriptAttributes {
Expand Down Expand Up @@ -243,77 +243,77 @@ impl Default for BuildScriptAttributes {
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CrateContext {
pub(crate) struct CrateContext {
/// The package name of the current crate
pub name: String,
pub(crate) name: String,

/// The full version of the current crate
pub version: semver::Version,
pub(crate) version: semver::Version,

/// The package URL of the current crate
#[serde(default)]
pub package_url: Option<String>,
pub(crate) package_url: Option<String>,

/// Optional source annotations if they were discoverable in the
/// lockfile. Workspace Members will not have source annotations and
/// potentially others.
#[serde(default)]
pub repository: Option<SourceAnnotation>,
pub(crate) repository: Option<SourceAnnotation>,

/// A list of all targets (lib, proc-macro, bin) associated with this package
#[serde(default)]
pub targets: BTreeSet<Rule>,
pub(crate) targets: BTreeSet<Rule>,

/// The name of the crate's root library target. This is the target that a dependent
/// would get if they were to depend on `{crate_name}`.
#[serde(default)]
pub library_target_name: Option<String>,
pub(crate) library_target_name: Option<String>,

/// A set of attributes common to most [Rule] types or target types.
#[serde(default)]
pub common_attrs: CommonAttributes,
pub(crate) common_attrs: CommonAttributes,

/// Optional attributes for build scripts. This field is only populated if
/// a build script (`custom-build`) target is defined for the crate.
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub build_script_attrs: Option<BuildScriptAttributes>,
pub(crate) build_script_attrs: Option<BuildScriptAttributes>,

/// The license used by the crate
#[serde(default)]
pub license: Option<String>,
pub(crate) license: Option<String>,

/// The SPDX licence IDs
/// #[serde(default)]
pub license_ids: BTreeSet<String>,
pub(crate) license_ids: BTreeSet<String>,

/// The license file
#[serde(default)]
pub license_file: Option<String>,
pub(crate) license_file: Option<String>,

/// Additional text to add to the generated BUILD file.
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub additive_build_file_content: Option<String>,
pub(crate) additive_build_file_content: Option<String>,

/// If true, disables pipelining for library targets generated for this crate
#[serde(skip_serializing_if = "std::ops::Not::not")]
#[serde(default)]
pub disable_pipelining: bool,
pub(crate) disable_pipelining: bool,

/// Extra targets that should be aliased.
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
#[serde(default)]
pub extra_aliased_targets: BTreeMap<String, String>,
pub(crate) extra_aliased_targets: BTreeMap<String, String>,

/// Transition rule to use instead of `alias`.
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub alias_rule: Option<AliasRule>,
pub(crate) alias_rule: Option<AliasRule>,
}

impl CrateContext {
pub fn new(
pub(crate) fn new(
annotation: &CrateAnnotation,
packages: &BTreeMap<PackageId, Package>,
source_annotations: &BTreeMap<PackageId, SourceAnnotation>,
Expand Down
2 changes: 1 addition & 1 deletion crate_universe/src/context/platforms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::utils::target_triple::TargetTriple;
/// Walk through all dependencies in a [CrateContext] list for all configuration specific
/// dependencies to produce a mapping of configurations/Cargo target_triples to compatible
/// Bazel target_triples. Also adds mappings for all known target_triples.
pub fn resolve_cfg_platforms(
pub(crate) fn resolve_cfg_platforms(
crates: Vec<&CrateContext>,
supported_platform_triples: &BTreeSet<TargetTriple>,
) -> Result<BTreeMap<String, BTreeSet<TargetTriple>>> {
Expand Down
4 changes: 2 additions & 2 deletions crate_universe/src/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub(crate) fn write_lockfile(lockfile: Context, path: &Path, dry_run: bool) -> R
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Clone)]
pub struct Digest(String);
pub(crate) struct Digest(String);

impl Digest {
pub(crate) fn new(
Expand Down Expand Up @@ -130,7 +130,7 @@ impl Digest {
Self(hasher.finalize().encode_hex::<String>())
}

pub fn bin_version(binary: &Path) -> Result<String> {
pub(crate) fn bin_version(binary: &Path) -> Result<String> {
let safe_vars = [OsStr::new("HOMEDRIVE"), OsStr::new("PATHEXT")];
let env = std::env::vars_os().filter(|(var, _)| safe_vars.contains(&var.as_os_str()));

Expand Down
Loading

0 comments on commit 422c3b2

Please sign in to comment.