#ifndef RFL_COMPARISONS_HPP_ #define RFL_COMPARISONS_HPP_ #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) { return Error("Value expected to be equal to " + std::to_string(threshold) + ", but got " + std::to_string(_value) + "."); } return _value; } template static parsing::schema::ValidationType to_schema() { using ValidationType = parsing::schema::ValidationType; const auto value = std::is_floating_point_v ? std::variant(static_cast(_threshold)) : std::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) { return Error("Value expected to be greater than or equal to " + std::to_string(threshold) + ", but got " + std::to_string(_value) + "."); } return _value; } template static parsing::schema::ValidationType to_schema() { using ValidationType = parsing::schema::ValidationType; const auto value = std::is_floating_point_v ? std::variant(static_cast(_threshold)) : std::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) { return Error("Value expected to be greater than " + std::to_string(threshold) + ", but got " + std::to_string(_value) + "."); } return _value; } template static parsing::schema::ValidationType to_schema() { using ValidationType = parsing::schema::ValidationType; const auto value = std::is_floating_point_v ? std::variant(static_cast(_threshold)) : std::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) { return Error("Value expected to be less than or equal to " + std::to_string(threshold) + ", but got " + std::to_string(_value) + "."); } return _value; } template static parsing::schema::ValidationType to_schema() { using ValidationType = parsing::schema::ValidationType; const auto value = std::is_floating_point_v ? std::variant(static_cast(_threshold)) : std::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) { return Error("Value expected to be less than " + std::to_string(threshold) + ", but got " + std::to_string(_value) + "."); } return _value; } template static parsing::schema::ValidationType to_schema() { using ValidationType = parsing::schema::ValidationType; const auto value = std::is_floating_point_v ? std::variant(static_cast(_threshold)) : std::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) { return Error("Value expected to not be equal to " + std::to_string(threshold) + ", but got " + std::to_string(_value) + "."); } return _value; } template static parsing::schema::ValidationType to_schema() { using ValidationType = parsing::schema::ValidationType; const auto value = std::is_floating_point_v ? std::variant(static_cast(_threshold)) : std::variant(static_cast(_threshold)); return ValidationType {ValidationType::NotEqualTo {.value_ = value}}; } }; } // namespace rfl #endif