File: Parser_result.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 (79 lines) | stat: -rw-r--r-- 2,515 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#ifndef RFL_PARSING_PARSER_RESULT_HPP_
#define RFL_PARSING_PARSER_RESULT_HPP_

#include <map>
#include <type_traits>

#include "../Result.hpp"
#include "../always_false.hpp"
#include "../internal/StringLiteral.hpp"
#include "Parser_base.hpp"
#include "schema/Type.hpp"

namespace rfl {
namespace parsing {

template <class R, class W, class T, class ProcessorsType>
  requires AreReaderAndWriter<R, W, Result<T>>
struct Parser<R, W, Result<T>, ProcessorsType> {
  using InputVarType = typename R::InputVarType;

  using ErrorType = NamedTuple<Field<"error", std::string>>;
  using VariantType = std::variant<std::remove_cvref_t<T>, ErrorType>;

  static Result<Result<T>> read(const R& _r,
                                const InputVarType& _var) noexcept {
    const auto handle = [](auto&& _t) -> Result<T> {
      using Type = std::remove_cvref_t<decltype(_t)>;
      if constexpr (std::is_same<Type, ErrorType>()) {
        return error(_t.template get<"error">());
      } else {
        return std::forward<std::remove_cvref_t<T>>(_t);
      }
    };

    const auto to_res = [&](VariantType&& _v) -> Result<T> {
      return std::visit(handle, std::forward<VariantType>(_v));
    };

    return Result<Result<T>>(
        Parser<R, W, VariantType, ProcessorsType>::read(_r, _var).transform(
            to_res));
  }

  template <class P>
  static void write(const W& _w, const Result<T>& _r,
                    const P& _parent) noexcept {
    // const auto write_t = [&](const auto& _t) -> Nothing {
    //   Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::write(_w, _t,
    //                                                               _parent);
    //   return Nothing{};
    // };

    // const auto write_err = [&](const auto& _err) -> Nothing {
    //   Parser<R, W, ErrorType, ProcessorsType>::write(
    //       _w, ErrorType(make_field<"error">(_err.what())), _parent);
    //   return Nothing{};
    // };
    if (_r) {
      Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::write(
          _w, _r.value(), _parent);
    } else {
      Parser<R, W, ErrorType, ProcessorsType>::write(
          _w, ErrorType(make_field<"error">(_r.error().what())), _parent);
    }

    // _r.transform(write_t).transform_error(write_err);
  }

  static schema::Type to_schema(
      std::map<std::string, schema::Type>* _definitions) {
    return Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::to_schema(
        _definitions);
  }
};

}  // namespace parsing
}  // namespace rfl

#endif