diff options
| author | Rutger Broekhoff | 2026-08-28 18:03:05 +0200 |
|---|---|---|
| committer | Rutger Broekhoff | 2026-08-28 18:03:05 +0200 |
| commit | 973aec43ea54bbf95b64fbcb636403401d1ca60e (patch) | |
| tree | 41b7911c420766a9b463245b9296f44c5bf35258 /server/src/problem.cppm | |
| download | routemon-973aec43ea54bbf95b64fbcb636403401d1ca60e.tar.gz routemon-973aec43ea54bbf95b64fbcb636403401d1ca60e.zip | |
Import from e4b104792206ee7ea64bf39c6b7d2c0c230f9d14
Diffstat (limited to 'server/src/problem.cppm')
| -rw-r--r-- | server/src/problem.cppm | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/server/src/problem.cppm b/server/src/problem.cppm new file mode 100644 index 0000000..8764962 --- /dev/null +++ b/server/src/problem.cppm | |||
| @@ -0,0 +1,60 @@ | |||
| 1 | module; | ||
| 2 | |||
| 3 | #include <boost/beast/http/message.hpp> | ||
| 4 | #include <boost/json.hpp> | ||
| 5 | #include <boost/locale/message.hpp> | ||
| 6 | |||
| 7 | export module routemon:problem; | ||
| 8 | |||
| 9 | import std; | ||
| 10 | import :locale; | ||
| 11 | |||
| 12 | namespace http = boost::beast::http; | ||
| 13 | namespace json = boost::json; | ||
| 14 | |||
| 15 | namespace routemon::problem { | ||
| 16 | |||
| 17 | export struct details { | ||
| 18 | http::status status; | ||
| 19 | blocale::message title; | ||
| 20 | std::string_view type_uri; | ||
| 21 | std::optional<blocale::message> detail = std::nullopt; | ||
| 22 | std::optional<std::string> instance = std::nullopt; | ||
| 23 | |||
| 24 | auto set_detail(blocale::message detail) -> details& { | ||
| 25 | this->detail = detail; | ||
| 26 | return *this; | ||
| 27 | } | ||
| 28 | |||
| 29 | auto set_instance(std::string&& instance) -> details& { | ||
| 30 | this->instance = instance; | ||
| 31 | return *this; | ||
| 32 | } | ||
| 33 | auto set_instance(std::string_view instance) -> details& { | ||
| 34 | this->instance = std::string{instance}; | ||
| 35 | return *this; | ||
| 36 | } | ||
| 37 | }; | ||
| 38 | |||
| 39 | export auto tag_invoke(json::value_from_tag, json::value& jv, details const& details, std::locale locale) -> void { | ||
| 40 | auto obj = json::object{ | ||
| 41 | {"type", details.type_uri}, | ||
| 42 | {"title", details.title.str(locale)}, | ||
| 43 | {"status", static_cast<unsigned>(details.status)}, | ||
| 44 | }; | ||
| 45 | if (details.detail) obj["detail"] = details.detail->str(locale); | ||
| 46 | if (details.instance) obj["instance"] = *details.instance; | ||
| 47 | jv = obj; | ||
| 48 | } | ||
| 49 | |||
| 50 | export struct tpl { | ||
| 51 | http::status status; | ||
| 52 | blocale::message title; | ||
| 53 | std::string_view type_uri; | ||
| 54 | |||
| 55 | auto instantiate() const -> details { | ||
| 56 | return details{status, title, type_uri}; | ||
| 57 | } | ||
| 58 | }; | ||
| 59 | |||
| 60 | } | ||