diff --git a/include/rfl.hpp b/include/rfl.hpp index e4e98e69..c83a76c5 100644 --- a/include/rfl.hpp +++ b/include/rfl.hpp @@ -4,12 +4,13 @@ #include "rfl/AllOf.hpp" #include "rfl/AnyOf.hpp" #include "rfl/Box.hpp" -#include "rfl/ConStr.hpp" #include "rfl/Field.hpp" #include "rfl/Flatten.hpp" #include "rfl/Literal.hpp" #include "rfl/NamedTuple.hpp" #include "rfl/OneOf.hpp" +#include "rfl/Pattern.hpp" +#include "rfl/PatternValidator.hpp" #include "rfl/Ref.hpp" #include "rfl/TaggedUnion.hpp" #include "rfl/Timestamp.hpp" @@ -30,6 +31,7 @@ #include "rfl/name_t.hpp" #include "rfl/named_tuple_t.hpp" #include "rfl/parsing/CustomParser.hpp" +#include "rfl/patterns.hpp" #include "rfl/remove_fields.hpp" #include "rfl/replace.hpp" #include "rfl/to_named_tuple.hpp" diff --git a/include/rfl/Pattern.hpp b/include/rfl/Pattern.hpp new file mode 100644 index 00000000..17053765 --- /dev/null +++ b/include/rfl/Pattern.hpp @@ -0,0 +1,14 @@ +#ifndef RFL_PATTERN_HPP_ +#define RFL_PATTERN_HPP_ + +#include "rfl/PatternValidator.hpp" +#include "rfl/Validator.hpp" + +namespace rfl { + +template +using Pattern = Validator>; + +} // namespace rfl + +#endif diff --git a/include/rfl/PatternValidator.hpp b/include/rfl/PatternValidator.hpp new file mode 100644 index 00000000..8d82398a --- /dev/null +++ b/include/rfl/PatternValidator.hpp @@ -0,0 +1,52 @@ +#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 diff --git a/include/rfl/Validator.hpp b/include/rfl/Validator.hpp index 7c9aebec..d778bf9e 100644 --- a/include/rfl/Validator.hpp +++ b/include/rfl/Validator.hpp @@ -21,6 +21,15 @@ struct Validator { Validator(const T& _value) : value_(V::validate(_value).value()) {} + template , + bool>::type = true> + Validator(const U& _value) : value_(V::validate(T(_value)).value()) {} + + template , + bool>::type = true> + Validator(U&& _value) + : value_(V::validate(T(std::forward(_value))).value()) {} + ~Validator() = default; /// Exposes the underlying value. diff --git a/include/rfl/patterns.hpp b/include/rfl/patterns.hpp new file mode 100644 index 00000000..ad463022 --- /dev/null +++ b/include/rfl/patterns.hpp @@ -0,0 +1,34 @@ +#ifndef RFL_PATTERNS_HPP_ +#define RFL_PATTERNS_HPP_ + +#include "rfl/Pattern.hpp" +#include "rfl/Validator.hpp" + +namespace rfl { + +// This pattern will match valid Base64 encoded strings with or without padding. +using Base64Encoded = Pattern; + +/// email +using Email = + Pattern; + +using UUIDv1 = Pattern< + R"(^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-1[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$)", + "UUIDv1">; + +using UUIDv2 = Pattern< + R"(^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-2[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$)", + "UUIDv2">; + +using UUIDv3 = Pattern< + R"(^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-3[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$)", + "UUIDv3">; + +using UUIDv4 = Pattern< + R"(^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$)", + "UUIDv4">; + +} // namespace rfl + +#endif diff --git a/tests/test_email.hpp b/tests/test_email.hpp new file mode 100644 index 00000000..b99795a1 --- /dev/null +++ b/tests/test_email.hpp @@ -0,0 +1,25 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_email() { + std::cout << "test_email" << std::endl; + + struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name; + rfl::Field<"email", rfl::Email> email; + }; + + const auto homer = Person{.first_name = "Homer", + .last_name = "Simpson", + .email = "homer@simpson.com"}; + + write_and_read( + homer, + R"({"firstName":"Homer","lastName":"Simpson","email":"homer@simpson.com"})"); +} diff --git a/tests/tests.cpp b/tests/tests.cpp index 7914c2b1..9164901e 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -34,6 +34,7 @@ #include "test_unique_ptr.hpp" #include "test_unique_ptr2.hpp" //#include "test_constr.hpp" +#include "test_email.hpp" #include "test_one_of.hpp" #include "test_unordered_map.hpp" #include "test_unordered_multimap.hpp" @@ -87,7 +88,7 @@ int main() { test_all_of(); test_any_of(); test_one_of(); - // test_constr(); + test_email(); return 0; }