Mercurial > public > mercurial-scm > hg
diff rust/rhg/src/error.rs @ 46797:bcdcb4423ae3
rhg: Add more conversions between error types
This allows using the `?` operator in more places, as the next commit does.
Differential Revision: https://phab.mercurial-scm.org/D10238
author | Simon Sapin <simon.sapin@octobus.net> |
---|---|
date | Fri, 19 Mar 2021 13:18:53 +0100 |
parents | b1f2c2b336ec |
children | 821929d59e01 |
line wrap: on
line diff
--- a/rust/rhg/src/error.rs Fri Mar 19 23:51:46 2021 +0100 +++ b/rust/rhg/src/error.rs Fri Mar 19 13:18:53 2021 +0100 @@ -2,11 +2,12 @@ use crate::ui::UiError; use crate::NoRepoInCwdError; use format_bytes::format_bytes; -use hg::config::{ConfigError, ConfigParseError}; +use hg::config::{ConfigError, ConfigParseError, ConfigValueParseError}; use hg::errors::HgError; use hg::repo::RepoError; use hg::revlog::revlog::RevlogError; use hg::utils::files::get_bytes_from_path; +use hg::{DirstateError, DirstateMapError, StatusError}; use std::convert::From; /// The kind of command error @@ -61,6 +62,12 @@ } } +impl From<ConfigValueParseError> for CommandError { + fn from(error: ConfigValueParseError) -> Self { + CommandError::abort(error.to_string()) + } +} + impl From<UiError> for CommandError { fn from(_error: UiError) -> Self { // If we already failed writing to stdout or stderr, @@ -144,3 +151,24 @@ } } } + +impl From<StatusError> for CommandError { + fn from(error: StatusError) -> Self { + CommandError::abort(format!("{}", error)) + } +} + +impl From<DirstateMapError> for CommandError { + fn from(error: DirstateMapError) -> Self { + CommandError::abort(format!("{}", error)) + } +} + +impl From<DirstateError> for CommandError { + fn from(error: DirstateError) -> Self { + match error { + DirstateError::Common(error) => error.into(), + DirstateError::Map(error) => error.into(), + } + } +}