Skip to content

Commit

Permalink
Prefer pub(crate) for visibility
Browse files Browse the repository at this point in the history
Summary:
`pub(crate)` generally gives better 'unused definition' warnings from rustc, because it knows the definition isn't used elsewhere.

Use `pub(crate)` wherever we can, and fix definitions that didn't need to be `pub` or `pub(crate)`.

Reviewed By: samkevich

Differential Revision: D56778389

fbshipit-source-id: b73364f96e9601e94a1196b25f78e92968a3edb0
  • Loading branch information
Wilfred authored and facebook-github-bot committed May 1, 2024
1 parent 27528d0 commit c15a71e
Show file tree
Hide file tree
Showing 11 changed files with 196 additions and 200 deletions.
39 changes: 21 additions & 18 deletions integrations/rust-project/src/buck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use crate::target::TargetInfo;
use crate::Crate;
use crate::Dep;

pub fn to_json_project(
pub(crate) fn to_json_project(
sysroot: Sysroot,
expanded_and_resolved: ExpandedAndResolved,
aliases: FxHashMap<Target, AliasedTargetInfo>,
Expand Down Expand Up @@ -286,7 +286,7 @@ fn format_route(route: &[usize], crates: &[Crate]) -> String {
}

/// If `path` starts with `base`, drop the prefix.
pub fn relative_to(path: &Path, base: &Path) -> PathBuf {
pub(crate) fn relative_to(path: &Path, base: &Path) -> PathBuf {
match path.strip_prefix(base) {
Ok(rel_path) => rel_path,
Err(_) => path,
Expand Down Expand Up @@ -366,12 +366,12 @@ fn as_deps(
}

#[derive(Debug, Default)]
pub struct Buck {
pub(crate) struct Buck {
mode: Option<String>,
}

impl Buck {
pub fn new(mode: Option<String>) -> Self {
pub(crate) fn new(mode: Option<String>) -> Self {
Buck { mode }
}

Expand Down Expand Up @@ -415,7 +415,7 @@ impl Buck {
}

/// Return the absolute path of the current Buck project root.
pub fn resolve_project_root(&self) -> Result<PathBuf, anyhow::Error> {
pub(crate) fn resolve_project_root(&self) -> Result<PathBuf, anyhow::Error> {
let mut command = self.command_without_config(["root"]);
command.arg("--kind=project");

Expand All @@ -429,7 +429,7 @@ impl Buck {
Ok(stdout.into())
}

pub fn resolve_root_of_file(&self, path: &Path) -> Result<PathBuf, anyhow::Error> {
pub(crate) fn resolve_root_of_file(&self, path: &Path) -> Result<PathBuf, anyhow::Error> {
let mut command = self.command_without_config(["root"]);
command.arg("--kind=project");

Expand All @@ -448,7 +448,7 @@ impl Buck {
Ok(stdout.into())
}

pub fn resolve_sysroot_src(&self) -> Result<PathBuf, anyhow::Error> {
pub(crate) fn resolve_sysroot_src(&self) -> Result<PathBuf, anyhow::Error> {
let mut command = self.command(["audit", "config"]);
command.args(["--json", "--", "rust.sysroot_src_path"]);
command
Expand All @@ -474,7 +474,7 @@ impl Buck {

/// Determines the owning target(s) of the saved file and builds them.
#[instrument]
pub fn check_saved_file(
pub(crate) fn check_saved_file(
&self,
use_clippy: bool,
saved_file: &Path,
Expand Down Expand Up @@ -519,7 +519,10 @@ impl Buck {
}

#[instrument(skip_all)]
pub fn expand_and_resolve(&self, targets: &[Target]) -> anyhow::Result<ExpandedAndResolved> {
pub(crate) fn expand_and_resolve(
&self,
targets: &[Target],
) -> anyhow::Result<ExpandedAndResolved> {
if targets.is_empty() {
return Ok(ExpandedAndResolved::default());
}
Expand All @@ -538,7 +541,7 @@ impl Buck {
}

#[instrument(skip_all)]
pub fn query_aliased_libraries(
pub(crate) fn query_aliased_libraries(
&self,
targets: &[Target],
) -> Result<FxHashMap<Target, AliasedTargetInfo>, anyhow::Error> {
Expand Down Expand Up @@ -570,7 +573,7 @@ impl Buck {
}

#[instrument(skip_all)]
pub fn query_owner(
pub(crate) fn query_owner(
&self,
files: &Vec<PathBuf>,
) -> Result<FxHashMap<String, Vec<Target>>, anyhow::Error> {
Expand All @@ -590,7 +593,10 @@ impl Buck {
}
}

pub fn utf8_output(output: io::Result<Output>, command: &Command) -> Result<String, anyhow::Error> {
pub(crate) fn utf8_output(
output: io::Result<Output>,
command: &Command,
) -> Result<String, anyhow::Error> {
match output {
Ok(Output {
stdout,
Expand All @@ -614,10 +620,7 @@ pub fn utf8_output(output: io::Result<Output>, command: &Command) -> Result<Stri
}
}

pub fn deserialize_output<T>(
output: io::Result<Output>,
command: &Command,
) -> Result<T, anyhow::Error>
fn deserialize_output<T>(output: io::Result<Output>, command: &Command) -> Result<T, anyhow::Error>
where
T: for<'a> Deserialize<'a>,
{
Expand Down Expand Up @@ -649,13 +652,13 @@ fn cmd_err(command: &Command, status: ExitStatus, stderr: &[u8]) -> anyhow::Erro

/// Trim a trailing new line from `String`.
/// Useful when trimming command output.
pub fn truncate_line_ending(s: &mut String) {
pub(crate) fn truncate_line_ending(s: &mut String) {
if let Some(x) = s.strip_suffix("\r\n").or_else(|| s.strip_suffix('\n')) {
s.truncate(x.len());
}
}

pub fn select_mode(mode: Option<&str>) -> Option<String> {
pub(crate) fn select_mode(mode: Option<&str>) -> Option<String> {
if let Some(mode) = mode {
Some(mode.to_owned())
} else if cfg!(target_os = "macos") {
Expand Down
8 changes: 4 additions & 4 deletions integrations/rust-project/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod check;
mod develop;
mod new;

pub use check::Check;
pub use develop::Develop;
pub use new::New;
pub use new::ProjectKind;
pub(crate) use check::Check;
pub(crate) use develop::Develop;
pub(crate) use new::New;
pub(crate) use new::ProjectKind;
12 changes: 6 additions & 6 deletions integrations/rust-project/src/cli/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ use crate::buck;
use crate::buck::select_mode;
use crate::diagnostics;

pub struct Check {
pub buck: buck::Buck,
pub use_clippy: bool,
pub saved_file: PathBuf,
pub(crate) struct Check {
pub(crate) buck: buck::Buck,
pub(crate) use_clippy: bool,
pub(crate) saved_file: PathBuf,
}

impl Check {
pub fn new(mode: Option<String>, use_clippy: bool, saved_file: PathBuf) -> Self {
pub(crate) fn new(mode: Option<String>, use_clippy: bool, saved_file: PathBuf) -> Self {
let mode = select_mode(mode.as_deref());
let buck = buck::Buck::new(mode);
Self {
Expand All @@ -31,7 +31,7 @@ impl Check {
}
}

pub fn run(&self) -> Result<(), anyhow::Error> {
pub(crate) fn run(&self) -> Result<(), anyhow::Error> {
let buck = &self.buck;

let cell_root = buck.resolve_root_of_file(&self.saved_file)?;
Expand Down
26 changes: 13 additions & 13 deletions integrations/rust-project/src/cli/develop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,32 @@ use crate::target::Target;
use crate::Command;

#[derive(Debug)]
pub struct Develop {
pub sysroot: SysrootConfig,
pub relative_paths: bool,
pub buck: buck::Buck,
pub check_cycles: bool,
pub(crate) struct Develop {
pub(crate) sysroot: SysrootConfig,
pub(crate) relative_paths: bool,
pub(crate) buck: buck::Buck,
pub(crate) check_cycles: bool,
}

pub struct OutputCfg {
pub(crate) struct OutputCfg {
out: Output,
pretty: bool,
}

#[derive(Debug)]
pub enum Input {
pub(crate) enum Input {
Targets(Vec<Target>),
Files(Vec<PathBuf>),
}

#[derive(Debug)]
pub enum Output {
pub(crate) enum Output {
Path(PathBuf),
Stdout,
}

impl Develop {
pub fn new() -> Self {
pub(crate) fn new() -> Self {
let mode = select_mode(None);
let buck = buck::Buck::new(mode);

Expand All @@ -67,7 +67,7 @@ impl Develop {
}
}

pub fn from_command(command: Command) -> (Develop, Input, OutputCfg) {
pub(crate) fn from_command(command: Command) -> (Develop, Input, OutputCfg) {
if let crate::Command::Develop {
files,
targets,
Expand Down Expand Up @@ -121,7 +121,7 @@ impl Develop {
}

impl Develop {
pub fn resolve_file_owners(
pub(crate) fn resolve_file_owners(
&self,
files: Vec<PathBuf>,
) -> Result<FxHashMap<String, Vec<Target>>, anyhow::Error> {
Expand Down Expand Up @@ -165,7 +165,7 @@ impl Develop {
Ok(file_owners)
}

pub fn run(&self, targets: Vec<Target>) -> Result<JsonProject, anyhow::Error> {
pub(crate) fn run(&self, targets: Vec<Target>) -> Result<JsonProject, anyhow::Error> {
let Develop {
sysroot,
relative_paths,
Expand Down Expand Up @@ -209,7 +209,7 @@ impl Develop {
Ok(rust_project)
}

pub fn run_as_cli(self, input: Input, cfg: OutputCfg) -> Result<(), anyhow::Error> {
pub(crate) fn run_as_cli(self, input: Input, cfg: OutputCfg) -> Result<(), anyhow::Error> {
let targets = match input {
Input::Targets(targets) => targets,
Input::Files(files) => {
Expand Down
26 changes: 13 additions & 13 deletions integrations/rust-project/src/cli/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ use clap::ValueEnum;
use tracing::info;

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
pub enum ProjectKind {
pub(crate) enum ProjectKind {
Binary,
Library,
}

pub struct New {
pub name: String,
pub kind: ProjectKind,
pub path: Option<PathBuf>,
pub(crate) struct New {
pub(crate) name: String,
pub(crate) kind: ProjectKind,
pub(crate) path: Option<PathBuf>,
}

impl New {
pub fn run(self) -> Result<(), anyhow::Error> {
pub(crate) fn run(self) -> Result<(), anyhow::Error> {
let name = self.name;

let (target, kind) = match self.kind {
Expand Down Expand Up @@ -96,15 +96,15 @@ impl New {
}
}

pub enum EntryFile {
enum EntryFile {
Main(MainFile),
Lib(LibFile),
}

pub struct MainFile;
struct MainFile;

impl MainFile {
pub fn render(&self) -> String {
fn render(&self) -> String {
"fn main() {
println!(\"Hello from Rust at Meta!\");
}
Expand All @@ -113,10 +113,10 @@ impl MainFile {
}
}

pub struct LibFile;
struct LibFile;

impl LibFile {
pub fn render(&self) -> String {
fn render(&self) -> String {
"pub fn add_numbers(left: usize, right: usize) -> usize {
left + right
}
Expand All @@ -137,13 +137,13 @@ mod tests {
}

#[derive(Debug, PartialEq, Eq)]
pub enum Target {
enum Target {
Library { name: String },
Binary { name: String },
}

impl Target {
pub fn render(&self) -> String {
fn render(&self) -> String {
match self {
Target::Library { name } => {
let template = include_str!("../../templates/TARGETS_LIB");
Expand Down
Loading

0 comments on commit c15a71e

Please sign in to comment.