Skip to content

Commit 8c1cec5

Browse files
committed
make more anyhow::Result usages explicit
1 parent abfbda1 commit 8c1cec5

17 files changed

+60
-64
lines changed

src/crates/git.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::CrateTrait;
22
use crate::cmd::{Command, ProcessLinesActions};
33
use crate::prepare::PrepareError;
44
use crate::Workspace;
5-
use anyhow::{Context as _, Result};
5+
use anyhow::Context as _;
66
use log::{info, warn};
77
use std::path::{Path, PathBuf};
88

@@ -63,7 +63,7 @@ impl GitRepo {
6363
}
6464

6565
impl CrateTrait for GitRepo {
66-
fn fetch(&self, workspace: &Workspace) -> Result<()> {
66+
fn fetch(&self, workspace: &Workspace) -> anyhow::Result<()> {
6767
// The credential helper that suppresses the password prompt shows this message when a
6868
// repository requires authentication:
6969
//
@@ -105,15 +105,15 @@ impl CrateTrait for GitRepo {
105105
}
106106
}
107107

108-
fn purge_from_cache(&self, workspace: &Workspace) -> Result<()> {
108+
fn purge_from_cache(&self, workspace: &Workspace) -> anyhow::Result<()> {
109109
let path = self.cached_path(workspace);
110110
if path.exists() {
111111
crate::utils::remove_dir_all(&path)?;
112112
}
113113
Ok(())
114114
}
115115

116-
fn copy_source_to(&self, workspace: &Workspace, dest: &Path) -> Result<()> {
116+
fn copy_source_to(&self, workspace: &Workspace, dest: &Path) -> anyhow::Result<()> {
117117
Command::new(workspace, "git")
118118
.args(&["clone"])
119119
.args(&[self.cached_path(workspace).as_path(), dest])

src/crates/local.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ fn copy_dir(src: &Path, dest: &Path) -> anyhow::Result<()> {
7474

7575
#[cfg(test)]
7676
mod tests {
77-
use anyhow::Result;
78-
7977
#[test]
8078
fn test_copy_dir() -> anyhow::Result<()> {
8179
let tmp_src = tempfile::tempdir()?;

src/crates/registry.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::CrateTrait;
22
use crate::Workspace;
3-
use anyhow::{Context as _, Result};
3+
use anyhow::Context as _;
44
use flate2::read::GzDecoder;
55
use log::info;
66
use std::fs::File;
@@ -88,7 +88,7 @@ impl RegistryCrate {
8888
.join(format!("{}-{}.crate", self.name, self.version))
8989
}
9090

91-
fn fetch_url(&self, workspace: &Workspace) -> Result<String> {
91+
fn fetch_url(&self, workspace: &Workspace) -> anyhow::Result<String> {
9292
match &self.registry {
9393
Registry::CratesIo => Ok(format!(
9494
"{0}/{1}/{1}-{2}.crate",
@@ -151,7 +151,7 @@ impl RegistryCrate {
151151
}
152152

153153
impl CrateTrait for RegistryCrate {
154-
fn fetch(&self, workspace: &Workspace) -> Result<()> {
154+
fn fetch(&self, workspace: &Workspace) -> anyhow::Result<()> {
155155
let local = self.cache_path(workspace);
156156
if local.exists() {
157157
info!("crate {} {} is already in cache", self.name, self.version);
@@ -173,15 +173,15 @@ impl CrateTrait for RegistryCrate {
173173
Ok(())
174174
}
175175

176-
fn purge_from_cache(&self, workspace: &Workspace) -> Result<()> {
176+
fn purge_from_cache(&self, workspace: &Workspace) -> anyhow::Result<()> {
177177
let path = self.cache_path(workspace);
178178
if path.exists() {
179179
crate::utils::remove_file(&path)?;
180180
}
181181
Ok(())
182182
}
183183

184-
fn copy_source_to(&self, workspace: &Workspace, dest: &Path) -> Result<()> {
184+
fn copy_source_to(&self, workspace: &Workspace, dest: &Path) -> anyhow::Result<()> {
185185
let cached = self.cache_path(workspace);
186186
let mut file = File::open(cached)?;
187187
let mut tar = Archive::new(GzDecoder::new(BufReader::new(&mut file)));
@@ -216,7 +216,7 @@ impl std::fmt::Display for RegistryCrate {
216216
}
217217
}
218218

219-
fn unpack_without_first_dir<R: Read>(archive: &mut Archive<R>, path: &Path) -> Result<()> {
219+
fn unpack_without_first_dir<R: Read>(archive: &mut Archive<R>, path: &Path) -> anyhow::Result<()> {
220220
let entries = archive.entries()?;
221221
for entry in entries {
222222
let mut entry = entry?;

src/native/windows.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use super::CurrentUser;
22
use crate::cmd::KillFailedError;
3-
use anyhow::{anyhow, Result};
3+
use anyhow::anyhow;
44
use std::fs::File;
55
use std::path::Path;
66
use windows_sys::Win32::Foundation::CloseHandle;
77
use windows_sys::Win32::System::Threading::{OpenProcess, TerminateProcess, PROCESS_TERMINATE};
88

9-
pub(crate) fn kill_process(id: u32) -> Result<(), KillFailedError> {
9+
pub(crate) fn kill_process(id: u32) -> anyhow::Result<(), KillFailedError> {
1010
let error = Err(KillFailedError { pid: id });
1111

1212
unsafe {
@@ -29,22 +29,22 @@ pub(crate) fn current_user() -> Option<CurrentUser> {
2929
None
3030
}
3131

32-
fn path_ends_in_exe<P: AsRef<Path>>(path: P) -> Result<bool> {
32+
fn path_ends_in_exe<P: AsRef<Path>>(path: P) -> anyhow::Result<bool> {
3333
path.as_ref()
3434
.extension()
3535
.ok_or_else(|| anyhow!("Unable to get `Path` extension"))
3636
.map(|ext| ext == "exe")
3737
}
3838

3939
/// Check that the file exists and has `.exe` as its extension.
40-
pub(crate) fn is_executable<P: AsRef<Path>>(path: P) -> Result<bool> {
40+
pub(crate) fn is_executable<P: AsRef<Path>>(path: P) -> anyhow::Result<bool> {
4141
let path = path.as_ref();
4242
File::open(path)
4343
.map_err(Into::into)
4444
.and_then(|_| path_ends_in_exe(path))
4545
}
4646

47-
pub(crate) fn make_executable<P: AsRef<Path>>(path: P) -> Result<()> {
47+
pub(crate) fn make_executable<P: AsRef<Path>>(path: P) -> anyhow::Result<()> {
4848
if is_executable(path)? {
4949
Ok(())
5050
} else {

src/prepare.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::cmd::Command;
22
use crate::{build::CratePatch, Crate, Toolchain, Workspace};
3-
use anyhow::{Context as _, Result};
3+
use anyhow::Context as _;
44
use log::info;
55
use std::path::Path;
66
use toml::{
@@ -33,7 +33,7 @@ impl<'a> Prepare<'a> {
3333
}
3434
}
3535

36-
pub(crate) fn prepare(&mut self) -> Result<()> {
36+
pub(crate) fn prepare(&mut self) -> anyhow::Result<()> {
3737
self.krate.copy_source_to(self.workspace, self.source_dir)?;
3838
self.validate_manifest()?;
3939
self.remove_override_files()?;
@@ -44,7 +44,7 @@ impl<'a> Prepare<'a> {
4444
Ok(())
4545
}
4646

47-
fn validate_manifest(&self) -> Result<()> {
47+
fn validate_manifest(&self) -> anyhow::Result<()> {
4848
info!(
4949
"validating manifest of {} on toolchain {}",
5050
self.krate, self.toolchain
@@ -67,7 +67,7 @@ impl<'a> Prepare<'a> {
6767
Ok(())
6868
}
6969

70-
fn remove_override_files(&self) -> Result<()> {
70+
fn remove_override_files(&self) -> anyhow::Result<()> {
7171
let paths = [
7272
&Path::new(".cargo").join("config"),
7373
&Path::new(".cargo").join("config.toml"),
@@ -84,15 +84,15 @@ impl<'a> Prepare<'a> {
8484
Ok(())
8585
}
8686

87-
fn tweak_toml(&self) -> Result<()> {
87+
fn tweak_toml(&self) -> anyhow::Result<()> {
8888
let path = self.source_dir.join("Cargo.toml");
8989
let mut tweaker = TomlTweaker::new(self.krate, &path, &self.patches)?;
9090
tweaker.tweak();
9191
tweaker.save(&path)?;
9292
Ok(())
9393
}
9494

95-
fn capture_lockfile(&mut self) -> Result<()> {
95+
fn capture_lockfile(&mut self) -> anyhow::Result<()> {
9696
if self.source_dir.join("Cargo.lock").exists() {
9797
info!(
9898
"crate {} already has a lockfile, it will not be regenerated",
@@ -137,7 +137,7 @@ impl<'a> Prepare<'a> {
137137
Ok(())
138138
}
139139

140-
fn fetch_deps(&mut self) -> Result<()> {
140+
fn fetch_deps(&mut self) -> anyhow::Result<()> {
141141
fetch_deps(self.workspace, self.toolchain, self.source_dir, &[])
142142
}
143143
}
@@ -147,7 +147,7 @@ pub(crate) fn fetch_deps(
147147
toolchain: &Toolchain,
148148
source_dir: &Path,
149149
fetch_build_std_targets: &[&str],
150-
) -> Result<()> {
150+
) -> anyhow::Result<()> {
151151
let mut missing_deps = false;
152152
let mut cmd = Command::new(workspace, toolchain.cargo())
153153
.args(&["fetch", "--manifest-path", "Cargo.toml"])
@@ -183,7 +183,11 @@ struct TomlTweaker<'a> {
183183
}
184184

185185
impl<'a> TomlTweaker<'a> {
186-
pub fn new(krate: &'a Crate, cargo_toml: &'a Path, patches: &[CratePatch]) -> Result<Self> {
186+
pub fn new(
187+
krate: &'a Crate,
188+
cargo_toml: &'a Path,
189+
patches: &[CratePatch],
190+
) -> anyhow::Result<Self> {
187191
let toml_content =
188192
::std::fs::read_to_string(cargo_toml).context(PrepareError::MissingCargoToml)?;
189193
let table: Table =
@@ -349,7 +353,7 @@ impl<'a> TomlTweaker<'a> {
349353
}
350354
}
351355

352-
pub fn save(self, output_file: &Path) -> Result<()> {
356+
pub fn save(self, output_file: &Path) -> anyhow::Result<()> {
353357
let crate_name = self.krate.to_string();
354358
::std::fs::write(output_file, toml::to_string(&self.table)?.as_bytes())?;
355359
info!(

src/toolchain.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,6 @@ pub(crate) fn list_installed_toolchains(rustup_home: &Path) -> anyhow::Result<Ve
553553
#[cfg(test)]
554554
mod tests {
555555
use super::Toolchain;
556-
use anyhow::Result;
557556

558557
#[test]
559558
fn test_dist_serde_repr() -> anyhow::Result<()> {

src/tools/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ mod binary_crates;
22
mod rustup;
33

44
use crate::workspace::Workspace;
5-
use anyhow::{bail, Result};
5+
use anyhow::bail;
66
use binary_crates::BinaryCrate;
77
use log::info;
88
use rustup::Rustup;
@@ -33,9 +33,9 @@ static INSTALLABLE_TOOLS: &[&dyn Tool] = &[
3333

3434
trait Tool: Send + Sync {
3535
fn name(&self) -> &'static str;
36-
fn is_installed(&self, workspace: &Workspace) -> Result<bool>;
37-
fn install(&self, workspace: &Workspace, fast_install: bool) -> Result<()>;
38-
fn update(&self, workspace: &Workspace, fast_install: bool) -> Result<()>;
36+
fn is_installed(&self, workspace: &Workspace) -> anyhow::Result<bool>;
37+
fn install(&self, workspace: &Workspace, fast_install: bool) -> anyhow::Result<()>;
38+
fn update(&self, workspace: &Workspace, fast_install: bool) -> anyhow::Result<()>;
3939

4040
fn binary_path(&self, workspace: &Workspace) -> PathBuf {
4141
crate::utils::normalize_path(&workspace.cargo_home().join("bin").join(format!(
@@ -46,7 +46,7 @@ trait Tool: Send + Sync {
4646
}
4747
}
4848

49-
pub(crate) fn install(workspace: &Workspace, fast_install: bool) -> Result<()> {
49+
pub(crate) fn install(workspace: &Workspace, fast_install: bool) -> anyhow::Result<()> {
5050
for tool in INSTALLABLE_TOOLS {
5151
if tool.is_installed(workspace)? {
5252
info!("tool {} is installed, trying to update it", tool.name());

src/tools/rustup.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::cmd::{Binary, Command, Runnable};
22
use crate::toolchain::MAIN_TOOLCHAIN_NAME;
33
use crate::tools::{Tool, RUSTUP};
44
use crate::workspace::Workspace;
5-
use anyhow::{Context as _, Result};
5+
use anyhow::Context as _;
66
use std::env::consts::EXE_SUFFIX;
77
use std::fs::{self, File};
88
use std::io;
@@ -23,7 +23,7 @@ impl Tool for Rustup {
2323
"rustup"
2424
}
2525

26-
fn is_installed(&self, workspace: &Workspace) -> Result<bool> {
26+
fn is_installed(&self, workspace: &Workspace) -> anyhow::Result<bool> {
2727
let path = self.binary_path(workspace);
2828
if !path.is_file() {
2929
return Ok(false);
@@ -32,7 +32,7 @@ impl Tool for Rustup {
3232
crate::native::is_executable(path)
3333
}
3434

35-
fn install(&self, workspace: &Workspace, _fast_install: bool) -> Result<()> {
35+
fn install(&self, workspace: &Workspace, _fast_install: bool) -> anyhow::Result<()> {
3636
fs::create_dir_all(workspace.cargo_home())?;
3737
fs::create_dir_all(workspace.rustup_home())?;
3838

@@ -73,7 +73,7 @@ impl Tool for Rustup {
7373
Ok(())
7474
}
7575

76-
fn update(&self, workspace: &Workspace, _fast_install: bool) -> Result<()> {
76+
fn update(&self, workspace: &Workspace, _fast_install: bool) -> anyhow::Result<()> {
7777
Command::new(workspace, &RUSTUP)
7878
.args(&["self", "update"])
7979
.run()

src/workspace.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::build::BuildDirectory;
22
use crate::cmd::{Command, SandboxImage};
33
use crate::inside_docker::CurrentContainer;
44
use crate::Toolchain;
5-
use anyhow::{Context as _, Result};
5+
use anyhow::Context as _;
66
use log::info;
77
use std::fs;
88
use std::path::{Path, PathBuf};
@@ -135,7 +135,7 @@ impl WorkspaceBuilder {
135135

136136
/// Initialize the workspace. This will create all the necessary local files and fetch the rest from the network. It's
137137
/// not unexpected for this method to take minutes to run on slower network connections.
138-
pub fn init(self) -> Result<Workspace> {
138+
pub fn init(self) -> anyhow::Result<Workspace> {
139139
std::fs::create_dir_all(&self.path).with_context(|| {
140140
format!(
141141
"failed to create workspace directory: {}",
@@ -207,7 +207,7 @@ impl Workspace {
207207
}
208208

209209
/// Remove all the contents of all the build directories, freeing disk space.
210-
pub fn purge_all_build_dirs(&self) -> Result<()> {
210+
pub fn purge_all_build_dirs(&self) -> anyhow::Result<()> {
211211
let dir = self.builds_dir();
212212
if dir.exists() {
213213
crate::utils::remove_dir_all(&dir)?;
@@ -216,7 +216,7 @@ impl Workspace {
216216
}
217217

218218
/// Remove all the contents of the caches in the workspace, freeing disk space.
219-
pub fn purge_all_caches(&self) -> Result<()> {
219+
pub fn purge_all_caches(&self) -> anyhow::Result<()> {
220220
let mut paths = vec![
221221
self.cache_dir(),
222222
self.cargo_home().join("git"),
@@ -265,7 +265,7 @@ impl Workspace {
265265
/// # Ok(())
266266
/// # }
267267
/// ```
268-
pub fn installed_toolchains(&self) -> Result<Vec<Toolchain>> {
268+
pub fn installed_toolchains(&self) -> anyhow::Result<Vec<Toolchain>> {
269269
crate::toolchain::list_installed_toolchains(&self.rustup_home())
270270
}
271271

@@ -313,7 +313,7 @@ impl Workspace {
313313
&self.inner.rustup_profile
314314
}
315315

316-
fn init(&self, fast_init: bool) -> Result<()> {
316+
fn init(&self, fast_init: bool) -> anyhow::Result<()> {
317317
info!("installing tools required by rustwide");
318318
crate::tools::install(self, fast_init)?;
319319
if !self.fetch_registry_index_during_builds() {
@@ -324,7 +324,7 @@ impl Workspace {
324324
}
325325

326326
#[allow(clippy::unnecessary_wraps)] // hopefully we could actually catch the error here at some point
327-
fn update_cratesio_registry(&self) -> Result<()> {
327+
fn update_cratesio_registry(&self) -> anyhow::Result<()> {
328328
// This nop cargo command is to update the registry so we don't have to do it for each
329329
// crate. using `install` is a temporary solution until
330330
// https://github.com/rust-lang/cargo/pull/5961 is ready

0 commit comments

Comments
 (0)