From 8562fcac289da92aedcbdaebad9fc6820995176a Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 16 Mar 2024 14:14:19 -0700 Subject: [PATCH] Reduce visibility of items that are not publicly accessible --- src/cargo.rs | 18 +++++++++--------- src/dependencies.rs | 28 +++++++++++++++------------- src/diff.rs | 8 ++++---- src/directory.rs | 2 +- src/env.rs | 2 +- src/error.rs | 4 ++-- src/features.rs | 2 +- src/flock.rs | 2 +- src/inherit.rs | 5 +++-- src/manifest.rs | 16 ++++++++-------- src/normalize.rs | 8 ++++---- src/run.rs | 6 +++--- src/rustflags.rs | 4 ++-- src/term.rs | 12 ++++++------ 14 files changed, 60 insertions(+), 57 deletions(-) diff --git a/src/cargo.rs b/src/cargo.rs index eef0dc4..6de6442 100644 --- a/src/cargo.rs +++ b/src/cargo.rs @@ -9,21 +9,21 @@ use std::process::{Command, Output, Stdio}; use std::{env, fs, iter}; #[derive(Deserialize)] -pub struct Metadata { +pub(crate) struct Metadata { pub target_directory: Directory, pub workspace_root: Directory, pub packages: Vec, } #[derive(Deserialize)] -pub struct PackageMetadata { +pub(crate) struct PackageMetadata { pub name: String, pub targets: Vec, pub manifest_path: PathBuf, } #[derive(Deserialize)] -pub struct BuildTarget { +pub(crate) struct BuildTarget { pub crate_types: Vec, } @@ -51,7 +51,7 @@ fn cargo_target_dir(project: &Project) -> impl Iterator Result { +pub(crate) fn manifest_dir() -> Result { if let Some(manifest_dir) = env::var_os("CARGO_MANIFEST_DIR") { return Ok(Directory::from(manifest_dir)); } @@ -64,7 +64,7 @@ pub fn manifest_dir() -> Result { } } -pub fn build_dependencies(project: &mut Project) -> Result<()> { +pub(crate) fn build_dependencies(project: &mut Project) -> Result<()> { let workspace_cargo_lock = path!(project.workspace / "Cargo.lock"); if workspace_cargo_lock.exists() { let _ = fs::copy(workspace_cargo_lock, path!(project.dir / "Cargo.lock")); @@ -97,7 +97,7 @@ pub fn build_dependencies(project: &mut Project) -> Result<()> { Ok(()) } -pub fn build_test(project: &Project, name: &Name) -> Result { +pub(crate) fn build_test(project: &Project, name: &Name) -> Result { let _ = cargo(project) .arg("clean") .arg("--package") @@ -120,7 +120,7 @@ pub fn build_test(project: &Project, name: &Name) -> Result { .map_err(Error::Cargo) } -pub fn build_all_tests(project: &Project) -> Result { +pub(crate) fn build_all_tests(project: &Project) -> Result { let _ = cargo(project) .arg("clean") .arg("--package") @@ -143,7 +143,7 @@ pub fn build_all_tests(project: &Project) -> Result { .map_err(Error::Cargo) } -pub fn run_test(project: &Project, name: &Name) -> Result { +pub(crate) fn run_test(project: &Project, name: &Name) -> Result { cargo(project) .arg("run") .args(target()) @@ -156,7 +156,7 @@ pub fn run_test(project: &Project, name: &Name) -> Result { .map_err(Error::Cargo) } -pub fn metadata() -> Result { +pub(crate) fn metadata() -> Result { let output = raw_cargo() .arg("metadata") .arg("--no-deps") diff --git a/src/dependencies.rs b/src/dependencies.rs index 934ae61..e87d2fe 100644 --- a/src/dependencies.rs +++ b/src/dependencies.rs @@ -13,7 +13,7 @@ use std::fmt; use std::fs; use std::path::PathBuf; -pub fn get_manifest(manifest_dir: &Directory) -> Result { +pub(crate) fn get_manifest(manifest_dir: &Directory) -> Result { let cargo_toml_path = manifest_dir.join("Cargo.toml"); let mut manifest = (|| { let manifest_str = fs::read_to_string(&cargo_toml_path)?; @@ -32,11 +32,13 @@ pub fn get_manifest(manifest_dir: &Directory) -> Result { Ok(manifest) } -pub fn get_workspace_manifest(manifest_dir: &Directory) -> WorkspaceManifest { +pub(crate) fn get_workspace_manifest(manifest_dir: &Directory) -> WorkspaceManifest { try_get_workspace_manifest(manifest_dir).unwrap_or_default() } -pub fn try_get_workspace_manifest(manifest_dir: &Directory) -> Result { +pub(crate) fn try_get_workspace_manifest( + manifest_dir: &Directory, +) -> Result { let cargo_toml_path = manifest_dir.join("Cargo.toml"); let manifest_str = fs::read_to_string(cargo_toml_path)?; let mut manifest: WorkspaceManifest = basic_toml::from_str(&manifest_str)?; @@ -72,7 +74,7 @@ fn fix_replacements(replacements: &mut Map, dir: &Directory) { } #[derive(Deserialize, Default, Debug)] -pub struct WorkspaceManifest { +pub(crate) struct WorkspaceManifest { #[serde(default)] pub workspace: WorkspaceWorkspace, #[serde(default)] @@ -82,7 +84,7 @@ pub struct WorkspaceManifest { } #[derive(Deserialize, Default, Debug)] -pub struct WorkspaceWorkspace { +pub(crate) struct WorkspaceWorkspace { #[serde(default)] pub package: WorkspacePackage, #[serde(default)] @@ -90,12 +92,12 @@ pub struct WorkspaceWorkspace { } #[derive(Deserialize, Default, Debug)] -pub struct WorkspacePackage { +pub(crate) struct WorkspacePackage { pub edition: Option, } #[derive(Deserialize, Default, Debug)] -pub struct Manifest { +pub(crate) struct Manifest { #[serde(rename = "cargo-features", default)] pub cargo_features: Vec, #[serde(default)] @@ -111,7 +113,7 @@ pub struct Manifest { } #[derive(Deserialize, Default, Debug)] -pub struct Package { +pub(crate) struct Package { pub name: String, #[serde(default)] pub edition: EditionOrInherit, @@ -119,14 +121,14 @@ pub struct Package { } #[derive(Debug)] -pub enum EditionOrInherit { +pub(crate) enum EditionOrInherit { Edition(Edition), Inherit, } #[derive(Serialize, Deserialize, Clone, Debug)] #[serde(remote = "Self")] -pub struct Dependency { +pub(crate) struct Dependency { #[serde(skip_serializing_if = "Option::is_none")] pub version: Option, #[serde(skip_serializing_if = "Option::is_none")] @@ -156,7 +158,7 @@ pub struct Dependency { } #[derive(Serialize, Deserialize, Clone, Debug)] -pub struct TargetDependencies { +pub(crate) struct TargetDependencies { #[serde(default, skip_serializing_if = "Map::is_empty")] pub dependencies: Map, #[serde( @@ -169,12 +171,12 @@ pub struct TargetDependencies { #[derive(Serialize, Deserialize, Clone, Debug)] #[serde(transparent)] -pub struct RegistryPatch { +pub(crate) struct RegistryPatch { pub crates: Map, } #[derive(Serialize, Deserialize, Clone, Debug)] -pub struct Patch { +pub(crate) struct Patch { #[serde(skip_serializing_if = "Option::is_none")] pub path: Option, #[serde(skip_serializing_if = "Option::is_none")] diff --git a/src/diff.rs b/src/diff.rs index 190a96e..02feb33 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -1,6 +1,6 @@ -pub use self::r#impl::Diff; +pub(crate) use self::r#impl::Diff; -pub enum Render<'a> { +pub(crate) enum Render<'a> { Common(&'a str), Unique(&'a str), } @@ -12,7 +12,7 @@ mod r#impl { use std::cmp; use std::panic; - pub struct Diff<'a> { + pub(crate) struct Diff<'a> { expected: &'a str, actual: &'a str, diff: Vec>, @@ -66,7 +66,7 @@ mod r#impl { mod r#impl { use super::Render; - pub enum Diff {} + pub(crate) enum Diff {} impl Diff { pub fn compute(_expected: &str, _actual: &str) -> Option { diff --git a/src/directory.rs b/src/directory.rs index d2193be..a52a4ce 100644 --- a/src/directory.rs +++ b/src/directory.rs @@ -8,7 +8,7 @@ use std::path::{Path, PathBuf}; #[derive(Clone, Debug, Serialize)] #[serde(transparent)] -pub struct Directory { +pub(crate) struct Directory { path: PathBuf, } diff --git a/src/env.rs b/src/env.rs index cfd4f43..4877ccc 100644 --- a/src/env.rs +++ b/src/env.rs @@ -2,7 +2,7 @@ use crate::error::{Error, Result}; use std::env; #[derive(PartialEq, Debug)] -pub enum Update { +pub(crate) enum Update { Wip, Overwrite, } diff --git a/src/error.rs b/src/error.rs index d6da1d1..e8923c6 100644 --- a/src/error.rs +++ b/src/error.rs @@ -5,7 +5,7 @@ use std::io; use std::path::PathBuf; #[derive(Debug)] -pub enum Error { +pub(crate) enum Error { Cargo(io::Error), CargoFail, GetManifest(PathBuf, Box), @@ -25,7 +25,7 @@ pub enum Error { WriteStderr(io::Error), } -pub type Result = std::result::Result; +pub(crate) type Result = std::result::Result; impl Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { diff --git a/src/features.rs b/src/features.rs index a885b6a..7767882 100644 --- a/src/features.rs +++ b/src/features.rs @@ -6,7 +6,7 @@ use std::ffi::OsStr; use std::fs; use std::path::PathBuf; -pub fn find() -> Option> { +pub(crate) fn find() -> Option> { try_find().ok() } diff --git a/src/flock.rs b/src/flock.rs index 24e24be..0b6c14a 100644 --- a/src/flock.rs +++ b/src/flock.rs @@ -10,7 +10,7 @@ use std::time::{Duration, SystemTime}; static LOCK: OnceCell> = OnceCell::new(); -pub struct Lock { +pub(crate) struct Lock { intraprocess_guard: Guard, lockfile: FileLock, } diff --git a/src/inherit.rs b/src/inherit.rs index 056e28d..b7f10dd 100644 --- a/src/inherit.rs +++ b/src/inherit.rs @@ -4,11 +4,12 @@ use std::fmt; #[derive(Deserialize)] #[serde(deny_unknown_fields)] -pub struct InheritEdition { +pub(crate) struct InheritEdition { + #[allow(dead_code)] pub workspace: True, } -pub struct True; +pub(crate) struct True; impl<'de> Deserialize<'de> for True { fn deserialize(deserializer: D) -> Result diff --git a/src/manifest.rs b/src/manifest.rs index d9cff78..c5c8647 100644 --- a/src/manifest.rs +++ b/src/manifest.rs @@ -6,7 +6,7 @@ use std::ffi::OsStr; use std::path::PathBuf; #[derive(Serialize, Debug)] -pub struct Manifest { +pub(crate) struct Manifest { #[serde(rename = "cargo-features", skip_serializing_if = "Vec::is_empty")] pub cargo_features: Vec, pub package: Package, @@ -29,7 +29,7 @@ pub struct Manifest { } #[derive(Serialize, Debug)] -pub struct Package { +pub(crate) struct Package { pub name: String, pub version: String, pub edition: Edition, @@ -39,7 +39,7 @@ pub struct Package { } #[derive(Serialize, Deserialize, Debug)] -pub enum Edition { +pub(crate) enum Edition { #[serde(rename = "2015")] E2015, #[serde(rename = "2018")] @@ -51,26 +51,26 @@ pub enum Edition { } #[derive(Serialize, Debug)] -pub struct Bin { +pub(crate) struct Bin { pub name: Name, pub path: PathBuf, } #[derive(Serialize, Clone, Debug)] -pub struct Name(pub String); +pub(crate) struct Name(pub String); #[derive(Serialize, Debug)] -pub struct Config { +pub(crate) struct Config { pub build: Build, } #[derive(Serialize, Debug)] -pub struct Build { +pub(crate) struct Build { pub rustflags: Vec<&'static str>, } #[derive(Serialize, Debug)] -pub struct Workspace { +pub(crate) struct Workspace { #[serde(skip_serializing_if = "Map::is_empty")] pub dependencies: Map, } diff --git a/src/normalize.rs b/src/normalize.rs index bb1285b..b62b61a 100644 --- a/src/normalize.rs +++ b/src/normalize.rs @@ -9,7 +9,7 @@ use std::cmp; use std::path::Path; #[derive(Copy, Clone)] -pub struct Context<'a> { +pub(crate) struct Context<'a> { pub krate: &'a str, pub source_dir: &'a Directory, pub workspace: &'a Directory, @@ -73,7 +73,7 @@ normalizations! { /// /// There is one "preferred" variation which is what we print when the stderr /// file is absent or not a match. -pub fn diagnostics(output: &str, context: Context) -> Variations { +pub(crate) fn diagnostics(output: &str, context: Context) -> Variations { let output = output.replace("\r\n", "\n"); let mut result = Variations::default(); @@ -84,7 +84,7 @@ pub fn diagnostics(output: &str, context: Context) -> Variations { result } -pub struct Variations { +pub(crate) struct Variations { variations: [String; Normalization::ALL.len()], } @@ -107,7 +107,7 @@ impl Variations { } } -pub fn trim>(output: S) -> String { +pub(crate) fn trim>(output: S) -> String { let bytes = output.as_ref(); let mut normalized = String::from_utf8_lossy(bytes).into_owned(); diff --git a/src/run.rs b/src/run.rs index e0e5503..29cb56b 100644 --- a/src/run.rs +++ b/src/run.rs @@ -19,7 +19,7 @@ use std::path::{Path, PathBuf}; use std::str; #[derive(Debug)] -pub struct Project { +pub(crate) struct Project { pub dir: Directory, source_dir: Directory, pub target_dir: Directory, @@ -35,7 +35,7 @@ pub struct Project { } #[derive(Debug)] -pub struct PathDependency { +pub(crate) struct PathDependency { pub name: String, pub normalized_path: Directory, } @@ -46,7 +46,7 @@ struct Report { } impl Runner { - pub fn run(&mut self) { + pub(crate) fn run(&mut self) { let mut tests = expand_globs(&self.tests); filter(&mut tests); diff --git a/src/rustflags.rs b/src/rustflags.rs index 5279859..c652324 100644 --- a/src/rustflags.rs +++ b/src/rustflags.rs @@ -4,7 +4,7 @@ use std::ffi::OsString; const RUSTFLAGS: &str = "RUSTFLAGS"; const IGNORED_LINTS: &[&str] = &["dead_code"]; -pub fn make_vec() -> Vec<&'static str> { +pub(crate) fn make_vec() -> Vec<&'static str> { let mut rustflags = vec!["--cfg", "trybuild"]; for &lint in IGNORED_LINTS { @@ -15,7 +15,7 @@ pub fn make_vec() -> Vec<&'static str> { rustflags } -pub fn envs() -> impl IntoIterator { +pub(crate) fn envs() -> impl IntoIterator { let mut rustflags = env::var_os(RUSTFLAGS)?; for flag in make_vec() { diff --git a/src/term.rs b/src/term.rs index 1d4346f..736d0da 100644 --- a/src/term.rs +++ b/src/term.rs @@ -5,25 +5,25 @@ use termcolor::{Color, ColorChoice, ColorSpec, StandardStream as Stream, WriteCo static TERM: OnceCell> = OnceCell::new(); -pub fn lock() -> MutexGuard<'static, Term> { +pub(crate) fn lock() -> MutexGuard<'static, Term> { TERM.get_or_init(|| Mutex::new(Term::new())) .lock() .unwrap_or_else(PoisonError::into_inner) } -pub fn bold() { +pub(crate) fn bold() { lock().set_color(ColorSpec::new().set_bold(true)); } -pub fn color(color: Color) { +pub(crate) fn color(color: Color) { lock().set_color(ColorSpec::new().set_fg(Some(color))); } -pub fn bold_color(color: Color) { +pub(crate) fn bold_color(color: Color) { lock().set_color(ColorSpec::new().set_bold(true).set_fg(Some(color))); } -pub fn reset() { +pub(crate) fn reset() { lock().reset(); } @@ -43,7 +43,7 @@ macro_rules! println { }}; } -pub struct Term { +pub(crate) struct Term { spec: ColorSpec, stream: Stream, start_of_line: bool,