3#include "ll/api/reflection/Reflection.h"
4#include "ll/api/reflection/SerializationError.h"
14namespace ll::reflection {
15template <concepts::IsVectorBase T,
class J>
16inline Expected<> deserialize_impl(T& vec, J&& j, meta::PriorityTag<5>);
17template <concepts::IsDispatcher T,
class J>
18inline Expected<> deserialize_impl(T& d, J&& j, meta::PriorityTag<5>);
19template <concepts::IsOptional T,
class J>
20inline Expected<> deserialize_impl(T& opt, J&& j, meta::PriorityTag<5>);
21template <concepts::IsString T,
class J>
22inline Expected<> deserialize_impl(T& str, J&& j, meta::PriorityTag<4>);
23template <concepts::TupleLike T,
class J>
24inline Expected<> deserialize_impl(T& tuple, J&& j, meta::PriorityTag<3>);
25template <concepts::ArrayLike T,
class J>
26inline Expected<> deserialize_impl(T& arr, J&& j, meta::PriorityTag<2>);
27template <concepts::Associative T,
class J>
28inline Expected<> deserialize_impl(T& map, J
const& j, meta::PriorityTag<2>);
29template <Reflectable T,
class J>
30inline Expected<> deserialize_impl(T& obj, J
const& j, meta::PriorityTag<1>);
31template <concepts::Require<std::is_enum> T,
class J>
32inline Expected<> deserialize_impl(T& e, J
const& j, meta::PriorityTag<1>);
33template <
class T,
class J>
34inline Expected<> deserialize_impl(T& obj, J
const& j, meta::PriorityTag<0>)
35 requires(std::convertible_to<J, T>);
37template <
class T,
class J>
38[[nodiscard]]
inline Expected<> deserialize(T& t, J&& j)
noexcept
39#if !defined(__INTELLISENSE__)
40 requires(
requires(T& t, J&& j) { deserialize_impl<T>(t, std::forward<J>(j), meta::PriorityTag<5>{}); })
43 return deserialize_impl<T>(t, std::forward<J>(j), meta::PriorityTag<5>{});
45 return makeExceptionError();
48template <
class T,
class J>
49[[nodiscard]]
inline Expected<T> deserialize_to(J&& j)
noexcept {
51 if (
auto d = deserialize<T>(*res, std::forward<J>(j)); !d) {
52 res = forwardError(d.error());
57template <concepts::IsVectorBase T,
class J>
58inline Expected<> deserialize_impl(T& vec, J&& j, meta::PriorityTag<5>) {
60 T::forEachComponent([&]<
typename axis_type,
size_t iter> {
62 res = deserialize<axis_type>(vec.template get<axis_type, iter>(),
static_cast<J&&
>(j[iter]));
63 if (!res) res = makeSerIndexError(iter, res.error());
68template <concepts::IsDispatcher T,
class J>
69inline Expected<> deserialize_impl(T& d, J&& j, meta::PriorityTag<5>) {
70 auto res{deserialize<typename T::storage_type>(d.storage, std::forward<J>(j))};
74template <concepts::IsOptional T,
class J>
75inline Expected<> deserialize_impl(T& opt, J&& j, meta::PriorityTag<5>) {
80 if (!opt) opt.emplace();
81 res = deserialize<typename T::value_type>(*opt, std::forward<J>(j));
85template <concepts::IsString T,
class J>
86inline Expected<> deserialize_impl(T& str, J&& j, meta::PriorityTag<4>) {
87 if (!j.is_string())
return makeStringError(
"field must be a string");
88 str = std::string{std::forward<J>(j)};
91template <concepts::TupleLike T,
class J>
92inline Expected<> deserialize_impl(T& tuple, J&& j, meta::PriorityTag<3>) {
93 if (!j.is_array())
return makeStringError(
"field must be an array");
94 if (j.size() != std::tuple_size_v<T>)
95 return makeStringError(fmt::format(
"array size must be {}", std::tuple_size_v<T>));
102 res = deserialize<std::remove_cvref_t<
decltype(arg)>>(arg,
static_cast<J&&
>(j[iter]));
103 if (!res) res = makeSerIndexError(iter, res.error());
113template <concepts::ArrayLike T,
class J>
114inline Expected<> deserialize_impl(T& arr, J&& j, meta::PriorityTag<2>) {
115 if (!j.is_array())
return makeStringError(
"field must be an array");
116 using value_type =
typename T::value_type;
117 if constexpr (
requires(T a) { a.clear(); }) {
120 if constexpr (
requires(T a, value_type v) { a.push_back(v); }) {
121 for (
size_t i = 0; i < j.size(); i++) {
122 if (
auto res = deserialize<value_type>(arr.emplace_back(),
static_cast<J&&
>(j[i])); !res) {
123 res = makeSerIndexError(i, res.error());
127 }
else if constexpr (
requires(T a, value_type v) { a.insert(v); }) {
128 for (
size_t i = 0; i < j.size(); i++) {
130 if (
auto res = deserialize<value_type>(tmp,
static_cast<J&&
>(j[i])); !res) {
131 res = makeSerIndexError(i, res.error());
134 arr.insert(std::move(tmp));
139template <concepts::Associative T,
class J>
140inline Expected<> deserialize_impl(T& map, J
const& j, meta::PriorityTag<2>) {
142 (concepts::IsString<typename T::key_type> || std::is_enum_v<typename T::key_type>),
143 "the key type of the associative container must be convertible to a string"
145 if (!j.is_object())
return makeStringError(
"field must be an object");
147 for (
auto&& [k, v] : j.items()) {
148 if constexpr (std::is_enum_v<typename T::key_type>) {
149 if (
auto res = deserialize<typename T::mapped_type>(
150 map[magic_enum::enum_cast<typename T::key_type>(k).value()],
151 std::forward<
decltype((v))>(v)
154 res = makeSerKeyError(magic_enum::enum_cast<typename T::key_type>(k).value(), res.error());
158 if (
auto res = deserialize<typename T::mapped_type>(map[k], std::forward<
decltype((v))>(v)); !res) {
159 res = makeSerKeyError(k, res.error());
166template <Reflectable T,
class J>
167inline Expected<> deserialize_impl(T& obj, J
const& j, meta::PriorityTag<1>) {
169 if (!j.is_object()) {
170 res = makeStringError(
"field must be an object");
173 forEachMember(obj, [&](std::string_view name,
auto& member) {
174 if (name.starts_with(
'$') || !res) {
177 using member_type = std::remove_cvref_t<
decltype((member))>;
178 auto sname = std::string{name};
179 if (j.contains(sname)) {
180 if constexpr (
requires(member_type& o, J
const& s) { deserialize<member_type>(o, s); }) {
181 res = deserialize<member_type>(member, j[sname]);
182 if (!res) res = makeSerMemberError(sname, res.error());
184 static_assert(traits::always_false<member_type>,
"this type can't deserialize");
187 if constexpr (!concepts::IsOptional<member_type>) {
188 res = makeStringError(fmt::format(
"missing required field \"{}\" when deserializing", sname));
190 member = std::nullopt;
196template <concepts::Require<std::is_enum> T,
class J>
197inline Expected<> deserialize_impl(T& e, J
const& j, meta::PriorityTag<1>) {
198 using enum_type = std::remove_cvref_t<T>;
200 e = magic_enum::enum_cast<enum_type>(std::string{j}).value();
202 e = magic_enum::enum_cast<enum_type>(std::underlying_type_t<enum_type>{j}).value();
206template <
class T,
class J>
207inline Expected<> deserialize_impl(T& obj, J
const& j, meta::PriorityTag<0>)
208 requires(std::convertible_to<J, T>)