Skip to content
Merged
11 changes: 6 additions & 5 deletions include/rfl/AnyOf.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef RFL_ANYOF_HPP_
#define RFL_ANYOF_HPP_

#include <sstream>
#include <string>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -28,13 +29,13 @@ struct AnyOf {

private:
static Error make_error_message(const std::vector<Error>& _errors) {
std::string msg =
"Expected at least one of the following validations to pass, but none "
"of them did:";
std::stringstream stream;
stream << "Expected at least one of the following validations to pass, but "
"none of them did:";
for (size_t i = 0; i < _errors.size(); ++i) {
msg += "\n" + std::to_string(i + 1) + ") " + _errors.at(i).what();
stream << "\n" << i + 1 << ") " << _errors.at(i).what();
}
return Error(msg);
return Error(stream.str());
}

template <class T, class Head, class... Tail>
Expand Down
7 changes: 2 additions & 5 deletions include/rfl/Literal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <compare>
#include <cstdint>
#include <functional>
#include <limits>
#include <string>
#include <tuple>
#include <type_traits>
Expand All @@ -28,10 +27,8 @@ class Literal {
using FieldsType = rfl::Tuple<LiteralHelper<fields_>...>;

public:
using ValueType =
std::conditional_t<sizeof...(fields_) <=
std::numeric_limits<std::uint8_t>::max(),
std::uint8_t, std::uint16_t>;
using ValueType = std::conditional_t<sizeof...(fields_) <= 255, std::uint8_t,
std::uint16_t>;

/// The number of different fields or different options that the literal
/// can assume.
Expand Down
14 changes: 7 additions & 7 deletions include/rfl/OneOf.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef RFL_ONEOF_HPP_
#define RFL_ONEOF_HPP_

#include <sstream>
#include <string>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -28,15 +29,14 @@ struct OneOf {

private:
static Error make_error_message(const std::vector<Error>& _errors) {
std::string msg = "Expected exactly 1 out of " +
std::to_string(sizeof...(Cs) + 1) +
" validations to pass, but " +
std::to_string(sizeof...(Cs) + 1 - _errors.size()) +
" of them did. The following errors were generated: ";
std::stringstream stream;
stream << "Expected exactly 1 out of " << sizeof...(Cs) + 1
<< " validations to pass, but " << sizeof...(Cs) + 1 - _errors.size()
<< " of them did. The following errors were generated: ";
for (size_t i = 0; i < _errors.size(); ++i) {
msg += "\n" + std::to_string(i + 1) + ") " + _errors.at(i).what();
stream << "\n" << i + 1 << ") " << _errors.at(i).what();
}
return Error(msg);
return Error(stream.str());
}

template <class T, class Head, class... Tail>
Expand Down
7 changes: 5 additions & 2 deletions include/rfl/PatternValidator.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef RFL_PATTERNVALIDATOR_HPP_
#define RFL_PATTERNVALIDATOR_HPP_

#include <sstream>
#include <string>

#if __has_include(<ctre.hpp>)
Expand All @@ -25,8 +26,10 @@ struct PatternValidator {
if (ctre::match<_regex.arr_>(_str)) {
return _str;
} else {
return rfl::Error("String '" + _str + "' did not match format '" +
_name.str() + "': '" + _regex.str() + "'.");
std::stringstream stream;
stream << "String '" << _str << "' did not match format '" << _name.str()
<< "': '" << _regex.str() << "'.";
return rfl::Error(stream.str());
}
}

Expand Down
2 changes: 1 addition & 1 deletion include/rfl/internal/StringLiteral.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct StringLiteral {
}

/// Returns the value as a string.
std::string str() const { return std::string(std::data(arr_), N - 1); }
std::string str() const { return std::string(string_view()); }

/// Returns the value as a string.
constexpr std::string_view string_view() const {
Expand Down
16 changes: 10 additions & 6 deletions include/rfl/parsing/FieldVariantReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <array>
#include <optional>
#include <sstream>
#include <string_view>
#include <type_traits>
#include <utility>
Expand Down Expand Up @@ -35,10 +36,11 @@ class FieldVariantReader {
_disc_value, _var,
std::make_integer_sequence<int, sizeof...(FieldTypes)>());
if (!*field_variant_) {
*field_variant_ = Error(
"Could not parse rfl::Variant, could not match field named "
"'" +
std::string(_disc_value) + "'.");
std::stringstream stream;
stream << "Could not parse rfl::Variant, could not match field named "
"'"
<< _disc_value << "'.";
*field_variant_ = Error(stream.str());
}
}

Expand All @@ -62,8 +64,10 @@ class FieldVariantReader {
return rfl::Variant<FieldTypes...>(FieldType(std::move(_val)));
};
const auto embellish_error = [&](const Error& _e) {
return Error("Could not parse rfl::Variant with field '" +
std::string(_disc_value) + "': " + _e.what());
std::stringstream stream;
stream << "Could not parse rfl::Variant with field '"
<< std::string(_disc_value) << "': " << _e.what();
return Error(stream.str());
};
*field_variant_ = Parser<R, W, ValueType, ProcessorsType>::read(*r_, _var)
.transform(to_variant)
Expand Down
7 changes: 5 additions & 2 deletions include/rfl/parsing/NamedTupleParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <array>
#include <bit>
#include <map>
#include <sstream>
#include <tuple>
#include <type_traits>
#include <unordered_map>
Expand Down Expand Up @@ -260,8 +261,10 @@ struct NamedTupleParser {
if constexpr (is_required_field) {
constexpr auto current_name =
internal::nth_element_t<_i, FieldTypes...>::name();
_errors->emplace_back(Error(
"Field named '" + std::string(current_name) + "' not found."));
std::stringstream stream;
stream << "Field named '" << std::string(current_name)
<< "' not found.";
_errors->emplace_back(Error(stream.str()));
} else {
if constexpr (!std::is_const_v<ValueType>) {
::new (rfl::get<_i>(_view)) ValueType();
Expand Down
28 changes: 17 additions & 11 deletions include/rfl/parsing/Parser_tagged_union.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define RFL_PARSING_PARSER_TAGGED_UNION_HPP_

#include <map>
#include <sstream>
#include <type_traits>

#include "../Result.hpp"
Expand Down Expand Up @@ -91,10 +92,12 @@ struct Parser<R, W, TaggedUnion<_discriminator, AlternativeTypes...>,
return res;
} else {
const auto names = PossibleTags::names();
return Error("Could not parse tagged union, could not match " +
_discriminator.str() + " '" + _disc_value +
"'. The following tags are allowed: " +
internal::strings::join(", ", names));
std::stringstream stream;
stream << "Could not parse tagged union, could not match "
<< _discriminator.str() << " '" << _disc_value
<< "'. The following tags are allowed: "
<< internal::strings::join(", ", names);
return Error(stream.str());
}
}

Expand Down Expand Up @@ -122,10 +125,12 @@ struct Parser<R, W, TaggedUnion<_discriminator, AlternativeTypes...>,
};

const auto embellish_error = [&](Error&& _e) {
return Error(
"Could not parse tagged union with "
"discrimininator " +
_discriminator.str() + " '" + _disc_value + "': " + _e.what());
std::stringstream stream;
stream << "Could not parse tagged union with "
"discrimininator "
<< _discriminator.str() << " '" << _disc_value
<< "': " << _e.what();
return Error(stream.str());
};

if constexpr (no_field_names_) {
Expand Down Expand Up @@ -153,9 +158,10 @@ struct Parser<R, W, TaggedUnion<_discriminator, AlternativeTypes...>,
};

const auto embellish_error = [](const auto&) {
return Error("Could not parse tagged union: Could not find field '" +
_discriminator.str() +
"' or type of field was not a string.");
std::stringstream stream;
stream << "Could not parse tagged union: Could not find field '"
<< _discriminator.str() << "' or type of field was not a string.";
return Error(stream.str());
};

if constexpr (no_field_names_) {
Expand Down
24 changes: 14 additions & 10 deletions include/rfl/parsing/ViewReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define RFL_PARSING_VIEWREADER_HPP_

#include <array>
#include <sstream>
#include <string_view>
#include <type_traits>
#include <utility>
Expand Down Expand Up @@ -52,9 +53,10 @@ class ViewReader {
*_already_assigned = true;
auto res = Parser<R, W, T, ProcessorsType>::read(_r, _var);
if (!res) {
_errors->emplace_back(Error("Failed to parse field '" +
std::string(name) +
"': " + std::move(res.error()->what())));
std::stringstream stream;
stream << "Failed to parse field '" << std::string(name)
<< "': " << res.error()->what();
_errors->emplace_back(Error(stream.str()));
return;
}
if constexpr (std::is_pointer_v<OriginalType>) {
Expand Down Expand Up @@ -83,9 +85,10 @@ class ViewReader {
}
auto res = Parser<R, W, T, ProcessorsType>::read(_r, _var);
if (!res) {
_errors->emplace_back(Error("Failed to parse field '" +
std::string(_current_name) +
"': " + std::move(res.error()->what())));
std::stringstream stream;
stream << "Failed to parse field '" << _current_name
<< "': " << res.error()->what();
_errors->emplace_back(Error(stream.str()));
return;
}
extra_fields->emplace(std::string(_current_name), std::move(*res));
Expand All @@ -111,10 +114,11 @@ class ViewReader {
}
} else if constexpr (ProcessorsType::no_extra_fields_) {
if (!already_assigned) {
_errors->emplace_back(
Error("Value named '" + std::string(_current_name) +
"' not used. Remove the rfl::NoExtraFields processor or add "
"rfl::ExtraFields to avoid this error message."));
std::stringstream stream;
stream << "Value named '" << _current_name
<< "' not used. Remove the rfl::NoExtraFields processor or add "
"rfl::ExtraFields to avoid this error message.";
_errors->emplace_back(Error(stream.str()));
}
}
}
Expand Down
24 changes: 14 additions & 10 deletions include/rfl/parsing/ViewReaderWithDefault.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define RFL_PARSING_VIEWREADERWITHDEFAULT_HPP_

#include <array>
#include <sstream>
#include <string_view>
#include <type_traits>
#include <utility>
Expand Down Expand Up @@ -48,9 +49,10 @@ class ViewReaderWithDefault {
*_already_assigned = true;
auto res = Parser<R, W, T, ProcessorsType>::read(_r, _var);
if (!res) {
_errors->emplace_back(Error("Failed to parse field '" +
std::string(name) +
"': " + std::move(res.error()->what())));
std::stringstream stream;
stream << "Failed to parse field '" << std::string(name)
<< "': " << res.error()->what();
_errors->emplace_back(Error(stream.str()));
return;
}
if constexpr (std::is_pointer_v<OriginalType>) {
Expand All @@ -73,9 +75,10 @@ class ViewReaderWithDefault {
std::remove_pointer_t<typename ExtraFieldsType::Type>>;
auto res = Parser<R, W, T, ProcessorsType>::read(_r, _var);
if (!res) {
_errors->emplace_back(Error("Failed to parse field '" +
std::string(_current_name) +
"': " + std::move(res.error()->what())));
std::stringstream stream;
stream << "Failed to parse field '" << _current_name
<< "': " << res.error()->what();
_errors->emplace_back(Error(stream.str()));
return;
}
extra_fields->emplace(std::string(_current_name), std::move(*res));
Expand All @@ -100,10 +103,11 @@ class ViewReaderWithDefault {
}
} else if constexpr (ProcessorsType::no_extra_fields_) {
if (!already_assigned) {
_errors->emplace_back(
Error("Value named '" + std::string(_current_name) +
"' not used. Remove the rfl::NoExtraFields processor or add "
"rfl::ExtraFields to avoid this error message."));
std::stringstream stream;
stream << "Value named '" << std::string(_current_name)
<< "' not used. Remove the rfl::NoExtraFields processor or add "
"rfl::ExtraFields to avoid this error message.";
_errors->emplace_back(Error(stream.str()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define RFL_PARSING_VIEWREADERWITHDEFAULTANDSTRIPPEDFIELDNAMES_HPP_

#include <array>
#include <sstream>
#include <string_view>
#include <type_traits>
#include <utility>
Expand Down Expand Up @@ -30,8 +31,10 @@ class ViewReaderWithDefaultAndStrippedFieldNames {
/// used when the field names are stripped.
std::optional<Error> read(const InputVarType& _var) const {
if (i_ == size_) {
return Error("Expected a maximum of " + std::to_string(size_) +
" fields, but got at least one more.");
std::stringstream stream;
stream << "Expected a maximum of " << std::to_string(size_)
<< " fields, but got at least one more.";
return Error(stream.str());
}
assign_to_field_i(*r_, _var, view_, errors_, i_,
std::make_integer_sequence<int, size_>());
Expand All @@ -51,9 +54,10 @@ class ViewReaderWithDefaultAndStrippedFieldNames {
if (_i == i) {
auto res = Parser<R, W, T, ProcessorsType>::read(_r, _var);
if (!res) {
_errors->emplace_back(Error("Failed to parse field '" +
std::string(name) +
"': " + std::move(res.error()->what())));
std::stringstream stream;
stream << "Failed to parse field '" << std::string(name)
<< "': " << res.error()->what();
_errors->emplace_back(Error(stream.str()));
return;
}
if constexpr (std::is_pointer_v<OriginalType>) {
Expand Down
14 changes: 9 additions & 5 deletions include/rfl/parsing/ViewReaderWithStrippedFieldNames.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define RFL_PARSING_VIEWREADERWITHSTRIPPEDFIELDNAMES_HPP_

#include <array>
#include <sstream>
#include <string_view>
#include <type_traits>
#include <utility>
Expand Down Expand Up @@ -37,8 +38,10 @@ class ViewReaderWithStrippedFieldNames {
/// used when the field names are stripped.
std::optional<Error> read(const InputVarType& _var) const {
if (i_ == size_) {
return Error("Expected a maximum of " + std::to_string(size_) +
" fields, but got at least one more.");
std::stringstream stream;
stream << "Expected a maximum of " << std::to_string(size_)
<< " fields, but got at least one more.";
return Error(stream.str());
}
assign_to_field_i(*r_, _var, view_, errors_, found_, set_, i_,
std::make_integer_sequence<int, size_>());
Expand All @@ -60,9 +63,10 @@ class ViewReaderWithStrippedFieldNames {
std::get<i>(*_found) = true;
auto res = Parser<R, W, T, ProcessorsType>::read(_r, _var);
if (!res) {
_errors->emplace_back(Error("Failed to parse field '" +
std::string(name) +
"': " + std::move(res.error()->what())));
std::stringstream stream;
stream << "Failed to parse field '" << std::string(name)
<< "': " << res.error()->what();
_errors->emplace_back(Error(stream.str()));
return;
}
if constexpr (std::is_pointer_v<OriginalType>) {
Expand Down
Loading
Loading