use std::fmt::Display;
pub type CargoResult<T> = anyhow::Result<T>;
pub type Error = anyhow::Error;
pub use anyhow::Context;
pub type CliResult = Result<(), CliError>;
#[derive(Debug)]
pub struct CliError {
pub error: Option<anyhow::Error>,
pub exit_code: i32,
}
impl CliError {
pub fn new(error: anyhow::Error, code: i32) -> CliError {
CliError {
error: Some(error),
exit_code: code,
}
}
pub fn code(code: i32) -> CliError {
CliError {
error: None,
exit_code: code,
}
}
}
impl From<anyhow::Error> for CliError {
fn from(err: anyhow::Error) -> CliError {
CliError::new(err, 101)
}
}
impl From<clap::Error> for CliError {
fn from(err: clap::Error) -> CliError {
#[allow(clippy::bool_to_int_with_if)]
let code = if err.use_stderr() { 1 } else { 0 };
CliError::new(err.into(), code)
}
}
impl From<std::io::Error> for CliError {
fn from(err: std::io::Error) -> CliError {
CliError::new(err.into(), 1)
}
}
pub(crate) fn no_crate_err(name: impl Display) -> Error {
anyhow::format_err!("The crate `{}` could not be found in registry index.", name)
}
pub(crate) fn non_existent_table_err(table: impl Display) -> Error {
anyhow::format_err!("The table `{}` could not be found.", table)
}
pub(crate) fn non_existent_dependency_err(name: impl Display, table: impl Display) -> Error {
anyhow::format_err!(
"The dependency `{}` could not be found in `{}`.",
name,
table,
)
}
pub(crate) fn invalid_cargo_config() -> Error {
anyhow::format_err!("Invalid cargo config")
}
pub(crate) fn unsupported_version_req(req: impl Display) -> Error {
anyhow::format_err!("Support for modifying {} is currently unsupported", req)
}
pub(crate) fn invalid_release_level(actual: impl Display, version: impl Display) -> Error {
anyhow::format_err!("Cannot increment the {} field for {}", actual, version)
}