5#include <crunch/core/crunch_types.hpp>
16template <
typename V,
typename T>
18 { V::Check(value, field_id) } -> std::same_as<std::optional<Error>>;
26 [[nodiscard]]
static constexpr auto Check(T,
FieldId)
noexcept
27 -> std::optional<Error> {
36 [[nodiscard]]
static constexpr std::optional<Error> check_presence(
37 bool set,
FieldId id)
noexcept {
49 [[nodiscard]]
static constexpr std::optional<Error> check_presence(
60 requires std::same_as<T, bool>
61 [[nodiscard]]
static constexpr auto Check(T value,
63 -> std::optional<Error> {
76 requires std::same_as<T, bool>
77 [[nodiscard]]
static constexpr auto Check(T value,
79 -> std::optional<Error> {
92 requires std::floating_point<T>
93 [[nodiscard]]
static constexpr auto Check(T value,
95 -> std::optional<Error> {
96 if (std::isfinite(value)) {
109template <auto Target, auto Tolerance>
111 template <
typename T>
112 requires(std::floating_point<T> || std::integral<T>) &&
113 (!std::is_same_v<T, bool>)
114 [[nodiscard]]
static constexpr auto Check(T value,
116 -> std::optional<Error> {
117 if (std::abs(value - Target) <= Tolerance) {
129 std::same_as<T, Required> || std::same_as<T, Optional>;
133 template <
typename T>
134 requires(std::signed_integral<T> || std::floating_point<T>) &&
135 (!std::is_same_v<T, bool>)
136 [[nodiscard]]
static constexpr auto Check(T value,
138 -> std::optional<Error> {
148 template <
typename T>
149 requires(std::signed_integral<T> || std::floating_point<T>) &&
150 (!std::is_same_v<T, bool>)
151 [[nodiscard]]
static constexpr auto Check(T value,
153 -> std::optional<Error> {
163 template <
typename T>
164 requires(std::floating_point<T> || std::integral<T>) &&
165 (!std::is_same_v<T, bool>)
166 [[nodiscard]]
static constexpr auto Check(T value,
168 -> std::optional<Error> {
178 template <
typename T>
179 requires std::integral<T> && (!std::is_same_v<T, bool>)
180 [[nodiscard]]
static constexpr auto Check(T value,
182 -> std::optional<Error> {
183 if (value % 2 == 0) {
192 template <
typename T>
193 requires std::integral<T> && (!std::is_same_v<T, bool>)
194 [[nodiscard]]
static constexpr auto Check(T value,
196 -> std::optional<Error> {
197 if (value % 2 != 0) {
205template <auto Threshold>
207 template <
typename T>
208 requires(std::floating_point<T> || std::integral<T>) &&
209 (!std::is_same_v<T, bool>)
210 [[nodiscard]]
static constexpr auto Check(T value,
212 -> std::optional<Error> {
213 if (value < Threshold) {
221template <auto Threshold>
223 template <
typename T>
224 requires(std::floating_point<T> || std::integral<T>) &&
225 (!std::is_same_v<T, bool>)
226 [[nodiscard]]
static constexpr auto Check(T value,
228 -> std::optional<Error> {
229 if (value > Threshold) {
238template <auto Threshold>
240 template <
typename T>
241 requires(std::floating_point<T> || std::integral<T>) &&
242 (!std::is_same_v<T, bool>)
243 [[nodiscard]]
static constexpr auto Check(T value,
245 -> std::optional<Error> {
246 if (value <= Threshold) {
255template <auto Threshold>
257 template <
typename T>
258 requires(std::floating_point<T> || std::integral<T>) &&
259 (!std::is_same_v<T, bool>)
260 [[nodiscard]]
static constexpr auto Check(T value,
262 -> std::optional<Error> {
263 if (value >= Threshold) {
271template <auto Threshold>
273 template <
typename T>
274 requires(std::floating_point<T> || std::integral<T> ||
275 std::is_enum_v<T>) &&
276 (!std::is_same_v<T, bool>)
277 [[nodiscard]]
static constexpr auto Check(T value,
279 -> std::optional<Error> {
280 if (value == Threshold) {
288template <auto Threshold>
290 template <
typename T>
291 requires(std::floating_point<T> || std::integral<T> ||
292 std::is_enum_v<T>) &&
293 (!std::is_same_v<T, bool>)
294 [[nodiscard]]
static constexpr auto Check(T value,
296 -> std::optional<Error> {
297 if (value != Threshold) {
305template <
auto... Values>
307 template <
typename T>
308 requires(std::floating_point<T> || std::integral<T> ||
309 std::is_enum_v<T>) &&
310 (!std::is_same_v<T, bool>)
311 [[nodiscard]]
static constexpr auto Check(T value,
313 -> std::optional<Error> {
314 if (((value == Values) || ...)) {
323template <std::
size_t N>
325 template <
typename T>
326 requires requires(
const T& t) { std::size(t); }
327 [[nodiscard]]
static constexpr auto Check(
const T& value,
329 -> std::optional<Error> {
330 if (std::size(value) == N) {
338template <std::
size_t N>
340 template <
typename T>
341 requires requires(
const T& t) { std::size(t); }
342 [[nodiscard]]
static constexpr auto Check(
const T& value,
344 -> std::optional<Error> {
345 if (std::size(value) >= N) {
353template <std::
size_t N>
355 template <
typename T>
356 requires requires(
const T& t) { std::size(t); }
357 [[nodiscard]]
static constexpr auto Check(
const T& value,
359 -> std::optional<Error> {
360 if (std::size(value) <= N) {
369 template <
typename T>
370 requires requires(
const T& t) {
371 { t.begin() } -> std::forward_iterator;
372 { t.end() } -> std::forward_iterator;
374 [[nodiscard]]
static constexpr auto Check(
const T& value,
376 -> std::optional<Error> {
378 auto end = value.end();
379 for (
auto it = value.begin(); it != end; ++it) {
380 for (
auto it2 = std::next(it); it2 != end; ++it2) {
383 "elements must be unique");
393 template <
typename T>
394 requires std::convertible_to<T, std::string_view>
395 [[nodiscard]]
static constexpr auto Check(T value,
397 -> std::optional<Error> {
398 std::string_view sv{value};
399 if (!sv.empty() && sv.back() ==
'\0') {
411template <std::
size_t N>
414 constexpr explicit FixedString(
const char (&str)[N]) {
415 for (std::size_t i = 0; i < N; ++i) {
419 constexpr std::string_view view()
const {
return {buf, N - 1}; }
423template <FixedString S>
425 template <
typename T>
426 requires std::convertible_to<T, std::string_view>
427 [[nodiscard]]
static constexpr auto Check(T value,
429 -> std::optional<Error> {
430 if (std::string_view{value} == S.view()) {
438template <FixedString S>
440 template <
typename T>
441 requires std::convertible_to<T, std::string_view>
442 [[nodiscard]]
static constexpr auto Check(T value,
444 -> std::optional<Error> {
445 if (std::string_view{value} != S.view()) {
Concept for validators that check field presence semantics.
Definition: crunch_validators.hpp:128
Concept defining a value validator.
Definition: crunch_validators.hpp:17
The public API for Crunch.
Definition: crunch_endian.hpp:10
int32_t FieldId
Unique identifier for a field within a Crunch message.
Definition: crunch_types.hpp:11
Validates that a floating-point value is within a tolerance of a target.
Definition: crunch_validators.hpp:110
Validates that a value equals a compile-time threshold.
Definition: crunch_validators.hpp:272
static constexpr Error validation(FieldId id, const char(&msg)[N]) noexcept
Creates an error representing a validation failure.
Definition: crunch_types.hpp:97
Validates that an integral value is even.
Definition: crunch_validators.hpp:177
Validates that a boolean value is false.
Definition: crunch_validators.hpp:74
Definition: crunch_validators.hpp:412
Validates that a value is greater than or equal to a compile-time threshold.
Definition: crunch_validators.hpp:256
Validates that a value is greater than a compile-time threshold.
Definition: crunch_validators.hpp:222
Validates that a floating-point value is finite (not NaN or Inf).
Definition: crunch_validators.hpp:90
Validates that a container has at least N elements.
Definition: crunch_validators.hpp:339
Validates that a container has at most N elements.
Definition: crunch_validators.hpp:354
Validates that a string-like or container value has a specific length.
Definition: crunch_validators.hpp:324
Validates that a value is less than or equal to a compile-time threshold.
Definition: crunch_validators.hpp:239
Validates that a value is less than a compile-time threshold.
Definition: crunch_validators.hpp:206
Validates that a value is strictly negative (< 0).
Definition: crunch_validators.hpp:147
Validates nothing (always succeeds).
Definition: crunch_validators.hpp:24
Validates that a value does not equal a compile-time threshold.
Definition: crunch_validators.hpp:289
Validates that a value is not zero.
Definition: crunch_validators.hpp:162
Validates that a string does not contain embedded nulls.
Definition: crunch_validators.hpp:392
Validates that an integral value is odd.
Definition: crunch_validators.hpp:191
Validates that a value is one of a set of compile-time values.
Definition: crunch_validators.hpp:306
Presence validator allowing an optional field (can be unset).
Definition: crunch_validators.hpp:48
Validates that a value is non-negative (>= 0).
Definition: crunch_validators.hpp:132
Presence validator enforcing that a field MUST be set.
Definition: crunch_validators.hpp:35
Validates that a string equals a compile-time string.
Definition: crunch_validators.hpp:424
Validates that a string does not equal a compile-time string.
Definition: crunch_validators.hpp:439
Validates that a boolean value is true.
Definition: crunch_validators.hpp:58
Validates that a container has unique elements.
Definition: crunch_validators.hpp:368