From 294058e21e33c7bfbb852f8f1a73b1555aa3eace Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Fri, 7 Apr 2017 19:17:47 +0300 Subject: Add to_result_status() and to_string(result_status) --- bbot/manifest | 12 ++++-- bbot/manifest.cxx | 113 +++++++++++++++++++++++++++++++++--------------------- 2 files changed, 78 insertions(+), 47 deletions(-) (limited to 'bbot') diff --git a/bbot/manifest b/bbot/manifest index 2cedbb6..4a5d449 100644 --- a/bbot/manifest +++ b/bbot/manifest @@ -7,7 +7,7 @@ #include #include -#include +#include #include #include @@ -146,8 +146,14 @@ namespace bbot abnormal }; - std::ostream& - operator<< (std::ostream&, result_status); + LIBBBOT_EXPORT std::string + to_string (result_status); + + LIBBBOT_EXPORT result_status + to_result_status (const std::string&); // May throw invalid_argument. + + inline std::ostream& + operator<< (std::ostream& os, result_status s) {return os << to_string (s);} inline result_status& operator |= (result_status& l, result_status r) diff --git a/bbot/manifest.cxx b/bbot/manifest.cxx index a40d633..b18389e 100644 --- a/bbot/manifest.cxx +++ b/bbot/manifest.cxx @@ -11,7 +11,7 @@ #include // size_t #include // move() #include // uint64_t -#include // find() +#include // invalid_argument #include // digit() #include @@ -32,24 +32,60 @@ namespace bbot using strings = vector; - static const strings machine_type_names ({"kvm", "nspawn"}); + // result_status + // + string + to_string (result_status s) + { + switch (s) + { + case result_status::success: return "success"; + case result_status::warning: return "warning"; + case result_status::error: return "error"; + case result_status::abort: return "abort"; + case result_status::abnormal: return "abnormal"; + } + + assert (false); + return string (); + } + + result_status + to_result_status (const string& s) + { + if (s == "success") return result_status::success; + else if (s == "warning") return result_status::warning; + else if (s == "error") return result_status::error; + else if (s == "abort") return result_status::abort; + else if (s == "abnormal") return result_status::abnormal; + else throw invalid_argument ("invalid result status '" + s + "'"); + } - static const strings result_status_names ({ - "success", - "warning", - "error", - "abort", - "abnormal"}); + // machine_type + // + static string + to_string (machine_type t) + { + switch (t) + { + case machine_type::kvm: return "kvm"; + case machine_type::nspawn: return "nspawn"; + } - ostream& - operator<< (ostream& o, result_status s) + assert (false); + return string (); + } + + machine_type + to_machine_type (const string& t) { - size_t i (static_cast (s)); - assert (i < result_status_names.size ()); - o << result_status_names[i]; - return o; + if (t == "kvm") return machine_type::kvm; + else if (t == "nspawn") return machine_type::nspawn; + else throw invalid_argument ("invalid machine type '" + t + "'"); } + // Utility functions + // inline static bool valid_sha256 (const string& s) noexcept { @@ -143,14 +179,14 @@ namespace bbot if (type) bad_name ("machine type redefinition"); - auto b (machine_type_names.cbegin ()); - auto e (machine_type_names.cend ()); - auto i (find (b, e, v)); - - if (i == e) + try + { + type = to_machine_type (v); + } + catch (const invalid_argument&) + { bad_value ("invalid machine type"); - - type = static_cast (i - b); + } } else if (!iu) bad_name ("unknown name '" + n + "' in machine manifest"); @@ -183,11 +219,7 @@ namespace bbot s.next ("summary", summary); if (type) - { - size_t v (static_cast (*type)); - assert (v < machine_type_names.size ()); - s.next ("type", machine_type_names[v]); - } + s.next ("type", to_string (*type)); s.next ("", ""); // End of manifest. } @@ -645,7 +677,8 @@ namespace bbot throw parsing (p.name (), nv.name_line, nv.name_column, d); }; - auto bad_value = [&p, &nv] (const string& d, size_t offset = 0) + auto bad_value = + [&p, &nv] (const string& d, size_t offset = 0) [[noreturn]] { throw parsing (p.name (), nv.value_line, nv.value_column + offset, d); }; @@ -653,14 +686,14 @@ namespace bbot auto result_stat = [&bad_value] (const string& v, const string& what) -> result_status { - auto b (result_status_names.cbegin ()); - auto e (result_status_names.cend ()); - auto i (find (b, e, v)); - - if (i == e) + try + { + return to_result_status (v); + } + catch (const invalid_argument&) + { bad_value ("invalid " + what); - - return static_cast (i - b); + } }; // Make sure this is the start and we support the version. @@ -807,20 +840,12 @@ namespace bbot s.next ("", "1"); // Start of manifest. s.next ("name", name); s.next ("version", version.string ()); - - size_t v (static_cast (status)); - assert (v < result_status_names.size ()); - s.next ("status", result_status_names[v]); + s.next ("status", to_string (status)); // Serialize *-status values. // for (const auto& r: results) - { - size_t rs (static_cast (r.status)); - assert (rs < result_status_names.size ()); - - s.next (r.operation + "-status", result_status_names[rs]); - } + s.next (r.operation + "-status", to_string (r.status)); // Serialize *-log values. // -- cgit v1.1