aboutsummaryrefslogtreecommitdiff
path: root/bbot/manifest.cxx
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2017-04-07 19:17:47 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2017-04-07 19:17:47 +0300
commit294058e21e33c7bfbb852f8f1a73b1555aa3eace (patch)
treeb39227b0315ff98e7e52bd210c52dffca712c280 /bbot/manifest.cxx
parentef0e6c709051fc46968d64dcf1c86bd197fe45c2 (diff)
Add to_result_status() and to_string(result_status)
Diffstat (limited to 'bbot/manifest.cxx')
-rw-r--r--bbot/manifest.cxx113
1 files changed, 69 insertions, 44 deletions
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 <cstddef> // size_t
#include <utility> // move()
#include <cstdint> // uint64_t
-#include <algorithm> // find()
+#include <stdexcept> // invalid_argument
#include <butl/utility> // digit()
#include <butl/tab-parser>
@@ -32,24 +32,60 @@ namespace bbot
using strings = vector<string>;
- 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<size_t> (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<machine_type> (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<size_t> (*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<result_status> (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<size_t> (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<size_t> (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.
//