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 #1365 and #1384
  • Loading branch information
epage committed Aug 24, 2022
1 parent d16a531 commit ca8f8c2
Show file tree
Hide file tree
Showing 6 changed files with 637 additions and 382 deletions.
2 changes: 1 addition & 1 deletion src/builder/mod.rs
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
@@ -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
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),
}
}
}

0 comments on commit ca8f8c2

Please sign in to comment.