From 0487f33eb10615f3c01df1924e1ea77c41d7fd18 Mon Sep 17 00:00:00 2001 From: ado Date: Fri, 1 Jan 2021 21:57:14 +0100 Subject: [PATCH] replace error_mode String and Bool with error_string and error_bool --- README.md | 11 ++++++----- include/ss/converter.hpp | 14 +++++++------- include/ss/parser.hpp | 12 ++++++------ test/test_converter.cpp | 2 +- test/test_parser.cpp | 4 ++-- 5 files changed, 22 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index edacc54..e0b80bd 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ $ make test ## Error handling Detailed error messages can be accessed via the **error_msg** method, and to -enable them the error mode has to be changed to **error_mode::String** using +enable them the error mode has to be changed to **error_mode::error_string** using the **set_error_mode** method: ```cpp void parser::set_error_mode(ss::error_mode); @@ -88,7 +88,7 @@ bool parser::valid(); bool parser::eof(); ``` Error messages can always be disabled by setting the error mode to -**error_mode::Bool**. An error can be detected using the **valid** method which +**error_mode::error_bool**. An error can be detected using the **valid** method which would return **false** if the file could not be opened, or if the conversion could not be made (invalid types, invalid number of columns, ...). The **eof** method can be used to detect if the end of the file was reached. @@ -238,9 +238,10 @@ struct even { return "number not even"; } }; - -// ... +``` +```cpp // only even numbers will pass // returns std::tuple auto [name, age] = p.get_next, void>(); - +``` +## Custom conversions diff --git a/include/ss/converter.hpp b/include/ss/converter.hpp index 41a83ee..4c6c0c9 100644 --- a/include/ss/converter.hpp +++ b/include/ss/converter.hpp @@ -104,7 +104,7 @@ template constexpr bool tied_class_v = tied_class::value; // the error can be set inside a string, or a bool -enum class error_mode { String, Bool }; +enum class error_mode { error_string, error_bool }; //////////////// // converter @@ -163,8 +163,8 @@ public: } bool valid() const { - return (error_mode_ == error_mode::String) ? string_error_.empty() - : bool_error_ == false; + return (error_mode_ == error_mode::error_string) ? string_error_.empty() + : bool_error_ == false; } const std::string& error_msg() const { @@ -216,7 +216,7 @@ private: } void set_error_invalid_conversion(const string_range msg, size_t pos) { - if (error_mode_ == error_mode::String) { + if (error_mode_ == error_mode::error_string) { string_error_.clear(); string_error_.append("invalid conversion for parameter ") .append(error_sufix(msg, pos)); @@ -227,7 +227,7 @@ private: void set_error_validate(const char* const error, const string_range msg, size_t pos) { - if (error_mode_ == error_mode::String) { + if (error_mode_ == error_mode::error_string) { string_error_.clear(); string_error_.append(error).append(" ").append( error_sufix(msg, pos)); @@ -237,7 +237,7 @@ private: } void set_error_number_of_colums(size_t expected_pos, size_t pos) { - if (error_mode_ == error_mode::String) { + if (error_mode_ == error_mode::error_string) { string_error_.clear(); string_error_.append("invalid number of columns, expected: ") .append(std::to_string(expected_pos)) @@ -370,7 +370,7 @@ private: std::vector input_; std::string string_error_; bool bool_error_; - enum error_mode error_mode_ { error_mode::Bool }; + enum error_mode error_mode_ { error_mode::error_bool }; }; template <> diff --git a/include/ss/parser.hpp b/include/ss/parser.hpp index 47087d3..e12ad32 100644 --- a/include/ss/parser.hpp +++ b/include/ss/parser.hpp @@ -33,8 +33,8 @@ public: } bool valid() const { - return (error_mode_ == error_mode::String) ? string_error_.empty() - : bool_error_ == false; + return (error_mode_ == error_mode::error_string) ? string_error_.empty() + : bool_error_ == false; } void set_error_mode(error_mode mode) { @@ -285,7 +285,7 @@ private: } void set_error_failed_check() { - if (error_mode_ == error_mode::String) { + if (error_mode_ == error_mode::error_string) { string_error_.append(file_name_).append(" failed check."); } else { bool_error_ = true; @@ -298,7 +298,7 @@ private: } void set_error_eof_reached() { - if (error_mode_ == error_mode::String) { + if (error_mode_ == error_mode::error_string) { string_error_.append(file_name_).append(" reached end of file."); } else { bool_error_ = true; @@ -306,7 +306,7 @@ private: } void set_error_invalid_conversion() { - if (error_mode_ == error_mode::String) { + if (error_mode_ == error_mode::error_string) { string_error_.append(file_name_) .append(" ") .append(std::to_string(line_number_)) @@ -328,7 +328,7 @@ private: const std::string delim_; std::string string_error_; bool bool_error_; - error_mode error_mode_{error_mode::Bool}; + error_mode error_mode_{error_mode::error_bool}; converter converter_; converter::split_input split_input_; FILE* file_{nullptr}; diff --git a/test/test_converter.cpp b/test/test_converter.cpp index 3e55d8d..a959bb2 100644 --- a/test/test_converter.cpp +++ b/test/test_converter.cpp @@ -330,7 +330,7 @@ TEST_CASE("testing error mode") { CHECK(!c.valid()); CHECK(c.error_msg().empty()); - c.set_error_mode(ss::error_mode::String); + c.set_error_mode(ss::error_mode::error_string); c.convert("junk"); CHECK(!c.valid()); CHECK(!c.error_msg().empty()); diff --git a/test/test_parser.cpp b/test/test_parser.cpp index d7cbb74..bb48cb4 100644 --- a/test/test_parser.cpp +++ b/test/test_parser.cpp @@ -186,7 +186,7 @@ TEST_CASE("testing composite conversion") { } ss::parser p{f.name, ","}; - p.set_error_mode(ss::error_mode::String); + p.set_error_mode(ss::error_mode::error_string); auto fail = [] { FAIL(""); }; auto expect_error = [](auto error) { CHECK(!error.empty()); }; @@ -455,7 +455,7 @@ TEST_CASE("testing error mode") { CHECK(!p.valid()); CHECK(p.error_msg().empty()); - p.set_error_mode(ss::error_mode::String); + p.set_error_mode(ss::error_mode::error_string); REQUIRE(!p.eof()); p.get_next();