Skip to content

Commit

Permalink
perf(error): Allow custmizing formatting
Browse files Browse the repository at this point in the history
For now, there isn't much a custom implementation can do.

Going from `Rich` to `Null` drops about 6 KiB from the binary

This is a part of clap-rs#1365 and clap-rs#1384
  • Loading branch information
epage committed Aug 24, 2022
1 parent d16a531 commit ef5f9f9
Show file tree
Hide file tree
Showing 7 changed files with 638 additions and 382 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ MSRV is now 1.60.0

- `Arg::num_args` now accepts ranges, allowing setting both the minimum and maximum number of values per occurrence
- Added `TypedValueParser::map` to make it easier to reuse existing value parsers
- *(error)* `Error::apply` for changing the formatter for dropping binary size
- *(help)* Show `PossibleValue::help` in long help (`--help`) (#3312)

### Fixes
Expand Down
2 changes: 1 addition & 1 deletion src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub use possible_value::PossibleValue;
pub use range::ValueRange;
pub use resettable::IntoResettable;
pub use resettable::Resettable;
pub use styled_str::StyledStr;
pub use value_hint::ValueHint;
pub use value_parser::_AutoValueParser;
pub use value_parser::via_prelude;
Expand All @@ -55,7 +56,6 @@ pub use value_parser::_AnonymousValueParser;

#[allow(unused_imports)]
pub(crate) use self::str::Inner as StrInner;
pub(crate) use self::styled_str::StyledStr;
pub(crate) use action::CountType;
pub(crate) use arg::render_arg_val;
pub(crate) use arg_settings::{ArgFlags, ArgSettings};
6 changes: 4 additions & 2 deletions src/builder/styled_str.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/// Terminal-styling container
#[derive(Clone, Default, Debug, PartialEq, Eq)]
pub(crate) struct StyledStr {
pub struct StyledStr {
pieces: Vec<(Option<Style>, String)>,
}

impl StyledStr {
pub(crate) const fn new() -> Self {
/// Create an empty buffer
pub const fn new() -> Self {
Self { pieces: Vec::new() }
}

Expand Down
41 changes: 41 additions & 0 deletions src/error/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,35 @@ pub enum ContextKind {
Custom,
}

impl ContextKind {
/// End-user description of the error case, where relevant
pub fn as_str(self) -> Option<&'static str> {
match self {
Self::InvalidSubcommand => Some("Invalid Subcommand"),
Self::InvalidArg => Some("Invalid Argument"),
Self::PriorArg => Some("Prior Argument"),
Self::ValidValue => Some("Value Value"),
Self::InvalidValue => Some("Invalid Value"),
Self::ActualNumValues => Some("Actual Number of Values"),
Self::ExpectedNumValues => Some("Expected Number of Values"),
Self::MinValues => Some("Minimum Number of Values"),
Self::SuggestedCommand => Some("Suggested Command"),
Self::SuggestedSubcommand => Some("Suggested Subcommand"),
Self::SuggestedArg => Some("Suggested Argument"),
Self::SuggestedValue => Some("Suggested Value"),
Self::TrailingArg => Some("Trailing Argument"),
Self::Usage => None,
Self::Custom => None,
}
}
}

impl std::fmt::Display for ContextKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.as_str().unwrap_or_default().fmt(f)
}
}

/// A piece of error information
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
Expand All @@ -49,3 +78,15 @@ pub enum ContextValue {
/// A single value
Number(isize),
}

impl std::fmt::Display for ContextValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::None => "".fmt(f),
Self::Bool(v) => v.fmt(f),
Self::String(v) => v.fmt(f),
Self::Strings(v) => v.join(", ").fmt(f),
Self::Number(v) => v.fmt(f),
}
}
}
Loading

0 comments on commit ef5f9f9

Please sign in to comment.