Skip to content

Commit

Permalink
RecordReader: Simplify error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
glebm committed Nov 18, 2023
1 parent 17f95a0 commit 7e70c3b
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions Source/data/record_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ class RecordReader {
{
advance();
DataFileField field = *it_;
if (tl::expected<void, DataFileField::Error> result = field.parseInt(out); !result.has_value()) {
DataFile::reportFatalFieldError(result.error(), filename_, name, field);
}
failOnError(field.parseInt(out), name, field);
}

template <typename T>
Expand All @@ -42,29 +40,23 @@ class RecordReader {
advance();
DataFileField field = *it_;
if (field.value().empty()) return;
if (tl::expected<void, DataFileField::Error> result = field.parseInt(out); !result.has_value()) {
DataFile::reportFatalFieldError(result.error(), filename_, name, field);
}
failOnError(field.parseInt(out), name, field);
}

template <typename T, size_t N>
void readIntArray(std::string_view name, T (&out)[N])
{
advance();
DataFileField field = *it_;
if (tl::expected<void, DataFileField::Error> result = field.parseIntArray(out); !result.has_value()) {
DataFile::reportFatalFieldError(result.error(), filename_, name, field);
}
failOnError(field.parseIntArray(out), name, field);
}

template <typename T, size_t N>
void readIntArray(std::string_view name, std::array<T, N> &out)
{
advance();
DataFileField field = *it_;
if (tl::expected<void, DataFileField::Error> result = field.parseIntArray(out); !result.has_value()) {
DataFile::reportFatalFieldError(result.error(), filename_, name, field);
}
failOnError(field.parseIntArray(out), name, field);
}

template <typename T>
Expand All @@ -73,18 +65,14 @@ class RecordReader {
{
advance();
DataFileField field = *it_;
if (tl::expected<void, DataFileField::Error> result = field.parseFixed6(out); !result.has_value()) {
DataFile::reportFatalFieldError(result.error(), filename_, name, field);
}
failOnError(field.parseFixed6(out), name, field);
}

void readBool(std::string_view name, bool &out)
{
advance();
DataFileField field = *it_;
if (tl::expected<void, DataFileField::Error> result = field.parseBool(out); !result.has_value()) {
DataFile::reportFatalFieldError(result.error(), filename_, name, field);
}
failOnError(field.parseBool(out), name, field);
}

void readString(std::string_view name, std::string &out)
Expand All @@ -98,21 +86,18 @@ class RecordReader {
{
advance();
DataFileField field = *it_;
if (tl::expected<T, std::string> result = parseFn(field.value()); result.has_value()) {
out = *std::move(result);
} else {
DataFile::reportFatalFieldError(DataFileField::Error::InvalidValue, filename_, name, field, result.error());
}
tl::expected<T, std::string> result = parseFn(field.value());
failOnError(result, name, field, DataFileField::Error::InvalidValue);
out = *std::move(result);
}

template <typename T, typename F>
void readEnumList(std::string_view name, T &out, F &&parseFn)
{
advance();
DataFileField field = *it_;
if (tl::expected<void, std::string> result = field.parseEnumList(out, std::forward<F>(parseFn)); !result.has_value()) {
DataFile::reportFatalFieldError(DataFileField::Error::InvalidValue, filename_, name, field, result.error());
}
failOnError(field.parseEnumList(out, std::forward<F>(parseFn)),
name, field, DataFileField::Error::InvalidValue);
}

std::string_view value()
Expand All @@ -125,6 +110,22 @@ class RecordReader {
void advance();

private:
template <typename T>
void failOnError(const tl::expected<T, DataFileField::Error> &result, std::string_view name, const DataFileField &field)
{
if (!result.has_value()) {
DataFile::reportFatalFieldError(result.error(), filename_, name, field);
}
}

template <typename T>
void failOnError(const tl::expected<T, std::string> &result, std::string_view name, const DataFileField &field, DataFileField::Error error)
{
if (!result.has_value()) {
DataFile::reportFatalFieldError(error, filename_, name, field, result.error());
}
}

FieldIterator it_;
const FieldIterator end_;
std::string_view filename_;
Expand Down

0 comments on commit 7e70c3b

Please sign in to comment.