From c34a2850b30a7e7f3e3086080f3bcde73a74ebb8 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Tue, 15 Nov 2022 00:46:32 +1100 Subject: [PATCH] Optimization: Box variants in Error to reduce size and remove unused variants in `BinstallError` (#530) * Box `HttpError` in `binsalk_downloader::remote::Error::Http` as `HttpError` contains `Url` which are too big. * Box `HttpError` in `BinstallError::Http` same as the previous commit. * Box `TinyTemplateError` in `BinstallError::Template` since `TinyTemplateError` is 56 bytes large where most of the other variants are below 40 bytes large. * Rm unsed variant `BinstallError::VersionUnavailable` * Box `CratesIoApiError` in `BinstallError::CratesIoApi` It is 32 bytes large while other variants are below 40 bytes large. * Improve err msg for `BinstallError::CrateContext` * Rm unused variant `BinstallError::VersionReq` Signed-off-by: Jiahao XU --- crates/binstalk-downloader/src/remote.rs | 4 +-- crates/binstalk/src/drivers/crates_io.rs | 2 +- crates/binstalk/src/errors.rs | 46 +++++++----------------- 3 files changed, 15 insertions(+), 37 deletions(-) diff --git a/crates/binstalk-downloader/src/remote.rs b/crates/binstalk-downloader/src/remote.rs index 1be1d4e4..86313afb 100644 --- a/crates/binstalk-downloader/src/remote.rs +++ b/crates/binstalk-downloader/src/remote.rs @@ -29,7 +29,7 @@ pub enum Error { Reqwest(#[from] reqwest::Error), #[error(transparent)] - Http(HttpError), + Http(Box), } #[derive(Debug, ThisError)] @@ -138,7 +138,7 @@ impl Client { Ok(response) } }) - .map_err(|err| Error::Http(HttpError { method, url, err })) + .map_err(|err| Error::Http(Box::new(HttpError { method, url, err }))) } /// Check if remote exists using `method`. diff --git a/crates/binstalk/src/drivers/crates_io.rs b/crates/binstalk/src/drivers/crates_io.rs index 1847b204..8202d906 100644 --- a/crates/binstalk/src/drivers/crates_io.rs +++ b/crates/binstalk/src/drivers/crates_io.rs @@ -38,7 +38,7 @@ pub async fn fetch_crate_cratesio( .await .map_err(|err| BinstallError::CratesIoApi { crate_name: name.into(), - err, + err: Box::new(err), })?; // Locate matching version diff --git a/crates/binstalk/src/errors.rs b/crates/binstalk/src/errors.rs index 266b6b43..322be894 100644 --- a/crates/binstalk/src/errors.rs +++ b/crates/binstalk/src/errors.rs @@ -9,8 +9,10 @@ use binstalk_downloader::{ remote::{Error as RemoteError, HttpError, ReqwestError}, }; use compact_str::CompactString; +use crates_io_api::Error as CratesIoApiError; use miette::{Diagnostic, Report}; use thiserror::Error; +use tinytemplate::error::Error as TinyTemplateError; use tokio::task; use tracing::{error, warn}; @@ -60,7 +62,7 @@ pub enum BinstallError { /// - Exit: 67 #[error(transparent)] #[diagnostic(severity(error), code(binstall::template))] - Template(#[from] tinytemplate::error::Error), + Template(Box), /// A generic error from our HTTP client, reqwest. /// @@ -81,7 +83,7 @@ pub enum BinstallError { /// - Exit: 69 #[error(transparent)] #[diagnostic(severity(error), code(binstall::http))] - Http(#[from] HttpError), + Http(#[from] Box), /// A subprocess failed. /// @@ -116,7 +118,7 @@ pub enum BinstallError { CratesIoApi { crate_name: CompactString, #[source] - err: crates_io_api::Error, + err: Box, }, /// The override path to the cargo manifest is invalid or cannot be resolved. @@ -158,23 +160,6 @@ pub enum BinstallError { err: semver::Error, }, - /// A version requirement is not valid. - /// - /// This is usually provided via the `--version` option. - /// - /// Note that we use the [`semver`] crate, which parses Cargo version requirement syntax; they - /// may be slightly different from other semver requirements expressions implementations. - /// - /// - Code: `binstall::version::requirement` - /// - Exit: 81 - #[error("version requirement '{req}' is not semver")] - #[diagnostic(severity(error), code(binstall::version::requirement))] - VersionReq { - req: CompactString, - #[source] - err: semver::Error, - }, - /// No available version matches the requirements. /// /// This may be the case when using the `--version` option. @@ -187,17 +172,6 @@ pub enum BinstallError { #[diagnostic(severity(error), code(binstall::version::mismatch))] VersionMismatch { req: semver::VersionReq }, - /// The crates.io API doesn't have manifest metadata for the given version. - /// - /// - Code: `binstall::version::unavailable` - /// - Exit: 83 - #[error("no crate information available for '{crate_name}' version '{v}'")] - #[diagnostic(severity(error), code(binstall::version::unavailable))] - VersionUnavailable { - crate_name: CompactString, - v: semver::Version, - }, - /// The crate@version syntax was used at the same time as the --version option. /// /// You can't do that as it's ambiguous which should apply. @@ -328,7 +302,7 @@ pub enum BinstallError { NoFallbackToCargoInstall, /// A wrapped error providing the context of which crate the error is about. - #[error("for crate {crate_name}")] + #[error("For crate {crate_name}: {error}")] CrateContext { #[source] error: Box, @@ -353,9 +327,7 @@ impl BinstallError { CargoManifestPath => 77, CargoManifest { .. } => 78, VersionParse { .. } => 80, - VersionReq { .. } => 81, VersionMismatch { .. } => 82, - VersionUnavailable { .. } => 83, SuperfluousVersionOption => 84, OverrideOptionUsedWithMultiInstall { .. } => 85, UnspecifiedBinaries => 86, @@ -460,3 +432,9 @@ impl From for BinstallError { } } } + +impl From for BinstallError { + fn from(e: TinyTemplateError) -> Self { + BinstallError::Template(Box::new(e)) + } +}