draconisplusplus/include/rfl/OneOf.hpp

73 lines
2.2 KiB
C++
Raw Normal View History

2024-05-31 22:59:00 -04:00
#ifndef RFL_ONEOF_HPP_
#define RFL_ONEOF_HPP_
#include <string>
#include <utility>
#include <vector>
#include "Result.hpp"
#include "parsing/schema/ValidationType.hpp"
namespace rfl {
2024-06-08 14:10:59 -04:00
/// Requires that all of the contraints C and Cs be true.
template <class C, class... Cs>
struct OneOf {
template <class T>
static rfl::Result<T> validate(const T& _value) noexcept {
return validate_impl<T, C, Cs...>(_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;
const auto types = std::vector<ValidationType>(
2024-06-08 15:53:06 -04:00
{C::template to_schema<T>(), Cs::template to_schema<T>()...}
);
2024-06-08 14:10:59 -04:00
return ValidationType {ValidationType::OneOf {.types_ = types}};
}
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
private:
static Error make_error_message(const std::vector<Error>& _errors) {
std::string msg = "Expected exactly 1 out of " +
std::to_string(sizeof...(Cs) + 1) +
" validations to pass, but " +
std::to_string(sizeof...(Cs) + 1 - _errors.size()) +
" of them did. The following errors were generated: ";
for (size_t i = 0; i < _errors.size(); ++i) {
msg += "\n" + std::to_string(i + 1) + ") " + _errors.at(i).what();
}
return Error(msg);
2024-05-31 22:59:00 -04:00
}
2024-06-08 14:10:59 -04:00
template <class T, class Head, class... Tail>
2024-06-08 15:53:06 -04:00
static rfl::Result<T>
validate_impl(const T& _value, std::vector<Error> _errors) {
2024-06-08 14:10:59 -04:00
const auto push_back = [&](Error&& _err) -> rfl::Result<T> {
_errors.emplace_back(std::forward<Error>(_err));
return _err;
};
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
const auto next_validation = [&](rfl::Result<T>&& _r) -> rfl::Result<T> {
_r.or_else(push_back);
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
if constexpr (sizeof...(Tail) == 0) {
if (_errors.size() == sizeof...(Cs)) { return _value; }
return make_error_message(_errors);
} else {
return validate_impl<T, Tail...>(
2024-06-08 15:53:06 -04:00
_value, std::forward<std::vector<Error>>(_errors)
);
2024-05-31 22:59:00 -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
return Head::validate(_value)
.and_then(next_validation)
.or_else(next_validation);
}
};
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