Skip to content

Commit a1dc47f

Browse files
committed
Create a ClapError alias
1 parent 68a9bae commit a1dc47f

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

mithril-client-cli/src/commands/deprecation.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use clap::{
33
error::{ContextKind, ContextValue},
44
};
55

6+
use crate::ClapError;
7+
68
/// Stores the deprecated command name and the new command name to use.
79
#[derive(Clone)]
810
pub struct DeprecatedCommand {
@@ -25,7 +27,7 @@ pub struct Deprecation;
2527

2628
impl Deprecation {
2729
fn find_deprecated_command(
28-
error: &clap::error::Error,
30+
error: &ClapError,
2931
deprecated_commands: Vec<DeprecatedCommand>,
3032
) -> Option<DeprecatedCommand> {
3133
if let Some(context_value) = error.get(ContextKind::InvalidSubcommand) {
@@ -40,11 +42,11 @@ impl Deprecation {
4042

4143
/// Modify result to add information on deprecated commands.
4244
pub fn handle_deprecated_commands<A>(
43-
matches_result: Result<A, clap::error::Error>,
45+
matches_result: Result<A, ClapError>,
4446
styles: Styles,
4547
deprecated_commands: Vec<DeprecatedCommand>,
46-
) -> Result<A, clap::error::Error> {
47-
matches_result.map_err(|mut e: clap::error::Error| {
48+
) -> Result<A, ClapError> {
49+
matches_result.map_err(|mut e: ClapError| {
4850
if let Some(deprecated_command) = Self::find_deprecated_command(&e, deprecated_commands)
4951
{
5052
let message = format!(
@@ -83,9 +85,9 @@ mod tests {
8385

8486
#[test]
8587
fn invalid_sub_command_message_for_a_non_deprecated_command_is_not_modified() {
86-
fn build_error() -> Result<MyCommand, clap::error::Error> {
87-
let mut e = clap::error::Error::new(ErrorKind::InvalidSubcommand)
88-
.with_cmd(&MyCommand::command());
88+
fn build_error() -> Result<MyCommand, ClapError> {
89+
let mut e =
90+
ClapError::new(ErrorKind::InvalidSubcommand).with_cmd(&MyCommand::command());
8991

9092
e.insert(
9193
ContextKind::InvalidSubcommand,
@@ -109,15 +111,14 @@ mod tests {
109111

110112
#[test]
111113
fn replace_error_message_on_deprecated_commands_and_show_the_new_command() {
112-
let mut e = clap::error::Error::new(clap::error::ErrorKind::InvalidSubcommand)
113-
.with_cmd(&MyCommand::command());
114+
let mut e = ClapError::new(ErrorKind::InvalidSubcommand).with_cmd(&MyCommand::command());
114115
e.insert(
115116
ContextKind::InvalidSubcommand,
116117
ContextValue::String("old_command".to_string()),
117118
);
118119

119120
let result = Deprecation::handle_deprecated_commands(
120-
Err(e) as Result<MyCommand, clap::error::Error>,
121+
Err(e) as Result<MyCommand, ClapError>,
121122
Styles::plain(),
122123
vec![DeprecatedCommand::new("old_command", "new_command")],
123124
);

mithril-client-cli/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@
1010
pub mod commands;
1111
mod configuration;
1212
mod utils;
13+
14+
/// Error Clap
15+
pub type ClapError = clap::error::Error;

mithril-client-cli/src/main.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ use std::sync::Arc;
1212
use std::{fs::File, path::PathBuf};
1313

1414
use mithril_client::MithrilResult;
15-
use mithril_client_cli::commands::{DeprecatedCommand, Deprecation};
1615
use mithril_doc::{Documenter, GenerateDocCommands, StructDoc};
1716

1817
use mithril_client_cli::commands::{
1918
cardano_db::CardanoDbCommands, cardano_transaction::CardanoTransactionCommands,
20-
mithril_stake_distribution::MithrilStakeDistributionCommands,
19+
mithril_stake_distribution::MithrilStakeDistributionCommands, DeprecatedCommand, Deprecation,
2120
};
21+
use mithril_client_cli::ClapError;
2222

2323
enum LogOutputType {
2424
StdErr,
@@ -141,7 +141,7 @@ impl Args {
141141
}
142142

143143
fn parse_with_decorator(
144-
decorator: &dyn Fn(Result<Self, clap::error::Error>) -> Result<Self, clap::error::Error>,
144+
decorator: &dyn Fn(Result<Self, ClapError>) -> Result<Self, ClapError>,
145145
) -> Self {
146146
let result = decorator(Self::try_parse());
147147
match result {
@@ -151,8 +151,8 @@ impl Args {
151151
}
152152

153153
fn handle_deprecated_decorator(
154-
args_result: Result<Self, clap::error::Error>,
155-
) -> Result<Self, clap::error::Error> {
154+
args_result: Result<Self, ClapError>,
155+
) -> Result<Self, ClapError> {
156156
let styles = Args::command().get_styles().clone();
157157
Deprecation::handle_deprecated_commands(
158158
args_result,
@@ -228,7 +228,7 @@ impl ArtifactCommands {
228228
#[tokio::main]
229229
async fn main() -> MithrilResult<()> {
230230
// Load args
231-
let args = Args::parse_with_decorator(&|result: Result<Args, clap::error::Error>| {
231+
let args = Args::parse_with_decorator(&|result: Result<Args, ClapError>| {
232232
Args::handle_deprecated_decorator(result)
233233
});
234234
let _guard = slog_scope::set_global_logger(args.build_logger()?);

0 commit comments

Comments
 (0)