replace error_mode String and Bool with error_string and error_bool

This commit is contained in:
ado 2021-01-01 21:57:14 +01:00
parent 0ec0c7dd6a
commit 0487f33eb1
5 changed files with 22 additions and 21 deletions

View File

@ -79,7 +79,7 @@ $ make test
## Error handling ## Error handling
Detailed error messages can be accessed via the **error_msg** method, and to 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: the **set_error_mode** method:
```cpp ```cpp
void parser::set_error_mode(ss::error_mode); void parser::set_error_mode(ss::error_mode);
@ -88,7 +88,7 @@ bool parser::valid();
bool parser::eof(); bool parser::eof();
``` ```
Error messages can always be disabled by setting the error mode to 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 would return **false** if the file could not be opened, or if the conversion
could not be made (invalid types, invalid number of columns, ...). 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. 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"; return "number not even";
} }
}; };
```
// ... ```cpp
// only even numbers will pass // only even numbers will pass
// returns std::tuple<std::string, int> // returns std::tuple<std::string, int>
auto [name, age] = p.get_next<std::string, even<int>, void>(); auto [name, age] = p.get_next<std::string, even<int>, void>();
```
## Custom conversions

View File

@ -104,7 +104,7 @@ template <typename... Ts>
constexpr bool tied_class_v = tied_class<Ts...>::value; constexpr bool tied_class_v = tied_class<Ts...>::value;
// the error can be set inside a string, or a bool // 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 // converter
@ -163,7 +163,7 @@ public:
} }
bool valid() const { bool valid() const {
return (error_mode_ == error_mode::String) ? string_error_.empty() return (error_mode_ == error_mode::error_string) ? string_error_.empty()
: bool_error_ == false; : bool_error_ == false;
} }
@ -216,7 +216,7 @@ private:
} }
void set_error_invalid_conversion(const string_range msg, size_t pos) { 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_.clear();
string_error_.append("invalid conversion for parameter ") string_error_.append("invalid conversion for parameter ")
.append(error_sufix(msg, pos)); .append(error_sufix(msg, pos));
@ -227,7 +227,7 @@ private:
void set_error_validate(const char* const error, const string_range msg, void set_error_validate(const char* const error, const string_range msg,
size_t pos) { size_t pos) {
if (error_mode_ == error_mode::String) { if (error_mode_ == error_mode::error_string) {
string_error_.clear(); string_error_.clear();
string_error_.append(error).append(" ").append( string_error_.append(error).append(" ").append(
error_sufix(msg, pos)); error_sufix(msg, pos));
@ -237,7 +237,7 @@ private:
} }
void set_error_number_of_colums(size_t expected_pos, size_t pos) { 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_.clear();
string_error_.append("invalid number of columns, expected: ") string_error_.append("invalid number of columns, expected: ")
.append(std::to_string(expected_pos)) .append(std::to_string(expected_pos))
@ -370,7 +370,7 @@ private:
std::vector<string_range> input_; std::vector<string_range> input_;
std::string string_error_; std::string string_error_;
bool bool_error_; bool bool_error_;
enum error_mode error_mode_ { error_mode::Bool }; enum error_mode error_mode_ { error_mode::error_bool };
}; };
template <> template <>

View File

@ -33,7 +33,7 @@ public:
} }
bool valid() const { bool valid() const {
return (error_mode_ == error_mode::String) ? string_error_.empty() return (error_mode_ == error_mode::error_string) ? string_error_.empty()
: bool_error_ == false; : bool_error_ == false;
} }
@ -285,7 +285,7 @@ private:
} }
void set_error_failed_check() { 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."); string_error_.append(file_name_).append(" failed check.");
} else { } else {
bool_error_ = true; bool_error_ = true;
@ -298,7 +298,7 @@ private:
} }
void set_error_eof_reached() { 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."); string_error_.append(file_name_).append(" reached end of file.");
} else { } else {
bool_error_ = true; bool_error_ = true;
@ -306,7 +306,7 @@ private:
} }
void set_error_invalid_conversion() { void set_error_invalid_conversion() {
if (error_mode_ == error_mode::String) { if (error_mode_ == error_mode::error_string) {
string_error_.append(file_name_) string_error_.append(file_name_)
.append(" ") .append(" ")
.append(std::to_string(line_number_)) .append(std::to_string(line_number_))
@ -328,7 +328,7 @@ private:
const std::string delim_; const std::string delim_;
std::string string_error_; std::string string_error_;
bool bool_error_; bool bool_error_;
error_mode error_mode_{error_mode::Bool}; error_mode error_mode_{error_mode::error_bool};
converter converter_; converter converter_;
converter::split_input split_input_; converter::split_input split_input_;
FILE* file_{nullptr}; FILE* file_{nullptr};

View File

@ -330,7 +330,7 @@ TEST_CASE("testing error mode") {
CHECK(!c.valid()); CHECK(!c.valid());
CHECK(c.error_msg().empty()); CHECK(c.error_msg().empty());
c.set_error_mode(ss::error_mode::String); c.set_error_mode(ss::error_mode::error_string);
c.convert<int>("junk"); c.convert<int>("junk");
CHECK(!c.valid()); CHECK(!c.valid());
CHECK(!c.error_msg().empty()); CHECK(!c.error_msg().empty());

View File

@ -186,7 +186,7 @@ TEST_CASE("testing composite conversion") {
} }
ss::parser p{f.name, ","}; 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 fail = [] { FAIL(""); };
auto expect_error = [](auto error) { CHECK(!error.empty()); }; auto expect_error = [](auto error) { CHECK(!error.empty()); };
@ -455,7 +455,7 @@ TEST_CASE("testing error mode") {
CHECK(!p.valid()); CHECK(!p.valid());
CHECK(p.error_msg().empty()); 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()); REQUIRE(!p.eof());
p.get_next<int>(); p.get_next<int>();