mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-11-29 14:26:19 +08:00
Parse Literal in terms of the ReflectionType
This commit is contained in:
@@ -33,12 +33,16 @@ class Literal {
|
||||
/// can assume.
|
||||
static constexpr ValueType num_fields_ = sizeof...(fields_);
|
||||
|
||||
using ReflectionType = std::string;
|
||||
|
||||
/// Constructs a Literal from another literal.
|
||||
Literal(const Literal<fields_...>& _other) = default;
|
||||
|
||||
/// Constructs a Literal from another literal.
|
||||
Literal(Literal<fields_...>&& _other) noexcept = default;
|
||||
|
||||
Literal(const std::string& _str) : value_(find_value(_str).value()) {}
|
||||
|
||||
/// A single-field literal is special because it
|
||||
/// can also have a default constructor.
|
||||
template <ValueType num_fields = num_fields_,
|
||||
@@ -150,6 +154,9 @@ class Literal {
|
||||
/// Inequality operator for strings.
|
||||
bool operator!=(const std::string& _str) const { return name() != _str; }
|
||||
|
||||
/// Alias for .name().
|
||||
std::string reflection() const { return name(); }
|
||||
|
||||
/// Alias for .name().
|
||||
std::string str() const { return name(); }
|
||||
|
||||
|
||||
@@ -251,37 +251,6 @@ struct Parser<R, W, Box<T>> {
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
template <class R, class W, internal::StringLiteral... _fields>
|
||||
requires AreReaderAndWriter<R, W, Literal<_fields...>>
|
||||
struct Parser<R, W, Literal<_fields...>> {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
/// Expresses the variables as type T.
|
||||
static Result<Literal<_fields...>> read(const R& _r,
|
||||
const InputVarType& _var) noexcept {
|
||||
const auto to_type = [](const auto& _str) {
|
||||
return Literal<_fields...>::from_string(_str);
|
||||
};
|
||||
|
||||
const auto embellish_error = [](const Error& _e) {
|
||||
return Error(std::string("Failed to parse Literal: ") + _e.what());
|
||||
};
|
||||
|
||||
return _r.template to_basic_type<std::string>(_var)
|
||||
.and_then(to_type)
|
||||
.or_else(embellish_error);
|
||||
}
|
||||
|
||||
/// Expresses the variable a a JSON.
|
||||
static OutputVarType write(const W& _w,
|
||||
const Literal<_fields...>& _literal) noexcept {
|
||||
return _w.from_basic_type(_literal.name());
|
||||
}
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/// Used for maps for which the key type is std::string. These are
|
||||
/// represented as objects.
|
||||
template <class R, class W, class MapType>
|
||||
|
||||
Reference in New Issue
Block a user