#ifndef RFL_COMPARISONS_HPP_ #define RFL_COMPARISONS_HPP_ #include #include #include "Result.hpp" #include "parsing/schema/ValidationType.hpp" namespace rfl { template struct EqualTo { template static Result validate(T _value) noexcept { constexpr auto threshold = static_cast(_threshold); if (_value != threshold) { std::stringstream stream; stream << "Value expected to be equal to " << threshold << ", but got " << _value << "."; return error(stream.str()); } return _value; } template static parsing::schema::ValidationType to_schema() { using ValidationType = parsing::schema::ValidationType; const auto value = std::is_floating_point_v ? rfl::Variant(static_cast(_threshold)) : rfl::Variant(static_cast(_threshold)); return ValidationType{ValidationType::EqualTo{.value_ = value}}; } }; template struct Minimum { template static Result validate(T _value) noexcept { constexpr auto threshold = static_cast(_threshold); if (_value < threshold) { std::stringstream stream; stream << "Value expected to be greater than or equal to " << threshold << ", but got " << _value << "."; return error(stream.str()); } return _value; } template static parsing::schema::ValidationType to_schema() { using ValidationType = parsing::schema::ValidationType; const auto value = std::is_floating_point_v ? rfl::Variant(static_cast(_threshold)) : rfl::Variant(static_cast(_threshold)); return ValidationType{ValidationType::Minimum{.value_ = value}}; } }; template struct ExclusiveMinimum { template static Result validate(T _value) noexcept { constexpr auto threshold = static_cast(_threshold); if (_value <= threshold) { std::stringstream stream; stream << "Value expected to be greater than " << threshold << ", but got " << _value << "."; return error(stream.str()); } return _value; } template static parsing::schema::ValidationType to_schema() { using ValidationType = parsing::schema::ValidationType; const auto value = std::is_floating_point_v ? rfl::Variant(static_cast(_threshold)) : rfl::Variant(static_cast(_threshold)); return ValidationType{ValidationType::ExclusiveMinimum{.value_ = value}}; } }; template struct Maximum { template static Result validate(T _value) noexcept { constexpr auto threshold = static_cast(_threshold); if (_value > threshold) { std::stringstream stream; stream << "Value expected to be less than or equal to " << threshold << ", but got " << _value << "."; return error(stream.str()); } return _value; } template static parsing::schema::ValidationType to_schema() { using ValidationType = parsing::schema::ValidationType; const auto value = std::is_floating_point_v ? rfl::Variant(static_cast(_threshold)) : rfl::Variant(static_cast(_threshold)); return ValidationType{ValidationType::Maximum{.value_ = value}}; } }; template struct ExclusiveMaximum { template static Result validate(T _value) noexcept { constexpr auto threshold = static_cast(_threshold); if (_value >= threshold) { std::stringstream stream; stream << "Value expected to be less than " << threshold << ", but got " << _value << "."; return error(stream.str()); } return _value; } template static parsing::schema::ValidationType to_schema() { using ValidationType = parsing::schema::ValidationType; const auto value = std::is_floating_point_v ? rfl::Variant(static_cast(_threshold)) : rfl::Variant(static_cast(_threshold)); return ValidationType{ValidationType::ExclusiveMaximum{.value_ = value}}; } }; template struct NotEqualTo { template static Result validate(T _value) noexcept { constexpr auto threshold = static_cast(_threshold); if (_value == threshold) { std::stringstream stream; stream << "Value expected not to be equal to " << threshold << ", but got " << _value << "."; return error(stream.str()); } return _value; } template static parsing::schema::ValidationType to_schema() { using ValidationType = parsing::schema::ValidationType; const auto value = std::is_floating_point_v ? rfl::Variant(static_cast(_threshold)) : rfl::Variant(static_cast(_threshold)); return ValidationType{ValidationType::NotEqualTo{.value_ = value}}; } }; } // namespace rfl #endif