draconisplusplus/include/rfl/comparisons.hpp

158 lines
5.4 KiB
C++
Raw Normal View History

2024-05-31 22:59:00 -04:00
#ifndef RFL_COMPARISONS_HPP_
#define RFL_COMPARISONS_HPP_
#include <type_traits>
#include "Result.hpp"
#include "parsing/schema/ValidationType.hpp"
namespace rfl {
2024-06-08 14:10:59 -04:00
template <auto _threshold>
struct EqualTo {
template <class T>
static Result<T> validate(T _value) noexcept {
constexpr auto threshold = static_cast<T>(_threshold);
if (_value != threshold) {
2024-06-08 15:53:06 -04:00
return Error(
2024-06-16 00:13:15 -04:00
"Value expected to be equal to " + std::to_string(threshold) + ", but got " +
std::to_string(_value) + "."
2024-06-08 15:53:06 -04:00
);
2024-06-08 14:10:59 -04:00
}
return _value;
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
const auto value = std::is_floating_point_v<T>
? std::variant<double, int>(static_cast<double>(_threshold))
: std::variant<double, int>(static_cast<int>(_threshold));
return ValidationType { ValidationType::EqualTo { .value_ = value } };
2024-06-08 14:10:59 -04:00
}
};
template <auto _threshold>
struct Minimum {
template <class T>
static Result<T> validate(T _value) noexcept {
constexpr auto threshold = static_cast<T>(_threshold);
if (_value < threshold) {
2024-06-08 15:53:06 -04:00
return Error(
2024-06-16 00:13:15 -04:00
"Value expected to be greater than or equal to " + std::to_string(threshold) +
", but got " + std::to_string(_value) + "."
2024-06-08 15:53:06 -04:00
);
2024-06-08 14:10:59 -04:00
}
return _value;
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
const auto value = std::is_floating_point_v<T>
? std::variant<double, int>(static_cast<double>(_threshold))
: std::variant<double, int>(static_cast<int>(_threshold));
return ValidationType { ValidationType::Minimum { .value_ = value } };
2024-05-31 22:59:00 -04:00
}
2024-06-08 14:10:59 -04:00
};
template <auto _threshold>
struct ExclusiveMinimum {
template <class T>
static Result<T> validate(T _value) noexcept {
constexpr auto threshold = static_cast<T>(_threshold);
if (_value <= threshold) {
2024-06-08 15:53:06 -04:00
return Error(
2024-06-16 00:13:15 -04:00
"Value expected to be greater than " + std::to_string(threshold) + ", but got " +
std::to_string(_value) + "."
2024-06-08 15:53:06 -04:00
);
2024-06-08 14:10:59 -04:00
}
return _value;
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
const auto value = std::is_floating_point_v<T>
? std::variant<double, int>(static_cast<double>(_threshold))
: std::variant<double, int>(static_cast<int>(_threshold));
return ValidationType { ValidationType::ExclusiveMinimum { .value_ = value } };
2024-06-08 14:10:59 -04:00
}
};
template <auto _threshold>
struct Maximum {
template <class T>
static Result<T> validate(T _value) noexcept {
constexpr auto threshold = static_cast<T>(_threshold);
if (_value > threshold) {
2024-06-08 15:53:06 -04:00
return Error(
2024-06-16 00:13:15 -04:00
"Value expected to be less than or equal to " + std::to_string(threshold) + ", but got " +
std::to_string(_value) + "."
2024-06-08 15:53:06 -04:00
);
2024-06-08 14:10:59 -04:00
}
return _value;
}
template <class T>
static parsing::schema::ValidationType to_schema() {
using ValidationType = parsing::schema::ValidationType;
2024-06-16 00:13:15 -04:00
const auto value = std::is_floating_point_v<T>
? std::variant<double, int>(static_cast<double>(_threshold))
: std::variant<double, int>(static_cast<int>(_threshold));
return ValidationType { ValidationType::Maximum { .value_ = value } };
2024-05-31 22:59:00 -04:00
}
2024-06-08 14:10:59 -04:00
};
template <auto _threshold>
struct ExclusiveMaximum {
template <class T>
static Result<T> validate(T _value) noexcept {
constexpr auto threshold = static_cast<T>(_threshold);
if (_value >= threshold) {
2024-06-08 15:53:06 -04:00
return Error(
2024-06-16 00:13:15 -04:00
"Value expected to be less than " + std::to_string(threshold) + ", but got " +
std::to_string(_value) + "."
2024-06-08 15:53:06 -04:00
);
2024-06-08 14:10:59 -04:00
}
return _value;
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
const auto value = std::is_floating_point_v<T>
? std::variant<double, int>(static_cast<double>(_threshold))
: std::variant<double, int>(static_cast<int>(_threshold));
return ValidationType { ValidationType::ExclusiveMaximum { .value_ = value } };
2024-06-08 14:10:59 -04:00
}
};
template <auto _threshold>
struct NotEqualTo {
template <class T>
static Result<T> validate(T _value) noexcept {
constexpr auto threshold = static_cast<T>(_threshold);
if (_value == threshold) {
2024-06-08 15:53:06 -04:00
return Error(
2024-06-16 00:13:15 -04:00
"Value expected to not be equal to " + std::to_string(threshold) + ", but got " +
std::to_string(_value) + "."
2024-06-08 15:53:06 -04:00
);
2024-06-08 14:10:59 -04:00
}
return _value;
}
template <class T>
static parsing::schema::ValidationType to_schema() {
using ValidationType = parsing::schema::ValidationType;
2024-06-16 00:13:15 -04:00
const auto value = std::is_floating_point_v<T>
? std::variant<double, int>(static_cast<double>(_threshold))
: std::variant<double, int>(static_cast<int>(_threshold));
return ValidationType { ValidationType::NotEqualTo { .value_ = value } };
2024-06-08 14:10:59 -04:00
}
};
} // namespace rfl
2024-05-31 22:59:00 -04:00
#endif