Skip to content

Commit

Permalink
Make the error codes and messages more descriptive
Browse files Browse the repository at this point in the history
  • Loading branch information
Bill-hbrhbr committed Jul 19, 2024
1 parent 876e9f9 commit fd6122a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 35 deletions.
28 changes: 16 additions & 12 deletions components/core/src/clp/regex_utils/ErrorCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,28 @@ auto ErrorCodeCategory::message(int ev) const -> string {
case ErrorCode::IllegalState:
return "Unrecognized state.";

case ErrorCode::Star:
return "Failed to translate due to metachar `*` (zero or more occurences).";
case ErrorCode::UntranslatableStar:
return "Unable to express regex quantifier `*` in wildcard, which repeats a token for "
"zero or more occurences, unless it is combined with a wildcard `.`";

case ErrorCode::Plus:
return "Failed to translate due to metachar `+` (one or more occurences).";
case ErrorCode::UntranslatablePlus:
return "Unable to express regex quantifier `+` in wildcard, which repeats a token for "
"one or more occurences, unless it is combined with a wildcard `.`";

case ErrorCode::Question:
return "Currently does not support returning a list of wildcard translations. The "
"metachar `?` (lazy match) may be supported in the future.";
case ErrorCode::UnsupportedQuestionMark:
return "Unable to express regex quantifier `?` in wildcard, which makes the preceding "
"token optional, unless the translator supports returning a list of possible "
"wildcard translations.";

case ErrorCode::Pipe:
return "Currently does not support returning a list of wildcard translations. The "
"regex OR condition feature may be supported in the future.";
case ErrorCode::UnsupportedPipe:
return "Unable to express regex OR `|` in wildcard, which allows the query string to "
"match a single token out of a series of options, unless the translator "
"supports returning a list of possible wildcard translations.";

case ErrorCode::Caret:
case ErrorCode::IllegalCaret:
return "Failed to translate due to start anchor `^` in the middle of the string.";

case ErrorCode::Dollar:
case ErrorCode::IllegalDollarSign:
return "Failed to translate due to end anchor `$` in the middle of the string.";

case ErrorCode::UnmatchedParenthesis:
Expand Down
17 changes: 6 additions & 11 deletions components/core/src/clp/regex_utils/ErrorCode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,13 @@ namespace clp::regex_utils {
enum class ErrorCode : uint8_t {
Success = 0,
IllegalState,
Star,
Plus,
Question,
Pipe,
Caret,
Dollar,
DisallowedEscapeSequence,
UntranslatableStar,
UntranslatablePlus,
UnsupportedQuestionMark,
UnsupportedPipe,
IllegalCaret,
IllegalDollarSign,
UnmatchedParenthesis,
UnsupportedCharsets,
IncompleteCharsetStructure,
UnsupportedQuantifier,
TokenUnquantifiable,
};

/**
Expand Down
12 changes: 6 additions & 6 deletions components/core/src/clp/regex_utils/regex_translation_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,15 @@ auto normal_state_transition(
state.set_next_state(TranslatorState::RegexPatternState::END);
break;
case cRegexZeroOrMore:
return ErrorCode::Star;
return ErrorCode::UntranslatableStar;
case cRegexOneOrMore:
return ErrorCode::Plus;
return ErrorCode::UntranslatablePlus;
case cRegexZeroOrOne:
return ErrorCode::Question;
return ErrorCode::UnsupportedQuestionMark;
case '|':
return ErrorCode::Pipe;
return ErrorCode::UnsupportedPipe;
case cRegexStartAnchor:
return ErrorCode::Caret;
return ErrorCode::IllegalCaret;
case ')':
return ErrorCode::UnmatchedParenthesis;
default:
Expand Down Expand Up @@ -209,7 +209,7 @@ auto end_state_transition(
[[maybe_unused]] RegexToWildcardTranslatorConfig const& config
) -> error_code {
if (cRegexEndAnchor != *it) {
return ErrorCode::Dollar;
return ErrorCode::IllegalDollarSign;
}
return ErrorCode::Success;
}
Expand Down
12 changes: 6 additions & 6 deletions components/core/tests/test-regex_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ TEST_CASE("regex_to_wildcard", "[regex_utils][regex_to_wildcard]") {
REQUIRE((regex_to_wildcard(". xyz .+ zyx .*").value() == "? xyz ?* zyx *"));

// Test unescaped meta characters
REQUIRE((regex_to_wildcard(".? xyz .* zyx .").error() == ErrorCode::Question));
REQUIRE((regex_to_wildcard(". xyz .** zyx .").error() == ErrorCode::Star));
REQUIRE((regex_to_wildcard(". xyz .*+ zyx .").error() == ErrorCode::Plus));
REQUIRE((regex_to_wildcard(". xyz |.* zyx .").error() == ErrorCode::Pipe));
REQUIRE((regex_to_wildcard(". xyz ^.* zyx .").error() == ErrorCode::Caret));
REQUIRE((regex_to_wildcard(".? xyz .* zyx .").error() == ErrorCode::UnsupportedQuestionMark));
REQUIRE((regex_to_wildcard(". xyz .** zyx .").error() == ErrorCode::UntranslatableStar));
REQUIRE((regex_to_wildcard(". xyz .*+ zyx .").error() == ErrorCode::UntranslatablePlus));
REQUIRE((regex_to_wildcard(". xyz |.* zyx .").error() == ErrorCode::UnsupportedPipe));
REQUIRE((regex_to_wildcard(". xyz ^.* zyx .").error() == ErrorCode::IllegalCaret));
}

// Test anchors and prefix/suffix wildcards
Expand All @@ -34,5 +34,5 @@ TEST_CASE("regex_to_wildcard_anchor_config", "[regex_utils][regex_to_wildcard][a
REQUIRE((regex_to_wildcard("xyz", config).value() == "*xyz*"));
REQUIRE((regex_to_wildcard("xyz$$", config).value() == "*xyz"));

REQUIRE((regex_to_wildcard("xyz$zyx$", config).error() == ErrorCode::Dollar));
REQUIRE((regex_to_wildcard("xyz$zyx$", config).error() == ErrorCode::IllegalDollarSign));
}

0 comments on commit fd6122a

Please sign in to comment.