File: Size.hpp

package info (click to toggle)
reflect-cpp 0.18.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 12,524 kB
  • sloc: cpp: 44,484; python: 131; makefile: 30; sh: 3
file content (35 lines) | stat: -rw-r--r-- 876 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#ifndef RFL_SIZE_HPP_
#define RFL_SIZE_HPP_

#include <map>

#include "Ref.hpp"
#include "Result.hpp"
#include "parsing/schema/ValidationType.hpp"

namespace rfl {

template <class V>
struct Size {
  template <class T>
  static rfl::Result<T> validate(const T& _t) {
    const auto to_t = [&](const auto&) { return _t; };
    const auto embellish_error = [](const auto& _err) -> Error {
      return Error("Size validation failed: " + _err.what());
    };
    return V::validate(_t.size()).transform(to_t).transform_error(
        embellish_error);
  }

  template <class T>
  static parsing::schema::ValidationType to_schema() {
    using ValidationType = parsing::schema::ValidationType;
    return ValidationType{ValidationType::Size{
        .size_limit_ =
            rfl::Ref<ValidationType>::make(V::template to_schema<size_t>())}};
  }
};

}  // namespace rfl

#endif