draconisplusplus/include/rfl/AllOf.hpp

41 lines
1.1 KiB
C++
Raw Normal View History

2024-05-31 22:59:00 -04:00
#ifndef RFL_ALLOF_HPP_
#define RFL_ALLOF_HPP_
#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 AllOf {
template <class T>
static rfl::Result<T> validate(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;
2024-06-16 00:13:15 -04:00
const auto types =
std::vector<ValidationType>({ C::template to_schema<T>(), Cs::template to_schema<T>()... });
return ValidationType { ValidationType::AllOf { .types_ = types } };
2024-06-08 14:10:59 -04:00
}
private:
template <class T, class Head, class... Tail>
static rfl::Result<T> validate_impl(T _value) noexcept {
if constexpr (sizeof...(Tail) == 0) {
return Head::validate(_value);
} else {
return Head::validate(_value).and_then(validate_impl<T, Tail...>);
}
}
};
} // namespace rfl
2024-05-31 22:59:00 -04:00
#endif