draconisplusplus/include/rfl/PatternValidator.hpp

40 lines
1,017 B
C++
Raw Normal View History

2024-05-31 22:59:00 -04:00
#ifndef RFL_PATTERNVALIDATOR_HPP_
#define RFL_PATTERNVALIDATOR_HPP_
#include <string>
#include "../ctre.hpp"
#include "Literal.hpp"
#include "Result.hpp"
#include "internal/StringLiteral.hpp"
#include "parsing/schema/ValidationType.hpp"
namespace rfl {
2024-06-08 14:10:59 -04:00
template <internal::StringLiteral _regex, internal::StringLiteral _name>
struct PatternValidator {
using Name = Literal<_name>;
using Regex = Literal<_regex>;
static Result<std::string> validate(const std::string& _str) noexcept {
if (ctre::match<_regex.arr_>(_str)) {
return _str;
} else {
2024-06-08 15:53:06 -04:00
return rfl::Error(
2024-06-16 00:13:15 -04:00
"String '" + _str + "' did not match format '" + _name.str() + "': '" + _regex.str() +
"'."
2024-06-08 15:53:06 -04:00
);
2024-06-08 14:10:59 -04:00
}
2024-05-31 22:59:00 -04:00
}
2024-06-08 14:10:59 -04:00
template <class T>
static parsing::schema::ValidationType to_schema() {
using ValidationType = parsing::schema::ValidationType;
2024-06-16 00:13:15 -04:00
return ValidationType { ValidationType::Regex { .pattern_ = Regex().str() } };
2024-06-08 14:10:59 -04:00
}
};
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
} // namespace rfl
2024-05-31 22:59:00 -04:00
#endif