summaryrefslogtreecommitdiffstats
path: root/server/src/problem.cppm
blob: 8764962563f4867de9fbb32dcebe516cdf2e2654 (about) (plain) (blame)
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
module;

#include <boost/beast/http/message.hpp>
#include <boost/json.hpp>
#include <boost/locale/message.hpp>

export module routemon:problem;

import std;
import :locale;

namespace http = boost::beast::http;
namespace json = boost::json;

namespace routemon::problem {

  export struct details {
    http::status status;
    blocale::message title;
    std::string_view type_uri;
    std::optional<blocale::message> detail = std::nullopt;
    std::optional<std::string> instance = std::nullopt;

    auto set_detail(blocale::message detail) -> details& {
      this->detail = detail;
      return *this;
    }

    auto set_instance(std::string&& instance) -> details& {
      this->instance = instance;
      return *this;
    }
    auto set_instance(std::string_view instance) -> details& {
      this->instance = std::string{instance};
      return *this;
    }
  };

  export auto tag_invoke(json::value_from_tag, json::value& jv, details const& details, std::locale locale) -> void {
    auto obj = json::object{
      {"type",   details.type_uri},
      {"title",  details.title.str(locale)},
      {"status", static_cast<unsigned>(details.status)},
    };
    if (details.detail)   obj["detail"]   = details.detail->str(locale);
    if (details.instance) obj["instance"] = *details.instance;
    jv = obj;
  }

  export struct tpl {
    http::status status;
    blocale::message title;
    std::string_view type_uri;

    auto instantiate() const -> details {
      return details{status, title, type_uri};
    }
  };

}