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 <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-11-15 00:46:32 +11:00 committed by GitHub
parent 933c6425ef
commit c34a2850b3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 37 deletions

View file

@ -29,7 +29,7 @@ pub enum Error {
Reqwest(#[from] reqwest::Error), Reqwest(#[from] reqwest::Error),
#[error(transparent)] #[error(transparent)]
Http(HttpError), Http(Box<HttpError>),
} }
#[derive(Debug, ThisError)] #[derive(Debug, ThisError)]
@ -138,7 +138,7 @@ impl Client {
Ok(response) 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`. /// Check if remote exists using `method`.

View file

@ -38,7 +38,7 @@ pub async fn fetch_crate_cratesio(
.await .await
.map_err(|err| BinstallError::CratesIoApi { .map_err(|err| BinstallError::CratesIoApi {
crate_name: name.into(), crate_name: name.into(),
err, err: Box::new(err),
})?; })?;
// Locate matching version // Locate matching version

View file

@ -9,8 +9,10 @@ use binstalk_downloader::{
remote::{Error as RemoteError, HttpError, ReqwestError}, remote::{Error as RemoteError, HttpError, ReqwestError},
}; };
use compact_str::CompactString; use compact_str::CompactString;
use crates_io_api::Error as CratesIoApiError;
use miette::{Diagnostic, Report}; use miette::{Diagnostic, Report};
use thiserror::Error; use thiserror::Error;
use tinytemplate::error::Error as TinyTemplateError;
use tokio::task; use tokio::task;
use tracing::{error, warn}; use tracing::{error, warn};
@ -60,7 +62,7 @@ pub enum BinstallError {
/// - Exit: 67 /// - Exit: 67
#[error(transparent)] #[error(transparent)]
#[diagnostic(severity(error), code(binstall::template))] #[diagnostic(severity(error), code(binstall::template))]
Template(#[from] tinytemplate::error::Error), Template(Box<TinyTemplateError>),
/// A generic error from our HTTP client, reqwest. /// A generic error from our HTTP client, reqwest.
/// ///
@ -81,7 +83,7 @@ pub enum BinstallError {
/// - Exit: 69 /// - Exit: 69
#[error(transparent)] #[error(transparent)]
#[diagnostic(severity(error), code(binstall::http))] #[diagnostic(severity(error), code(binstall::http))]
Http(#[from] HttpError), Http(#[from] Box<HttpError>),
/// A subprocess failed. /// A subprocess failed.
/// ///
@ -116,7 +118,7 @@ pub enum BinstallError {
CratesIoApi { CratesIoApi {
crate_name: CompactString, crate_name: CompactString,
#[source] #[source]
err: crates_io_api::Error, err: Box<CratesIoApiError>,
}, },
/// The override path to the cargo manifest is invalid or cannot be resolved. /// The override path to the cargo manifest is invalid or cannot be resolved.
@ -158,23 +160,6 @@ pub enum BinstallError {
err: semver::Error, 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. /// No available version matches the requirements.
/// ///
/// This may be the case when using the `--version` option. /// This may be the case when using the `--version` option.
@ -187,17 +172,6 @@ pub enum BinstallError {
#[diagnostic(severity(error), code(binstall::version::mismatch))] #[diagnostic(severity(error), code(binstall::version::mismatch))]
VersionMismatch { req: semver::VersionReq }, 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. /// 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. /// You can't do that as it's ambiguous which should apply.
@ -328,7 +302,7 @@ pub enum BinstallError {
NoFallbackToCargoInstall, NoFallbackToCargoInstall,
/// A wrapped error providing the context of which crate the error is about. /// 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 { CrateContext {
#[source] #[source]
error: Box<BinstallError>, error: Box<BinstallError>,
@ -353,9 +327,7 @@ impl BinstallError {
CargoManifestPath => 77, CargoManifestPath => 77,
CargoManifest { .. } => 78, CargoManifest { .. } => 78,
VersionParse { .. } => 80, VersionParse { .. } => 80,
VersionReq { .. } => 81,
VersionMismatch { .. } => 82, VersionMismatch { .. } => 82,
VersionUnavailable { .. } => 83,
SuperfluousVersionOption => 84, SuperfluousVersionOption => 84,
OverrideOptionUsedWithMultiInstall { .. } => 85, OverrideOptionUsedWithMultiInstall { .. } => 85,
UnspecifiedBinaries => 86, UnspecifiedBinaries => 86,
@ -460,3 +432,9 @@ impl From<DownloadError> for BinstallError {
} }
} }
} }
impl From<TinyTemplateError> for BinstallError {
fn from(e: TinyTemplateError) -> Self {
BinstallError::Template(Box::new(e))
}
}