diff --git a/include/ss/converter.hpp b/include/ss/converter.hpp index 76a4950..65548dd 100644 --- a/include/ss/converter.hpp +++ b/include/ss/converter.hpp @@ -344,39 +344,6 @@ private: } } - void set_error_invalid_mapping() { - constexpr static auto error_msg = "received empty mapping"; - - if constexpr (string_error) { - error_.clear(); - error_.append(error_msg); - } else if constexpr (throw_on_error) { - throw ss::exception{error_msg}; - } else { - error_ = true; - } - } - - void set_error_mapping_out_of_range(size_t maximum_index, - size_t number_of_columnts) { - constexpr static auto error_msg1 = "maximum index: "; - constexpr static auto error_msg2 = ", greater than number of columns: "; - - if constexpr (string_error) { - error_.clear(); - error_.append(error_msg1) - .append(std::to_string(maximum_index)) - .append(error_msg2) - .append(std::to_string(number_of_columnts)); - } else if constexpr (throw_on_error) { - throw ss::exception{error_msg1 + std::to_string(maximum_index) + - error_msg2 + - std::to_string(number_of_columnts)}; - } else { - error_ = true; - } - } - //////////////// // convert implementation //////////////// @@ -433,19 +400,9 @@ private: return column_mappings_[tuple_position]; } + // assumes positions are valid and the vector is not empty void set_column_mapping(std::vector positions, size_t number_of_columns) { - if (positions.empty()) { - set_error_invalid_mapping(); - return; - } - - auto max_index = *std::max_element(positions.begin(), positions.end()); - if (max_index >= number_of_columns) { - set_error_mapping_out_of_range(max_index, number_of_columns); - return; - } - column_mappings_ = positions; number_of_columns_ = number_of_columns; } diff --git a/include/ss/parser.hpp b/include/ss/parser.hpp index bddfe80..9d2188d 100644 --- a/include/ss/parser.hpp +++ b/include/ss/parser.hpp @@ -138,6 +138,12 @@ public: } auto fields = std::vector{fields_args...}; + + if (fields.empty()) { + set_error_empty_mapping(); + return; + } + std::vector column_mappings; for (const auto& field : fields) { @@ -159,6 +165,7 @@ public: reader_.converter_.set_column_mapping(column_mappings, header_.size()); reader_.next_line_converter_.set_column_mapping(column_mappings, header_.size()); + if (line() == 0) { ignore_next(); } @@ -485,15 +492,11 @@ private: void set_error_invalid_conversion() { if constexpr (string_error) { - // TODO remove buffer from error msg error_.append(file_name_) .append(" ") .append(std::to_string(reader_.line_number_)) .append(": ") - .append(reader_.converter_.error_msg()) - .append(": \"") - .append(reader_.buffer_) - .append("\""); + .append(reader_.converter_.error_msg()); } else if constexpr (!throw_on_error) { error_ = true; } @@ -538,6 +541,19 @@ private: } } + void set_error_empty_mapping() { + constexpr static auto error_msg = "received empty mapping"; + + if constexpr (string_error) { + error_.clear(); + error_.append(error_msg); + } else if constexpr (throw_on_error) { + throw ss::exception{error_msg}; + } else { + error_ = true; + } + } + //////////////// // line reading //////////////// diff --git a/test/test_converter.cpp b/test/test_converter.cpp index d0d18af..6dd4562 100644 --- a/test/test_converter.cpp +++ b/test/test_converter.cpp @@ -978,3 +978,4 @@ TEST_CASE("converter test invalid split conversions with exceptions") { buff(R"(just,some,2,"strings\")"))); CHECK(c.unterminated_quote()); } + diff --git a/test/test_parser.cpp b/test/test_parser.cpp index 6581b20..393e624 100644 --- a/test/test_parser.cpp +++ b/test/test_parser.cpp @@ -1168,14 +1168,12 @@ void test_invalid_fields_impl(const std::vector& lines, } out.close(); - /* TODO test { // No fields specified ss::parser p{f.name, ","}; - p.use_fields(); - CHECK(!p.valid()); + auto command = [&] { p.use_fields(); }; + expect_error_on_command(p, command); } - */ { // Unknown field