Parse Literal in terms of the ReflectionType

This commit is contained in:
Patrick Urbanke
2023-10-31 18:34:29 +01:00
parent 03210cdc73
commit 463fa175fc
2 changed files with 7 additions and 31 deletions
+7
View File
@@ -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(); }
-31
View File
@@ -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>