From 359a703f11f5a50215e01aa5a5d9c8b1215618cd Mon Sep 17 00:00:00 2001 From: Jessica Black Date: Tue, 31 Oct 2023 19:01:29 -0700 Subject: [PATCH 1/2] Upgrade `error_stack` --- build.rs | 10 +++++----- src/ext/error_stack.rs | 16 +++++++++------- src/ext/io.rs | 4 ++-- src/ext/io/sync.rs | 6 +++--- src/fossa_cli.rs | 14 +++++--------- 5 files changed, 24 insertions(+), 26 deletions(-) diff --git a/build.rs b/build.rs index b86f83cf..f18ef870 100644 --- a/build.rs +++ b/build.rs @@ -1,5 +1,5 @@ use error_stack::fmt::HookContext; -use error_stack::{IntoReport, Report, Result, ResultExt}; +use error_stack::{Report, Result, ResultExt}; use std::env; use std::fs; use std::path::Path; @@ -49,7 +49,7 @@ fn main() -> Result<(), Error> { fn env_var(var: &str) -> Result { env::var(var) - .into_report() + .map_err(Report::from) .change_context_lazy(|| Error::CargoEnv(var.to_owned())) .attach(Help("ensure that this program is running in a Cargo build")) } @@ -57,7 +57,7 @@ fn env_var(var: &str) -> Result { fn write_file String>(root: &Path, name: &str, generator: F) -> Result<(), Error> { let target = root.join(name); fs::write(&target, generator()) - .into_report() + .map_err(Report::from) .change_context_lazy(|| Error::WriteFile(target)) .attach(Help( "ensure that the build is not running on a read-only file system", @@ -97,11 +97,11 @@ fn generate_cargo_build_vars() -> Result<(), Error> { let output = Command::new("git") .args(["rev-parse", "HEAD"]) .output() - .into_report() + .map_err(Report::from) .change_context(Error::CargoBuildVars)?; let git_hash = String::from_utf8(output.stdout) - .into_report() + .map_err(Report::from) .change_context(Error::CargoBuildVars)?; println!("cargo:rustc-env=GIT_HASH={}", git_hash); diff --git a/src/ext/error_stack.rs b/src/ext/error_stack.rs index 2b0388ae..8ce79af0 100644 --- a/src/ext/error_stack.rs +++ b/src/ext/error_stack.rs @@ -1,7 +1,7 @@ //! Extensions to `error_stack`. use colored::Colorize; -use error_stack::{Context, IntoReport, Report, ResultExt}; +use error_stack::{Context, Report, ResultExt}; use crate::doc; @@ -40,7 +40,7 @@ pub trait ErrorHelper { fn help_lazy, F: FnOnce() -> S>(self, helper: F) -> Self; } -impl ErrorHelper for error_stack::Result { +impl ErrorHelper for error_stack::Result { fn help>(self, help_text: S) -> Self { let help = help_literal(); let help_text = help_text.as_ref(); @@ -84,7 +84,7 @@ pub trait ErrorDocReference { fn documentation_lazy, F: FnOnce() -> S>(self, url_generator: F) -> Self; } -impl ErrorDocReference for error_stack::Result { +impl ErrorDocReference for error_stack::Result { fn documentation>(self, url: S) -> Self { let doc = documentation_literal(); let doc_url = url.as_ref(); @@ -125,7 +125,7 @@ pub trait DescribeContext { fn describe_lazy, F: FnOnce() -> S>(self, describer: F) -> Self; } -impl DescribeContext for error_stack::Result { +impl DescribeContext for error_stack::Result { fn describe>(self, description: S) -> Self { let context = describe_literal(); let description = description.as_ref(); @@ -160,7 +160,7 @@ pub trait FatalErrorReport { fn request_support(self) -> Self; } -impl FatalErrorReport for error_stack::Result { +impl FatalErrorReport for error_stack::Result { fn request_support(self) -> Self { let support = support_literal(); let support_url = doc::link::fossa_support(); @@ -201,11 +201,13 @@ where #[track_caller] fn context(self, context: C) -> Result> { - self.into_report().change_context(context) + self.map_err(Report::from) + .map_err(|err| err.change_context(context)) } #[track_caller] fn context_lazy C>(self, context: F) -> Result> { - self.into_report().change_context_lazy(context) + self.map_err(Report::from) + .map_err(|err| err.change_context(context())) } } diff --git a/src/ext/io.rs b/src/ext/io.rs index 1d9f93ee..64066210 100644 --- a/src/ext/io.rs +++ b/src/ext/io.rs @@ -25,7 +25,7 @@ use std::{ path::{Path, PathBuf}, }; -use error_stack::{report, IntoReport, Report, ResultExt}; +use error_stack::{report, Report, ResultExt}; use tokio::{fs::File, task}; use crate::{ @@ -195,7 +195,7 @@ where Report: From, F: FnOnce() -> Result + Send + 'static, { - spawn_blocking(|| work().into_report()).await + spawn_blocking(|| work().map_err(Report::from)).await } /// Run the provided blocking closure in the background. diff --git a/src/ext/io/sync.rs b/src/ext/io/sync.rs index 966e80a0..1ed4204f 100644 --- a/src/ext/io/sync.rs +++ b/src/ext/io/sync.rs @@ -27,7 +27,7 @@ use std::{ path::{Path, PathBuf}, }; -use error_stack::{IntoReport, Report, ResultExt}; +use error_stack::{Report, ResultExt}; use itertools::Itertools; use libflate::gzip; use once_cell::sync::OnceCell; @@ -208,7 +208,7 @@ pub fn validate_file(path: PathBuf) -> Result> { } else { Error::NotRegularFile .wrap_err() - .into_report() + .map_err(Report::from) .attach_printable_lazy(|| format!("validate file: '{}'", path.display())) } } @@ -243,7 +243,7 @@ pub fn home_dir() -> Result<&'static PathBuf, Report> { static LAZY: OnceCell = OnceCell::new(); LAZY.get_or_try_init(|| { debug!("Performing uncached lookup of home directory"); - dirs::home_dir().ok_or(Error::LocateUserHome).into_report() + dirs::home_dir().ok_or(Error::LocateUserHome).map_err(Report::from) .describe("on macOS and Linux, this uses the $HOME environment variable or the system call 'getpwuid_r'") .describe("on Windows, this uses the Windows API call 'SHGetKnownFolderPath'") .describe("this is a very rare condition, and it's not likely that Broker will be able to resolve this issue") diff --git a/src/fossa_cli.rs b/src/fossa_cli.rs index d97f78ec..3fc50bed 100644 --- a/src/fossa_cli.rs +++ b/src/fossa_cli.rs @@ -2,7 +2,7 @@ use bytes::Bytes; use cached::proc_macro::cached; -use error_stack::{bail, report, IntoReport}; +use error_stack::{bail, report}; use error_stack::{Result, ResultExt}; use futures::future::try_join3; use indoc::formatdoc; @@ -546,8 +546,7 @@ async fn download_from_github(version: &str) -> Result, Error> { .get(&download_url) .send() .await - .into_report() - .change_context(Error::Download) + .context(Error::Download) .help_lazy(|| formatdoc!{" Try downloading FOSSA CLI from '{download_url}' to determine if this is an issue with the local network. You also may be able to work around this issue by using the installation script for FOSSA CLI, @@ -558,8 +557,7 @@ async fn download_from_github(version: &str) -> Result, Error> { let content = response .bytes() .await - .into_report() - .change_context(Error::Download) + .context(Error::Download) .help_lazy(|| formatdoc!{" Try downloading FOSSA CLI from '{download_url}' to determine if this is an issue with the local network. You also may be able to work around this issue by using the installation script for FOSSA CLI, @@ -604,8 +602,7 @@ where .write(true) .mode(0o770) .open(final_path) - .into_report() - .change_context_lazy(|| Error::FinalCopy(final_path_string.clone()))?; + .context_lazy(|| Error::FinalCopy(final_path_string.clone()))?; std::io::copy(&mut zip_file, &mut final_file) .context_lazy(|| Error::FinalCopy(final_path_string.clone())) @@ -623,8 +620,7 @@ where .create(true) .write(true) .open(final_path) - .into_report() - .change_context_lazy(|| Error::FinalCopy(final_path_string.clone()))?; + .context_lazy(|| Error::FinalCopy(final_path_string.clone()))?; std::io::copy(&mut zip_file, &mut final_file) .context_lazy(|| Error::FinalCopy(final_path_string.clone())) From 77c1d89e2897cbac5237544c605e97ea8eb3d07a Mon Sep 17 00:00:00 2001 From: Jessica Black Date: Thu, 9 Nov 2023 16:25:57 -0800 Subject: [PATCH 2/2] Fix lints --- src/ext/command.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/ext/command.rs b/src/ext/command.rs index d9c15894..613ca4a9 100644 --- a/src/ext/command.rs +++ b/src/ext/command.rs @@ -3,7 +3,6 @@ use std::{ ffi::{OsStr, OsString}, fmt::Display, - ops::Deref, path::PathBuf, process::{ExitStatus, Stdio}, }; @@ -729,7 +728,7 @@ where T: CommandDescriber, { fn describe(&self) -> Description { - self.deref().deref().describe() + (*self).describe() } } @@ -814,15 +813,15 @@ where T: OutputProvider, { fn stdout(&self) -> Vec { - self.deref().deref().stdout() + (*self).stdout() } fn stderr(&self) -> Vec { - self.deref().deref().stderr() + (*self).stderr() } fn status(&self) -> ExitStatus { - self.deref().deref().status() + (*self).status() } }