Skip to content

Commit 422c3b2

Browse files
authored
Use pub(crate) rather than pub (#2526)
Now that we actually have some pub items, this will help us to avoid accidentally leaking more (e.g. in #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.
1 parent 7c98e23 commit 422c3b2

31 files changed

+500
-481
lines changed

crate_universe/src/config.rs

Lines changed: 78 additions & 75 deletions
Large diffs are not rendered by default.

crate_universe/src/context.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Convert annotated metadata into a renderable context
22
3-
pub mod crate_context;
3+
pub(crate) mod crate_context;
44
mod platforms;
55

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

21-
pub use self::crate_context::*;
21+
pub(crate) use self::crate_context::*;
2222

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

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

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

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

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

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

4646
/// A list of crates visible to this bazel module.
47-
pub direct_dev_deps: BTreeSet<CrateId>,
47+
pub(crate) direct_dev_deps: BTreeSet<CrateId>,
4848
}
4949

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

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

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

211-
pub fn has_duplicate_workspace_member_dep(&self, dep: &CrateDependency) -> bool {
211+
pub(crate) fn has_duplicate_workspace_member_dep(&self, dep: &CrateDependency) -> bool {
212212
1 < self
213213
.workspace_member_deps()
214214
.into_iter()
215215
.filter(|check| check.id.name == dep.id.name && check.alias == dep.alias)
216216
.count()
217217
}
218218

219-
pub fn has_duplicate_binary_crate(&self, bin: &CrateId) -> bool {
219+
pub(crate) fn has_duplicate_binary_crate(&self, bin: &CrateId) -> bool {
220220
1 < self
221221
.binary_crates
222222
.iter()

crate_universe/src/context/crate_context.rs

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,25 @@ pub struct CrateDependency {
2727

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

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

4343
/// A glob pattern of all source files required by the target
44-
pub srcs: Glob,
44+
pub(crate) srcs: Glob,
4545
}
4646

4747
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Clone)]
48-
pub enum Rule {
48+
pub(crate) enum Rule {
4949
/// `rust_library`
5050
Library(TargetAttributes),
5151

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

7070
#[serde(skip_serializing_if = "BTreeSet::is_empty")]
71-
pub compile_data_glob: BTreeSet<String>,
71+
pub(crate) compile_data_glob: BTreeSet<String>,
7272

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

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

7979
#[serde(skip_serializing_if = "BTreeSet::is_empty")]
80-
pub data_glob: BTreeSet<String>,
80+
pub(crate) data_glob: BTreeSet<String>,
8181

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

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

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

91-
pub edition: String,
91+
pub(crate) edition: String,
9292

9393
#[serde(skip_serializing_if = "Option::is_none")]
94-
pub linker_script: Option<String>,
94+
pub(crate) linker_script: Option<String>,
9595

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

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

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

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

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

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

114-
pub version: String,
114+
pub(crate) version: String,
115115

116116
#[serde(skip_serializing_if = "Vec::is_empty")]
117-
pub tags: Vec<String>,
117+
pub(crate) tags: Vec<String>,
118118
}
119119

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

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

157157
#[serde(skip_serializing_if = "BTreeSet::is_empty")]
158-
pub data_glob: BTreeSet<String>,
158+
pub(crate) data_glob: BTreeSet<String>,
159159

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

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

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

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

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

192192
#[serde(skip_serializing_if = "Select::is_empty")]
193-
pub rundir: Select<String>,
193+
pub(crate) rundir: Select<String>,
194194

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

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

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

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

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

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

213213
#[serde(skip_serializing_if = "Option::is_none")]
214-
pub links: Option<String>,
214+
pub(crate) links: Option<String>,
215215

216216
#[serde(skip_serializing_if = "BTreeSet::is_empty")]
217-
pub toolchains: BTreeSet<Label>,
217+
pub(crate) toolchains: BTreeSet<Label>,
218218
}
219219

220220
impl Default for BuildScriptAttributes {
@@ -243,77 +243,77 @@ impl Default for BuildScriptAttributes {
243243
}
244244

245245
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
246-
pub struct CrateContext {
246+
pub(crate) struct CrateContext {
247247
/// The package name of the current crate
248-
pub name: String,
248+
pub(crate) name: String,
249249

250250
/// The full version of the current crate
251-
pub version: semver::Version,
251+
pub(crate) version: semver::Version,
252252

253253
/// The package URL of the current crate
254254
#[serde(default)]
255-
pub package_url: Option<String>,
255+
pub(crate) package_url: Option<String>,
256256

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

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

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

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

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

282282
/// The license used by the crate
283283
#[serde(default)]
284-
pub license: Option<String>,
284+
pub(crate) license: Option<String>,
285285

286286
/// The SPDX licence IDs
287287
/// #[serde(default)]
288-
pub license_ids: BTreeSet<String>,
288+
pub(crate) license_ids: BTreeSet<String>,
289289

290290
/// The license file
291291
#[serde(default)]
292-
pub license_file: Option<String>,
292+
pub(crate) license_file: Option<String>,
293293

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

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

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

309309
/// Transition rule to use instead of `alias`.
310310
#[serde(skip_serializing_if = "Option::is_none")]
311311
#[serde(default)]
312-
pub alias_rule: Option<AliasRule>,
312+
pub(crate) alias_rule: Option<AliasRule>,
313313
}
314314

315315
impl CrateContext {
316-
pub fn new(
316+
pub(crate) fn new(
317317
annotation: &CrateAnnotation,
318318
packages: &BTreeMap<PackageId, Package>,
319319
source_annotations: &BTreeMap<PackageId, SourceAnnotation>,

crate_universe/src/context/platforms.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::utils::target_triple::TargetTriple;
1010
/// Walk through all dependencies in a [CrateContext] list for all configuration specific
1111
/// dependencies to produce a mapping of configurations/Cargo target_triples to compatible
1212
/// Bazel target_triples. Also adds mappings for all known target_triples.
13-
pub fn resolve_cfg_platforms(
13+
pub(crate) fn resolve_cfg_platforms(
1414
crates: Vec<&CrateContext>,
1515
supported_platform_triples: &BTreeSet<TargetTriple>,
1616
) -> Result<BTreeMap<String, BTreeSet<TargetTriple>>> {

crate_universe/src/lockfile.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub(crate) fn write_lockfile(lockfile: Context, path: &Path, dry_run: bool) -> R
5555
}
5656

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

6060
impl Digest {
6161
pub(crate) fn new(
@@ -130,7 +130,7 @@ impl Digest {
130130
Self(hasher.finalize().encode_hex::<String>())
131131
}
132132

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

0 commit comments

Comments
 (0)