summaryrefslogtreecommitdiffhomepage
path: root/serialize/pass-mode.cpp
blob: 6d52a5def830c3a51e50a8c9cbc690279be1c3ad (plain)
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
#include "pass-mode.hpp"
#include "compat/exception.hpp"
#include "serialize/corrade-string.hpp"
#include <nlohmann/json.hpp>
#include <Corrade/Containers/StringStlView.h>

namespace nlohmann {

using namespace floormat;

static constexpr struct {
    pass_mode mode;
    StringView str;
} table[] = {
    { pass_mode::shoot_through, "shoot-through"_s },
    { pass_mode::pass, "pass"_s },
    { pass_mode::blocked, "blocked"_s },
    { pass_mode::see_through, "see-through"_s },
};

void adl_serializer<pass_mode>::to_json(json& j, pass_mode val)
{
    for (const auto [mode, str] : table)
        if (mode == val)
        {
            j = str;
            return;
        }
    fm_throw("invalid pass mode '{}'"_cf, std::size_t(val));
}

void adl_serializer<pass_mode>::from_json(const json& j, pass_mode& val)
{
    StringView value = j;
    for (const auto [mode, str] : table)
        if (str == value)
        {
            val = mode;
            return;
        }
    fm_throw("invalid pass mode '{}'"_cf, value);
}

} // namespace nlohmann