fix(error): Use a non-generic Error alias

`clap::Error::raw` was producing ambiguity errors with a default generic
parameter on `clap::error::Error` (which `clap::Error` is a re-export
of).

I tried making `clap::Error` a type alias with a default generic
parameter but that ran into an ambiguity error with `map_err`.

So I'm going ahead and hard coding `clap::Error`.  We don't expect
people to change this all that often.
This commit is contained in:
Ed Page 2022-09-20 13:37:00 -05:00
parent c26e7fd617
commit 90bcb7f75e
3 changed files with 13 additions and 7 deletions

View file

@ -15,7 +15,7 @@ use crate::output::TAB;
/// Defines how to format an error for displaying to the user /// Defines how to format an error for displaying to the user
pub trait ErrorFormatter: Sized { pub trait ErrorFormatter: Sized {
/// Stylize the error for the terminal /// 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`] /// Report [`ErrorKind`]
@ -28,7 +28,7 @@ pub trait ErrorFormatter: Sized {
pub struct KindFormatter; pub struct KindFormatter;
impl ErrorFormatter for 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(); let mut styled = StyledStr::new();
start_error(&mut styled); start_error(&mut styled);
if let Some(msg) = error.kind().as_str() { if let Some(msg) = error.kind().as_str() {
@ -50,7 +50,7 @@ pub struct RawFormatter;
#[cfg(feature = "error-context")] #[cfg(feature = "error-context")]
impl ErrorFormatter for RawFormatter { impl ErrorFormatter for RawFormatter {
fn format_error(error: &crate::Error<Self>) -> StyledStr { fn format_error(error: &crate::error::Error<Self>) -> StyledStr {
let mut styled = StyledStr::new(); let mut styled = StyledStr::new();
start_error(&mut styled); start_error(&mut styled);
if let Some(msg) = error.kind().as_str() { if let Some(msg) = error.kind().as_str() {
@ -87,7 +87,7 @@ pub struct RichFormatter;
#[cfg(feature = "error-context")] #[cfg(feature = "error-context")]
impl ErrorFormatter for RichFormatter { 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(); let mut styled = StyledStr::new();
start_error(&mut styled); start_error(&mut styled);
@ -119,7 +119,7 @@ fn start_error(styled: &mut StyledStr) {
#[must_use] #[must_use]
#[cfg(feature = "error-context")] #[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() { match error.kind() {
ErrorKind::ArgumentConflict => { ErrorKind::ArgumentConflict => {
let invalid_arg = error.get(ContextKind::InvalidArg); let invalid_arg = error.get(ContextKind::InvalidArg);

View file

@ -101,7 +101,6 @@ pub use crate::builder::ArgAction;
pub use crate::builder::Command; pub use crate::builder::Command;
pub use crate::builder::ValueHint; pub use crate::builder::ValueHint;
pub use crate::builder::{Arg, ArgGroup}; pub use crate::builder::{Arg, ArgGroup};
pub use crate::error::Error;
pub use crate::parser::ArgMatches; pub use crate::parser::ArgMatches;
#[cfg(feature = "color")] #[cfg(feature = "color")]
pub use crate::util::color::ColorChoice; pub use crate::util::color::ColorChoice;
@ -110,6 +109,13 @@ pub use crate::util::color::ColorChoice;
pub(crate) use crate::util::color::ColorChoice; pub(crate) use crate::util::color::ColorChoice;
pub use crate::util::Id; 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}; pub use crate::derive::{Args, CommandFactory, FromArgMatches, Parser, Subcommand, ValueEnum};
#[cfg(feature = "derive")] #[cfg(feature = "derive")]

View file

@ -1,6 +1,6 @@
use super::utils; 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] #[track_caller]
fn assert_error<F: clap::error::ErrorFormatter>( fn assert_error<F: clap::error::ErrorFormatter>(