#ifndef RFL_PATTERNVALIDATOR_HPP_ #define RFL_PATTERNVALIDATOR_HPP_ #include #include #include #include #include #include #include "internal/StringLiteral.hpp" #include "rfl/Literal.hpp" #include "rfl/Result.hpp" namespace rfl { template struct PatternValidator { static Result validate(const std::string& _str) noexcept { const auto check_pattern = [&](const auto& _regex_pattern) -> rfl::Result { if (!std::regex_match(_str, _regex_pattern)) { return rfl::Error("String '" + _str + "' did not match format '" + _name.str() + "': '" + _regex.str() + "'."); } return _str; }; return regex_pattern().and_then(check_pattern); } private: /// Uses a memoization pattern to init the pattern. static rfl::Result regex_pattern() noexcept { if (!regex_pattern_) { try { regex_pattern_ = std::regex(_regex.str()); } catch (std::exception& e) { return Error(e.what()); } } return *regex_pattern_; } private: /// The underlying regex pattern. static inline std::optional regex_pattern_; }; } // namespace rfl #endif