Skip to content

Commit

Permalink
refactor(error): Move escape suggestion to general suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Oct 13, 2022
1 parent 813060e commit 5275660
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 21 deletions.
3 changes: 0 additions & 3 deletions src/error/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ pub enum ContextKind {
/// Trailing argument
TrailingArg,
/// Potential fix for the user
SuggestedTrailingArg,
/// Potential fix for the user
Suggested,
/// A usage string
Usage,
Expand All @@ -56,7 +54,6 @@ impl ContextKind {
Self::SuggestedArg => Some("Suggested Argument"),
Self::SuggestedValue => Some("Suggested Value"),
Self::TrailingArg => Some("Trailing Argument"),
Self::SuggestedTrailingArg => Some("Suggested Trailing Argument"),
Self::Suggested => Some("Suggested"),
Self::Usage => None,
Self::Custom => None,
Expand Down
15 changes: 0 additions & 15 deletions src/error/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,6 @@ impl ErrorFormatter for RichFormatter {
(_, _) => {}
}

let invalid_arg = error.get(ContextKind::InvalidArg);
if let Some(ContextValue::String(invalid_arg)) = invalid_arg {
let suggested_trailing_arg = error.get(ContextKind::SuggestedTrailingArg);
if suggested_trailing_arg == Some(&ContextValue::Bool(true)) {
styled.none("\n\n");
styled.none(TAB);
styled.none("If you tried to supply '");
styled.warning(invalid_arg);
styled.none("' as a value rather than a flag, use '");
styled.good("-- ");
styled.good(invalid_arg);
styled.none("'");
}
}

let suggestions = error.get(ContextKind::Suggested);
if let Some(ContextValue::StyledStrs(suggestions)) = suggestions {
for suggestion in suggestions {
Expand Down
18 changes: 15 additions & 3 deletions src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,18 @@ impl<F: ErrorFormatter> Error<F> {

#[cfg(feature = "error-context")]
{
let mut suggestions = vec![];
if suggested_trailing_arg {
let mut styled_suggestion = StyledStr::new();
styled_suggestion.none("If you tried to supply '");
styled_suggestion.warning(&arg);
styled_suggestion.none("' as a value rather than a flag, use '");
styled_suggestion.good("-- ");
styled_suggestion.good(&arg);
styled_suggestion.none("'");
suggestions.push(styled_suggestion);
}

err = err
.extend_context_unchecked([(ContextKind::InvalidArg, ContextValue::String(arg))]);
if let Some(usage) = usage {
Expand All @@ -670,10 +682,10 @@ impl<F: ErrorFormatter> Error<F> {
);
}
}
if suggested_trailing_arg {
if !suggestions.is_empty() {
err = err.insert_context_unchecked(
ContextKind::SuggestedTrailingArg,
ContextValue::Bool(true),
ContextKind::Suggested,
ContextValue::StyledStrs(suggestions),
);
}
}
Expand Down

0 comments on commit 5275660

Please sign in to comment.