Skip to content

Commit

Permalink
Merge pull request #4239 from epage/alias
Browse files Browse the repository at this point in the history
fix(error): Use a non-generic Error alias
  • Loading branch information
epage committed Sep 20, 2022
2 parents c26e7fd + 4674e43 commit ba56ac7
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 83 deletions.
45 changes: 4 additions & 41 deletions src/error/format.rs
Expand Up @@ -15,7 +15,7 @@ use crate::output::TAB;
/// Defines how to format an error for displaying to the user
pub trait ErrorFormatter: Sized {
/// Stylize the error for the terminal
fn format_error(error: &crate::Error<Self>) -> StyledStr;
fn format_error(error: &crate::error::Error<Self>) -> StyledStr;
}

/// Report [`ErrorKind`]
Expand All @@ -28,7 +28,7 @@ pub trait ErrorFormatter: Sized {
pub struct KindFormatter;

impl ErrorFormatter for KindFormatter {
fn format_error(error: &crate::Error<Self>) -> StyledStr {
fn format_error(error: &crate::error::Error<Self>) -> StyledStr {
let mut styled = StyledStr::new();
start_error(&mut styled);
if let Some(msg) = error.kind().as_str() {
Expand All @@ -43,51 +43,14 @@ impl ErrorFormatter for KindFormatter {
}
}

/// Dump the error context reported
#[non_exhaustive]
#[cfg(feature = "error-context")]
pub struct RawFormatter;

#[cfg(feature = "error-context")]
impl ErrorFormatter for RawFormatter {
fn format_error(error: &crate::Error<Self>) -> StyledStr {
let mut styled = StyledStr::new();
start_error(&mut styled);
if let Some(msg) = error.kind().as_str() {
styled.none(msg.to_owned());
} else if let Some(source) = error.inner.source.as_ref() {
styled.none(source.to_string());
} else {
styled.none("Unknown cause");
}
styled.none("\n");

if error.context().next().is_some() {
styled.none("\n");
}
for (kind, value) in error.context() {
if let Some(kind) = kind.as_str() {
styled.none(kind);
styled.none(": ");
styled.none(value.to_string());
} else {
styled.none(value.to_string());
}
styled.none("\n");
}

styled
}
}

/// Richly formatted error context
#[non_exhaustive]
#[cfg(feature = "error-context")]
pub struct RichFormatter;

#[cfg(feature = "error-context")]
impl ErrorFormatter for RichFormatter {
fn format_error(error: &crate::Error<Self>) -> StyledStr {
fn format_error(error: &crate::error::Error<Self>) -> StyledStr {
let mut styled = StyledStr::new();
start_error(&mut styled);

Expand Down Expand Up @@ -119,7 +82,7 @@ fn start_error(styled: &mut StyledStr) {

#[must_use]
#[cfg(feature = "error-context")]
fn write_dynamic_context(error: &crate::Error, styled: &mut StyledStr) -> bool {
fn write_dynamic_context(error: &crate::error::Error, styled: &mut StyledStr) -> bool {
match error.kind() {
ErrorKind::ArgumentConflict => {
let invalid_arg = error.get(ContextKind::InvalidArg);
Expand Down
2 changes: 0 additions & 2 deletions src/error/mod.rs
Expand Up @@ -39,8 +39,6 @@ pub use context::ContextKind;
#[cfg(feature = "error-context")]
pub use context::ContextValue;
#[cfg(feature = "error-context")]
pub use format::RawFormatter;
#[cfg(feature = "error-context")]
pub use format::RichFormatter;

#[cfg(not(feature = "error-context"))]
Expand Down
8 changes: 7 additions & 1 deletion src/lib.rs
Expand Up @@ -101,7 +101,6 @@ pub use crate::builder::ArgAction;
pub use crate::builder::Command;
pub use crate::builder::ValueHint;
pub use crate::builder::{Arg, ArgGroup};
pub use crate::error::Error;
pub use crate::parser::ArgMatches;
#[cfg(feature = "color")]
pub use crate::util::color::ColorChoice;
Expand All @@ -110,6 +109,13 @@ pub use crate::util::color::ColorChoice;
pub(crate) use crate::util::color::ColorChoice;
pub use crate::util::Id;

/// Command Line Argument Parser Error
///
/// See [`Command::error`] to create an error.
///
/// [`Command::error`]: crate::Command::error
pub type Error = crate::error::Error<crate::error::DefaultFormatter>;

pub use crate::derive::{Args, CommandFactory, FromArgMatches, Parser, Subcommand, ValueEnum};

#[cfg(feature = "derive")]
Expand Down
40 changes: 1 addition & 39 deletions tests/builder/error.rs
@@ -1,6 +1,6 @@
use super::utils;

use clap::{arg, error::ErrorKind, value_parser, Arg, Command, Error};
use clap::{arg, error::Error, error::ErrorKind, value_parser, Arg, Command};

#[track_caller]
fn assert_error<F: clap::error::ErrorFormatter>(
Expand Down Expand Up @@ -96,25 +96,6 @@ Options:
assert_error(err, expected_kind, MESSAGE, false);
}

#[test]
#[cfg(feature = "error-context")]
fn raw_prints_help() {
let cmd = Command::new("test");
let res = cmd
.try_get_matches_from(["test", "--help"])
.map_err(|e| e.apply::<clap::error::RawFormatter>());
assert!(res.is_err());
let err = res.unwrap_err();
let expected_kind = ErrorKind::DisplayHelp;
static MESSAGE: &str = "\
Usage: test
Options:
-h, --help Print help information
";
assert_error(err, expected_kind, MESSAGE, false);
}

#[test]
fn kind_formats_validation_error() {
let cmd = Command::new("test");
Expand Down Expand Up @@ -147,22 +128,3 @@ For more information try '--help'
";
assert_error(err, expected_kind, MESSAGE, true);
}

#[test]
#[cfg(feature = "error-context")]
fn raw_formats_validation_error() {
let cmd = Command::new("test");
let res = cmd
.try_get_matches_from(["test", "unused"])
.map_err(|e| e.apply::<clap::error::RawFormatter>());
assert!(res.is_err());
let err = res.unwrap_err();
let expected_kind = ErrorKind::UnknownArgument;
static MESSAGE: &str = "\
error: Found an argument which wasn't expected or isn't valid in this context
Invalid Argument: unused
Usage: test
";
assert_error(err, expected_kind, MESSAGE, true);
}

0 comments on commit ba56ac7

Please sign in to comment.