From b9e2eb6f929dbc7c4af59a5bb164668fffff159c Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Mon, 31 May 2021 14:58:35 +0800 Subject: [PATCH] Make all utils functions and structs crate-private and cleanup unused functions --- src/cli/common.rs | 18 +++--- src/cli/download_tracker.rs | 4 +- src/cli/self_update.rs | 8 +-- src/cli/self_update/unix.rs | 6 +- src/command.rs | 2 +- src/config.rs | 4 +- src/dist/component/package.rs | 8 +-- src/dist/component/transaction.rs | 2 +- src/dist/manifestation.rs | 2 +- src/dist/notifications.rs | 2 +- src/dist/temp.rs | 2 +- src/install.rs | 8 ++- src/lib.rs | 2 +- src/notifications.rs | 2 +- src/settings.rs | 6 +- src/utils/mod.rs | 12 ++-- src/utils/notifications.rs | 2 +- src/utils/notify.rs | 2 +- src/utils/raw.rs | 70 ++++++-------------- src/utils/toml_utils.rs | 12 ++-- src/utils/tty.rs | 8 +-- src/utils/units.rs | 6 +- src/utils/utils.rs | 103 ++++++++++++++++-------------- 23 files changed, 136 insertions(+), 155 deletions(-) diff --git a/src/cli/common.rs b/src/cli/common.rs index 1b7645feb8..192aba1f3e 100644 --- a/src/cli/common.rs +++ b/src/cli/common.rs @@ -249,7 +249,7 @@ fn show_channel_updates(cfg: &Cfg, toolchains: Vec<(String, Result Ok(()) } -pub fn update_all_channels( +pub(crate) fn update_all_channels( cfg: &Cfg, do_self_update: bool, force_update: bool, @@ -327,7 +327,7 @@ pub fn self_update_permitted(explicit: bool) -> Result { } } -pub fn self_update(before_restart: F) -> Result +pub(crate) fn self_update(before_restart: F) -> Result where F: FnOnce() -> Result, { @@ -354,7 +354,7 @@ where Ok(utils::ExitCode(0)) } -pub fn list_targets(toolchain: &Toolchain<'_>) -> Result { +pub(crate) fn list_targets(toolchain: &Toolchain<'_>) -> Result { let mut t = term2::stdout(); let distributable = DistributableToolchain::new_for_components(&toolchain)?; let components = distributable.list_components()?; @@ -378,7 +378,7 @@ pub fn list_targets(toolchain: &Toolchain<'_>) -> Result { Ok(utils::ExitCode(0)) } -pub fn list_installed_targets(toolchain: &Toolchain<'_>) -> Result { +pub(crate) fn list_installed_targets(toolchain: &Toolchain<'_>) -> Result { let mut t = term2::stdout(); let distributable = DistributableToolchain::new_for_components(&toolchain)?; let components = distributable.list_components()?; @@ -397,7 +397,7 @@ pub fn list_installed_targets(toolchain: &Toolchain<'_>) -> Result) -> Result { +pub(crate) fn list_components(toolchain: &Toolchain<'_>) -> Result { let mut t = term2::stdout(); let distributable = DistributableToolchain::new_for_components(&toolchain)?; let components = distributable.list_components()?; @@ -415,7 +415,7 @@ pub fn list_components(toolchain: &Toolchain<'_>) -> Result { Ok(utils::ExitCode(0)) } -pub fn list_installed_components(toolchain: &Toolchain<'_>) -> Result { +pub(crate) fn list_installed_components(toolchain: &Toolchain<'_>) -> Result { let mut t = term2::stdout(); let distributable = DistributableToolchain::new_for_components(&toolchain)?; let components = distributable.list_components()?; @@ -460,7 +460,7 @@ fn print_toolchain_path( Ok(()) } -pub fn list_toolchains(cfg: &Cfg, verbose: bool) -> Result { +pub(crate) fn list_toolchains(cfg: &Cfg, verbose: bool) -> Result { let toolchains = cfg.list_toolchains()?; if toolchains.is_empty() { writeln!(process().stdout(), "no installed toolchains")?; @@ -495,7 +495,7 @@ pub fn list_toolchains(cfg: &Cfg, verbose: bool) -> Result { Ok(utils::ExitCode(0)) } -pub fn list_overrides(cfg: &Cfg) -> Result { +pub(crate) fn list_overrides(cfg: &Cfg) -> Result { let overrides = cfg.settings_file.with(|s| Ok(s.overrides.clone()))?; if overrides.is_empty() { @@ -538,7 +538,7 @@ pub fn version() -> &'static str { &RENDERED } -pub fn dump_testament() -> Result { +pub(crate) fn dump_testament() -> Result { use git_testament::GitModification::*; writeln!( process().stdout(), diff --git a/src/cli/download_tracker.rs b/src/cli/download_tracker.rs index 95e4ca9aa7..2cb64665cf 100644 --- a/src/cli/download_tracker.rs +++ b/src/cli/download_tracker.rs @@ -73,7 +73,7 @@ impl DownloadTracker { self } - pub fn handle_notification(&mut self, n: &Notification<'_>) -> bool { + pub(crate) fn handle_notification(&mut self, n: &Notification<'_>) -> bool { match *n { Notification::Install(In::Utils(Un::DownloadContentLengthReceived(content_len))) => { self.content_length_received(content_len); @@ -219,7 +219,7 @@ impl DownloadTracker { } } - pub fn push_unit(&mut self, new_unit: Unit) { + pub(crate) fn push_unit(&mut self, new_unit: Unit) { self.units.push(new_unit); } diff --git a/src/cli/self_update.rs b/src/cli/self_update.rs index 85b5214199..11e343f2e9 100644 --- a/src/cli/self_update.rs +++ b/src/cli/self_update.rs @@ -70,7 +70,7 @@ use crate::utils::Notification; use crate::{Cfg, UpdateStatus}; use crate::{DUP_TOOLS, TOOLS}; use os::*; -pub use os::{delete_rustup_and_cargo_home, run_update, self_replace}; +pub(crate) use os::{delete_rustup_and_cargo_home, run_update, self_replace}; #[cfg(windows)] pub use windows::complete_windows_uninstall; @@ -324,7 +324,7 @@ fn canonical_cargo_home() -> Result> { /// Installing is a simple matter of copying the running binary to /// `CARGO_HOME`/bin, hard-linking the various Rust tools to it, /// and adding `CARGO_HOME`/bin to PATH. -pub fn install( +pub(crate) fn install( no_prompt: bool, verbose: bool, quiet: bool, @@ -862,7 +862,7 @@ fn _install_selection<'a>( }) } -pub fn uninstall(no_prompt: bool) -> Result { +pub(crate) fn uninstall(no_prompt: bool) -> Result { if NEVER_SELF_UPDATE { err!("self-uninstall is disabled for this build of rustup"); err!("you should probably use your system package manager to uninstall rustup"); @@ -978,7 +978,7 @@ pub fn uninstall(no_prompt: bool) -> Result { /// (and on windows this process will not be running to do it), /// rustup-init is stored in `CARGO_HOME`/bin, and then deleted next /// time rustup runs. -pub fn update(cfg: &Cfg) -> Result { +pub(crate) fn update(cfg: &Cfg) -> Result { use common::SelfUpdatePermission::*; let update_permitted = if NEVER_SELF_UPDATE { HardFail diff --git a/src/cli/self_update/unix.rs b/src/cli/self_update/unix.rs index d7f9a5a2b6..697bfa8f88 100644 --- a/src/cli/self_update/unix.rs +++ b/src/cli/self_update/unix.rs @@ -12,7 +12,7 @@ use crate::utils::Notification; // If the user is trying to install with sudo, on some systems this will // result in writing root-owned files to the user's home directory, because // sudo is configured not to change $HOME. Don't let that bogosity happen. -pub fn do_anti_sudo_check(no_prompt: bool) -> Result { +pub(crate) fn do_anti_sudo_check(no_prompt: bool) -> Result { pub fn home_mismatch() -> (bool, PathBuf, PathBuf) { let fallback = || (false, PathBuf::new(), PathBuf::new()); // test runner should set this, nothing else @@ -126,7 +126,7 @@ pub fn do_remove_from_programs() -> Result<()> { /// Tell the upgrader to replace the rustup bins, then delete /// itself. -pub fn run_update(setup_path: &Path) -> Result { +pub(crate) fn run_update(setup_path: &Path) -> Result { let status = Command::new(setup_path) .arg("--self-replace") .status() @@ -142,7 +142,7 @@ pub fn run_update(setup_path: &Path) -> Result { /// This function is as the final step of a self-upgrade. It replaces /// `CARGO_HOME`/bin/rustup with the running exe, and updates the the /// links to it. -pub fn self_replace() -> Result { +pub(crate) fn self_replace() -> Result { install_bins()?; Ok(utils::ExitCode(0)) diff --git a/src/command.rs b/src/command.rs index 1efd1e1a47..07b9e3c424 100644 --- a/src/command.rs +++ b/src/command.rs @@ -7,7 +7,7 @@ use anyhow::{Context, Result}; use crate::errors::*; use crate::utils::utils::ExitCode; -pub fn run_command_for_dir>( +pub(crate) fn run_command_for_dir>( mut cmd: Command, arg0: &str, args: &[S], diff --git a/src/config.rs b/src/config.rs index c6a17b91f0..8a0b62339e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -246,7 +246,7 @@ pub struct Cfg { } impl Cfg { - pub fn from_env(notify_handler: Arc)>) -> Result { + pub(crate) fn from_env(notify_handler: Arc)>) -> Result { // Set up the rustup home directory let rustup_dir = utils::rustup_home()?; @@ -359,7 +359,7 @@ impl Cfg { } /// construct a download configuration - pub fn download_cfg<'a>( + pub(crate) fn download_cfg<'a>( &'a self, notify_handler: &'a dyn Fn(crate::dist::Notification<'_>), ) -> DownloadCfg<'a> { diff --git a/src/dist/component/package.rs b/src/dist/component/package.rs index a1d9daee11..db310d3cfc 100644 --- a/src/dist/component/package.rs +++ b/src/dist/component/package.rs @@ -139,7 +139,7 @@ impl Package for DirectoryPackage { pub struct TarPackage<'a>(DirectoryPackage, temp::Dir<'a>); impl<'a> TarPackage<'a> { - pub fn new( + pub(crate) fn new( stream: R, temp_cfg: &'a temp::Cfg, notify_handler: Option<&'a dyn Fn(Notification<'_>)>, @@ -551,7 +551,7 @@ impl<'a> Package for TarPackage<'a> { pub struct TarGzPackage<'a>(TarPackage<'a>); impl<'a> TarGzPackage<'a> { - pub fn new( + pub(crate) fn new( stream: R, temp_cfg: &'a temp::Cfg, notify_handler: Option<&'a dyn Fn(Notification<'_>)>, @@ -587,7 +587,7 @@ impl<'a> Package for TarGzPackage<'a> { pub struct TarXzPackage<'a>(TarPackage<'a>); impl<'a> TarXzPackage<'a> { - pub fn new( + pub(crate) fn new( stream: R, temp_cfg: &'a temp::Cfg, notify_handler: Option<&'a dyn Fn(Notification<'_>)>, @@ -623,7 +623,7 @@ impl<'a> Package for TarXzPackage<'a> { pub struct TarZStdPackage<'a>(TarPackage<'a>); impl<'a> TarZStdPackage<'a> { - pub fn new( + pub(crate) fn new( stream: R, temp_cfg: &'a temp::Cfg, notify_handler: Option<&'a dyn Fn(Notification<'_>)>, diff --git a/src/dist/component/transaction.rs b/src/dist/component/transaction.rs index 828ae8da3c..1facf7d195 100644 --- a/src/dist/component/transaction.rs +++ b/src/dist/component/transaction.rs @@ -168,7 +168,7 @@ impl<'a> Transaction<'a> { pub fn temp(&self) -> &'a temp::Cfg { self.temp_cfg } - pub fn notify_handler(&self) -> &'a dyn Fn(Notification<'_>) { + pub(crate) fn notify_handler(&self) -> &'a dyn Fn(Notification<'_>) { self.notify_handler } } diff --git a/src/dist/manifestation.rs b/src/dist/manifestation.rs index 33f071b5da..36c807fc24 100644 --- a/src/dist/manifestation.rs +++ b/src/dist/manifestation.rs @@ -379,7 +379,7 @@ impl Manifestation { } /// Installation using the legacy v1 manifest format - pub fn update_v1( + pub(crate) fn update_v1( &self, new_manifest: &[String], update_hash: Option<&Path>, diff --git a/src/dist/notifications.rs b/src/dist/notifications.rs index 21d8a074b6..5b68a536f8 100644 --- a/src/dist/notifications.rs +++ b/src/dist/notifications.rs @@ -54,7 +54,7 @@ impl<'a> From> for Notification<'a> { } impl<'a> Notification<'a> { - pub fn level(&self) -> NotificationLevel { + pub(crate) fn level(&self) -> NotificationLevel { use self::Notification::*; match self { Temp(n) => n.level(), diff --git a/src/dist/temp.rs b/src/dist/temp.rs index 4e07fd2e1f..96a342f105 100644 --- a/src/dist/temp.rs +++ b/src/dist/temp.rs @@ -49,7 +49,7 @@ pub struct File<'a> { } impl<'a> Notification<'a> { - pub fn level(&self) -> NotificationLevel { + pub(crate) fn level(&self) -> NotificationLevel { use self::Notification::*; match self { CreatingRoot(_) | CreatingFile(_) | CreatingDirectory(_) => NotificationLevel::Verbose, diff --git a/src/install.rs b/src/install.rs index 38c74edcab..a150f59d7c 100644 --- a/src/install.rs +++ b/src/install.rs @@ -86,7 +86,11 @@ impl<'a> InstallMethod<'a> { } } - pub fn run(self, path: &Path, notify_handler: &dyn Fn(Notification<'_>)) -> Result { + pub(crate) fn run( + self, + path: &Path, + notify_handler: &dyn Fn(Notification<'_>), + ) -> Result { if path.exists() { // Don't uninstall first for Dist method match self { @@ -147,6 +151,6 @@ impl<'a> InstallMethod<'a> { } } -pub fn uninstall(path: &Path, notify_handler: &dyn Fn(Notification<'_>)) -> Result<()> { +pub(crate) fn uninstall(path: &Path, notify_handler: &dyn Fn(Notification<'_>)) -> Result<()> { utils::remove_dir("install", path, notify_handler) } diff --git a/src/lib.rs b/src/lib.rs index c6bd36d042..aa47f6b80d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,7 +13,7 @@ pub use crate::currentprocess::*; pub use crate::errors::*; pub use crate::notifications::*; pub use crate::toolchain::*; -pub use crate::utils::{notify, toml_utils}; +pub(crate) use crate::utils::toml_utils; #[macro_use] extern crate rs_tracing; diff --git a/src/notifications.rs b/src/notifications.rs index 6c484d34a3..dd83e2b9a5 100644 --- a/src/notifications.rs +++ b/src/notifications.rs @@ -56,7 +56,7 @@ impl<'a> From> for Notification<'a> { } impl<'a> Notification<'a> { - pub fn level(&self) -> NotificationLevel { + pub(crate) fn level(&self) -> NotificationLevel { use self::Notification::*; match self { Install(n) => n.level(), diff --git a/src/settings.rs b/src/settings.rs index aabaf6cbb8..c0197fee51 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -108,7 +108,7 @@ impl Settings { } } - pub fn remove_override( + pub(crate) fn remove_override( &mut self, path: &Path, notify_handler: &dyn Fn(Notification<'_>), @@ -117,7 +117,7 @@ impl Settings { self.overrides.remove(&key).is_some() } - pub fn add_override( + pub(crate) fn add_override( &mut self, path: &Path, toolchain: String, @@ -128,7 +128,7 @@ impl Settings { self.overrides.insert(key, toolchain); } - pub fn dir_override( + pub(crate) fn dir_override( &self, dir: &Path, notify_handler: &dyn Fn(Notification<'_>), diff --git a/src/utils/mod.rs b/src/utils/mod.rs index db68c78118..627b01d844 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,11 +1,11 @@ ///! Utility functions for Rustup -pub mod notifications; +pub(crate) mod notifications; pub mod raw; -pub mod toml_utils; -pub mod tty; -pub mod units; +pub(crate) mod toml_utils; +pub(crate) mod tty; +pub(crate) mod units; #[allow(clippy::module_inception)] pub mod utils; -pub use crate::utils::notifications::Notification; -pub mod notify; +pub(crate) use crate::utils::notifications::Notification; +pub(crate) mod notify; diff --git a/src/utils/notifications.rs b/src/utils/notifications.rs index 2dd54fe148..56ff44d6b3 100644 --- a/src/utils/notifications.rs +++ b/src/utils/notifications.rs @@ -43,7 +43,7 @@ pub enum Notification<'a> { } impl<'a> Notification<'a> { - pub fn level(&self) -> NotificationLevel { + pub(crate) fn level(&self) -> NotificationLevel { use self::Notification::*; match self { SetDefaultBufferSize(_) => NotificationLevel::Debug, diff --git a/src/utils/notify.rs b/src/utils/notify.rs index c4f3a4f528..e30800404f 100644 --- a/src/utils/notify.rs +++ b/src/utils/notify.rs @@ -1,5 +1,5 @@ #[derive(Debug)] -pub enum NotificationLevel { +pub(crate) enum NotificationLevel { Verbose, Info, Warn, diff --git a/src/utils/raw.rs b/src/utils/raw.rs index 47b1257740..a65c60ef6d 100644 --- a/src/utils/raw.rs +++ b/src/utils/raw.rs @@ -1,17 +1,13 @@ use std::env; -use std::ffi::{OsStr, OsString}; use std::fs; use std::io; use std::io::Write; use std::path::Path; -use std::process::{Command, ExitStatus}; use std::str; -use thiserror::Error as ThisError; - use crate::process; -pub fn ensure_dir_exists, F: FnOnce(&Path)>( +pub(crate) fn ensure_dir_exists, F: FnOnce(&Path)>( path: P, callback: F, ) -> io::Result { @@ -23,7 +19,7 @@ pub fn ensure_dir_exists, F: FnOnce(&Path)>( } } -pub fn is_directory>(path: P) -> bool { +pub(crate) fn is_directory>(path: P) -> bool { fs::metadata(path).ok().as_ref().map(fs::Metadata::is_dir) == Some(true) } @@ -35,7 +31,7 @@ pub fn path_exists>(path: P) -> bool { fs::metadata(path).is_ok() } -pub fn random_string(length: usize) -> String { +pub(crate) fn random_string(length: usize) -> String { use rand::Rng; const CHARSET: &[u8] = b"abcdefghijklmnopqrstuvwxyz0123456789_"; let mut rng = rand::thread_rng(); @@ -44,7 +40,7 @@ pub fn random_string(length: usize) -> String { .collect() } -pub fn if_not_empty>(s: S) -> Option { +pub(crate) fn if_not_empty>(s: S) -> Option { if s == *"" { None } else { @@ -66,7 +62,7 @@ pub fn write_file(path: &Path, contents: &str) -> io::Result<()> { Ok(()) } -pub fn filter_file bool>( +pub(crate) fn filter_file bool>( src: &Path, dest: &Path, mut filter: F, @@ -106,7 +102,7 @@ pub fn append_file(dest: &Path, line: &str) -> io::Result<()> { Ok(()) } -pub fn symlink_dir(src: &Path, dest: &Path) -> io::Result<()> { +pub(crate) fn symlink_dir(src: &Path, dest: &Path) -> io::Result<()> { #[cfg(windows)] fn symlink_dir_inner(src: &Path, dest: &Path) -> io::Result<()> { // std's symlink uses Windows's symlink function, which requires @@ -146,14 +142,14 @@ fn symlink_junction_inner(target: &Path, junction: &Path) -> io::Result<()> { #[repr(C)] #[allow(non_snake_case)] - pub struct REPARSE_MOUNTPOINT_DATA_BUFFER { - pub ReparseTag: DWORD, - pub ReparseDataLength: DWORD, - pub Reserved: WORD, - pub ReparseTargetLength: WORD, - pub ReparseTargetMaximumLength: WORD, - pub Reserved1: WORD, - pub ReparseTarget: WCHAR, + struct REPARSE_MOUNTPOINT_DATA_BUFFER { + ReparseTag: DWORD, + ReparseDataLength: DWORD, + Reserved: WORD, + ReparseTargetLength: WORD, + ReparseTargetMaximumLength: WORD, + Reserved1: WORD, + ReparseTarget: WCHAR, } // We're using low-level APIs to create the junction, and these are more picky about paths. @@ -214,31 +210,11 @@ fn symlink_junction_inner(target: &Path, junction: &Path) -> io::Result<()> { } } -pub fn hardlink(src: &Path, dest: &Path) -> io::Result<()> { +pub(crate) fn hardlink(src: &Path, dest: &Path) -> io::Result<()> { let _ = fs::remove_file(dest); fs::hard_link(src, dest) } -#[derive(Debug, ThisError)] -pub enum CommandError { - #[error("error running command")] - Io(#[source] io::Error), - #[error("command exited with unsuccessful status {0}")] - Status(ExitStatus), -} - -pub type CommandResult = Result; - -pub fn cmd_status(cmd: &mut Command) -> CommandResult<()> { - cmd.status().map_err(CommandError::Io).and_then(|s| { - if s.success() { - Ok(()) - } else { - Err(CommandError::Status(s)) - } - }) -} - pub fn remove_dir(path: &Path) -> io::Result<()> { if fs::symlink_metadata(path)?.file_type().is_symlink() { if cfg!(windows) { @@ -255,7 +231,7 @@ pub fn remove_dir(path: &Path) -> io::Result<()> { } } -pub fn copy_dir(src: &Path, dest: &Path) -> io::Result<()> { +pub(crate) fn copy_dir(src: &Path, dest: &Path) -> io::Result<()> { fs::create_dir(dest)?; for entry in src.read_dir()? { let entry = entry?; @@ -271,13 +247,7 @@ pub fn copy_dir(src: &Path, dest: &Path) -> io::Result<()> { Ok(()) } -pub fn prefix_arg>(name: &str, s: S) -> OsString { - let mut arg = OsString::from(name); - arg.push(s); - arg -} - -pub fn has_cmd(cmd: &str) -> bool { +fn has_cmd(cmd: &str) -> bool { let cmd = format!("{}{}", cmd, env::consts::EXE_SUFFIX); let path = process().var_os("PATH").unwrap_or_default(); env::split_paths(&path) @@ -285,17 +255,17 @@ pub fn has_cmd(cmd: &str) -> bool { .any(|p| p.exists()) } -pub fn find_cmd<'a>(cmds: &[&'a str]) -> Option<&'a str> { +pub(crate) fn find_cmd<'a>(cmds: &[&'a str]) -> Option<&'a str> { cmds.iter().cloned().find(|&s| has_cmd(s)) } #[cfg(windows)] -pub mod windows { +pub(crate) mod windows { use std::ffi::OsStr; use std::io; use std::os::windows::ffi::OsStrExt; - pub fn to_u16s>(s: S) -> io::Result> { + pub(crate) fn to_u16s>(s: S) -> io::Result> { fn inner(s: &OsStr) -> io::Result> { let mut maybe_result: Vec = s.encode_wide().collect(); if maybe_result.iter().any(|&u| u == 0) { diff --git a/src/utils/toml_utils.rs b/src/utils/toml_utils.rs index 42296895aa..1d7376e1c6 100644 --- a/src/utils/toml_utils.rs +++ b/src/utils/toml_utils.rs @@ -1,7 +1,7 @@ use anyhow::{anyhow, Result}; use thiserror::Error as ThisError; -pub fn get_value(table: &mut toml::value::Table, key: &str, path: &str) -> Result { +fn get_value(table: &mut toml::value::Table, key: &str, path: &str) -> Result { table .remove(key) .ok_or_else(|| anyhow!(format!("missing key: '{}'", path.to_owned() + key))) @@ -11,7 +11,7 @@ pub fn get_value(table: &mut toml::value::Table, key: &str, path: &str) -> Resul #[error("expected type: '{0}' for '{1}'")] struct ExpectedType(&'static str, String); -pub fn get_string(table: &mut toml::value::Table, key: &str, path: &str) -> Result { +pub(crate) fn get_string(table: &mut toml::value::Table, key: &str, path: &str) -> Result { if let toml::Value::String(s) = get_value(table, key, path)? { Ok(s) } else { @@ -19,7 +19,7 @@ pub fn get_string(table: &mut toml::value::Table, key: &str, path: &str) -> Resu } } -pub fn get_opt_string( +pub(crate) fn get_opt_string( table: &mut toml::value::Table, key: &str, path: &str, @@ -35,7 +35,7 @@ pub fn get_opt_string( } } -pub fn get_bool(table: &mut toml::value::Table, key: &str, path: &str) -> Result { +pub(crate) fn get_bool(table: &mut toml::value::Table, key: &str, path: &str) -> Result { get_value(table, key, path).and_then(|v| { if let toml::Value::Boolean(b) = v { Ok(b) @@ -45,7 +45,7 @@ pub fn get_bool(table: &mut toml::value::Table, key: &str, path: &str) -> Result }) } -pub fn get_table( +pub(crate) fn get_table( table: &mut toml::value::Table, key: &str, path: &str, @@ -61,7 +61,7 @@ pub fn get_table( } } -pub fn get_array( +pub(crate) fn get_array( table: &mut toml::value::Table, key: &str, path: &str, diff --git a/src/utils/tty.rs b/src/utils/tty.rs index 5911b56062..c7ae1c9451 100644 --- a/src/utils/tty.rs +++ b/src/utils/tty.rs @@ -1,21 +1,21 @@ // Copied from rustc. isatty crate did not work as expected #[cfg(unix)] -pub fn stderr_isatty() -> bool { +pub(crate) fn stderr_isatty() -> bool { isatty(libc::STDERR_FILENO) } #[cfg(windows)] -pub fn stderr_isatty() -> bool { +pub(crate) fn stderr_isatty() -> bool { isatty(winapi::um::winbase::STD_ERROR_HANDLE) } #[cfg(unix)] -pub fn stdout_isatty() -> bool { +pub(crate) fn stdout_isatty() -> bool { isatty(libc::STDOUT_FILENO) } #[cfg(windows)] -pub fn stdout_isatty() -> bool { +pub(crate) fn stdout_isatty() -> bool { isatty(winapi::um::winbase::STD_OUTPUT_HANDLE) } diff --git a/src/utils/units.rs b/src/utils/units.rs index 19d84e5b4b..39fddb1538 100644 --- a/src/utils/units.rs +++ b/src/utils/units.rs @@ -6,19 +6,19 @@ pub enum Unit { IO, } -pub enum UnitMode { +pub(crate) enum UnitMode { Norm, Rate, } /// Human readable size (some units) -pub enum Size { +pub(crate) enum Size { B(usize, UnitMode), IO(usize, UnitMode), } impl Size { - pub fn new(size: usize, unit: Unit, unitmode: UnitMode) -> Self { + pub(crate) fn new(size: usize, unit: Unit, unitmode: UnitMode) -> Self { match unit { Unit::B => Self::B(size, unitmode), Unit::IO => Self::IO(size, unitmode), diff --git a/src/utils/utils.rs b/src/utils/utils.rs index e788afa72b..acce9c11ca 100644 --- a/src/utils/utils.rs +++ b/src/utils/utils.rs @@ -3,7 +3,6 @@ use std::env; use std::fs::{self, File}; use std::io::{self, BufReader, Write}; use std::path::{Path, PathBuf}; -use std::process::Command; use anyhow::{anyhow, bail, Context, Result}; use retry::delay::{jitter, Fibonacci}; @@ -17,9 +16,9 @@ use crate::utils::notifications::Notification; use crate::utils::raw; use crate::{home_process, process}; -pub use crate::utils::utils::raw::{ - find_cmd, has_cmd, if_not_empty, is_directory, is_file, path_exists, prefix_arg, random_string, -}; +pub(crate) use crate::utils::utils::raw::{find_cmd, if_not_empty, is_directory}; + +pub use crate::utils::utils::raw::{is_file, path_exists}; pub struct ExitCode(pub i32); @@ -40,7 +39,7 @@ where }) } -pub fn open_file(name: &'static str, path: &Path) -> Result { +pub(crate) fn open_file(name: &'static str, path: &Path) -> Result { File::open(path).with_context(|| RustupError::ReadingFile { name, path: PathBuf::from(path), @@ -61,21 +60,26 @@ pub fn write_file(name: &'static str, path: &Path, contents: &str) -> Result<()> }) } -pub fn append_file(name: &'static str, path: &Path, line: &str) -> Result<()> { +pub(crate) fn append_file(name: &'static str, path: &Path, line: &str) -> Result<()> { raw::append_file(path, line).with_context(|| RustupError::WritingFile { name, path: PathBuf::from(path), }) } -pub fn write_line(name: &'static str, file: &mut File, path: &Path, line: &str) -> Result<()> { +pub(crate) fn write_line( + name: &'static str, + file: &mut File, + path: &Path, + line: &str, +) -> Result<()> { writeln!(file, "{}", line).with_context(|| RustupError::WritingFile { name, path: path.to_path_buf(), }) } -pub fn write_str(name: &'static str, file: &mut File, path: &Path, s: &str) -> Result<()> { +pub(crate) fn write_str(name: &'static str, file: &mut File, path: &Path, s: &str) -> Result<()> { write!(file, "{}", s).with_context(|| RustupError::WritingFile { name, path: path.to_path_buf(), @@ -94,7 +98,7 @@ where rename(name, src, dest, notify) } -pub fn rename_dir<'a, N>( +pub(crate) fn rename_dir<'a, N>( name: &'static str, src: &'a Path, dest: &'a Path, @@ -106,7 +110,7 @@ where rename(name, src, dest, notify) } -pub fn filter_file bool>( +pub(crate) fn filter_file bool>( name: &'static str, src: &Path, dest: &Path, @@ -122,7 +126,7 @@ pub fn filter_file bool>( }) } -pub fn canonicalize_path<'a, N>(path: &'a Path, notify_handler: &dyn Fn(N)) -> PathBuf +pub(crate) fn canonicalize_path<'a, N>(path: &'a Path, notify_handler: &dyn Fn(N)) -> PathBuf where N: From>, { @@ -141,7 +145,7 @@ pub fn download_file( download_file_with_resume(&url, &path, hasher, false, ¬ify_handler) } -pub fn download_file_with_resume( +pub(crate) fn download_file_with_resume( url: &Url, path: &Path, hasher: Option<&mut Sha256>, @@ -247,19 +251,11 @@ fn download_file_( res } -pub fn parse_url(url: &str) -> Result { +pub(crate) fn parse_url(url: &str) -> Result { Url::parse(url).with_context(|| format!("failed to parse url: {}", url)) } -pub fn cmd_status(name: &'static str, cmd: &mut Command) -> Result<()> { - use std::ffi::OsString; - - raw::cmd_status(cmd).with_context(|| RustupError::RunningCommand { - name: OsString::from(name), - }) -} - -pub fn assert_is_file(path: &Path) -> Result<()> { +pub(crate) fn assert_is_file(path: &Path) -> Result<()> { if !is_file(path) { Err(anyhow!(format!("not a file: '{}'", path.display()))) } else { @@ -267,7 +263,7 @@ pub fn assert_is_file(path: &Path) -> Result<()> { } } -pub fn assert_is_directory(path: &Path) -> Result<()> { +pub(crate) fn assert_is_directory(path: &Path) -> Result<()> { if !is_directory(path) { Err(anyhow!(format!("not a directory: '{}'", path.display()))) } else { @@ -275,7 +271,11 @@ pub fn assert_is_directory(path: &Path) -> Result<()> { } } -pub fn symlink_dir<'a, N>(src: &'a Path, dest: &'a Path, notify_handler: &dyn Fn(N)) -> Result<()> +pub(crate) fn symlink_dir<'a, N>( + src: &'a Path, + dest: &'a Path, + notify_handler: &dyn Fn(N), +) -> Result<()> where N: From>, { @@ -289,7 +289,7 @@ where }) } -pub fn hard_or_symlink_file(src: &Path, dest: &Path) -> Result<()> { +pub(crate) fn hard_or_symlink_file(src: &Path, dest: &Path) -> Result<()> { if hardlink_file(src, dest).is_err() { symlink_file(src, dest)?; } @@ -304,7 +304,7 @@ pub fn hardlink_file(src: &Path, dest: &Path) -> Result<()> { } #[cfg(unix)] -pub fn symlink_file(src: &Path, dest: &Path) -> Result<()> { +fn symlink_file(src: &Path, dest: &Path) -> Result<()> { std::os::unix::fs::symlink(src, dest).with_context(|| RustupError::LinkingFile { src: PathBuf::from(src), dest: PathBuf::from(dest), @@ -312,7 +312,7 @@ pub fn symlink_file(src: &Path, dest: &Path) -> Result<()> { } #[cfg(windows)] -pub fn symlink_file(src: &Path, dest: &Path) -> Result<()> { +fn symlink_file(src: &Path, dest: &Path) -> Result<()> { // we are supposed to not use symlink on windows Err(anyhow!(RustupError::LinkingFile { src: PathBuf::from(src), @@ -320,7 +320,11 @@ pub fn symlink_file(src: &Path, dest: &Path) -> Result<()> { })) } -pub fn copy_dir<'a, N>(src: &'a Path, dest: &'a Path, notify_handler: &dyn Fn(N)) -> Result<()> +pub(crate) fn copy_dir<'a, N>( + src: &'a Path, + dest: &'a Path, + notify_handler: &dyn Fn(N), +) -> Result<()> where N: From>, { @@ -334,7 +338,7 @@ where }) } -pub fn copy_file(src: &Path, dest: &Path) -> Result<()> { +pub(crate) fn copy_file(src: &Path, dest: &Path) -> Result<()> { let metadata = fs::symlink_metadata(src).with_context(|| RustupError::ReadingFile { name: "metadata for", path: PathBuf::from(src), @@ -354,7 +358,7 @@ pub fn copy_file(src: &Path, dest: &Path) -> Result<()> { } } -pub fn remove_dir<'a, N>( +pub(crate) fn remove_dir<'a, N>( name: &'static str, path: &'a Path, notify_handler: &dyn Fn(N), @@ -391,7 +395,7 @@ pub fn remove_file(name: &'static str, path: &Path) -> Result<()> { }) } -pub fn ensure_file_removed(name: &'static str, path: &Path) -> Result<()> { +pub(crate) fn ensure_file_removed(name: &'static str, path: &Path) -> Result<()> { let result = remove_file(name, path); if let Err(err) = &result { if let Some(retry::Error::Operation { error: e, .. }) = @@ -408,18 +412,18 @@ pub fn ensure_file_removed(name: &'static str, path: &Path) -> Result<()> { }) } -pub fn read_dir(name: &'static str, path: &Path) -> Result { +pub(crate) fn read_dir(name: &'static str, path: &Path) -> Result { fs::read_dir(path).with_context(|| RustupError::ReadingDirectory { name, path: PathBuf::from(path), }) } -pub fn open_browser(path: &Path) -> Result<()> { +pub(crate) fn open_browser(path: &Path) -> Result<()> { opener::open_browser(path).context("couldn't open browser") } -pub fn set_permissions(path: &Path, perms: fs::Permissions) -> Result<()> { +fn set_permissions(path: &Path, perms: fs::Permissions) -> Result<()> { fs::set_permissions(path, perms).map_err(|e| { RustupError::SettingPermissions { p: PathBuf::from(path), @@ -438,7 +442,7 @@ pub fn file_size(path: &Path) -> Result { .len()) } -pub fn make_executable(path: &Path) -> Result<()> { +pub(crate) fn make_executable(path: &Path) -> Result<()> { #[allow(clippy::unnecessary_wraps)] #[cfg(windows)] fn inner(_: &Path) -> Result<()> { @@ -468,7 +472,7 @@ pub fn make_executable(path: &Path) -> Result<()> { inner(path) } -pub fn current_dir() -> Result { +pub(crate) fn current_dir() -> Result { process() .current_dir() .context(RustupError::LocatingWorkingDir) @@ -478,23 +482,23 @@ pub fn current_exe() -> Result { env::current_exe().context(RustupError::LocatingWorkingDir) } -pub fn to_absolute>(path: P) -> Result { +pub(crate) fn to_absolute>(path: P) -> Result { current_dir().map(|mut v| { v.push(path); v }) } -pub fn home_dir() -> Option { +pub(crate) fn home_dir() -> Option { home::home_dir_from(&home_process()) } -pub fn cargo_home() -> Result { +pub(crate) fn cargo_home() -> Result { home::cargo_home_from(&home_process()).context("failed to determine cargo home") } // Creates a ~/.rustup folder -pub fn create_rustup_home() -> Result<()> { +pub(crate) fn create_rustup_home() -> Result<()> { // If RUSTUP_HOME is set then don't make any assumptions about where it's // ok to put ~/.rustup if process().var_os("RUSTUP_HOME").is_some() { @@ -511,16 +515,16 @@ fn dot_dir(name: &str) -> Option { home_dir().map(|p| p.join(name)) } -pub fn rustup_home_in_user_dir() -> Result { +fn rustup_home_in_user_dir() -> Result { // XXX: This error message seems wrong/bogus. dot_dir(".rustup").ok_or_else(|| anyhow::anyhow!("couldn't find value of RUSTUP_HOME")) } -pub fn rustup_home() -> Result { +pub(crate) fn rustup_home() -> Result { home::rustup_home_from(&home_process()).context("failed to determine rustup home dir") } -pub fn format_path_for_display(path: &str) -> String { +pub(crate) fn format_path_for_display(path: &str) -> String { let unc_present = path.find(r"\\?\"); match unc_present { @@ -529,7 +533,7 @@ pub fn format_path_for_display(path: &str) -> String { } } -pub fn toolchain_sort>(v: &mut Vec) { +pub(crate) fn toolchain_sort>(v: &mut Vec) { use semver::{Identifier, Version}; fn special_version(ord: u64, s: &str) -> Version { @@ -636,7 +640,7 @@ where }) } -pub fn delete_dir_contents(dir_path: &Path) { +pub(crate) fn delete_dir_contents(dir_path: &Path) { match remove_dir_all::remove_dir_contents(dir_path) { Err(e) if e.kind() != io::ErrorKind::NotFound => { panic!("Unable to clean up {}: {:?}", dir_path.display(), e); @@ -645,7 +649,7 @@ pub fn delete_dir_contents(dir_path: &Path) { } } -pub struct FileReaderWithProgress<'a> { +pub(crate) struct FileReaderWithProgress<'a> { fh: io::BufReader, notify_handler: &'a dyn Fn(Notification<'_>), nbytes: u64, @@ -653,7 +657,10 @@ pub struct FileReaderWithProgress<'a> { } impl<'a> FileReaderWithProgress<'a> { - pub fn new_file(path: &Path, notify_handler: &'a dyn Fn(Notification<'_>)) -> Result { + pub(crate) fn new_file( + path: &Path, + notify_handler: &'a dyn Fn(Notification<'_>), + ) -> Result { let fh = match File::open(path) { Ok(fh) => fh, Err(_) => { @@ -699,7 +706,7 @@ impl<'a> io::Read for FileReaderWithProgress<'a> { // search user database to get home dir of euid user #[cfg(unix)] -pub fn home_dir_from_passwd() -> Option { +pub(crate) fn home_dir_from_passwd() -> Option { use std::ffi::{CStr, OsString}; use std::mem::MaybeUninit; use std::os::unix::ffi::OsStringExt;