init: use Result<T> for the parsing functions
Test: boot bullhead
Change-Id: I7f00c5f0f54dd4fe05df73e1d6a89b56d788e113
diff --git a/init/keyword_map.h b/init/keyword_map.h
index 481d637..c95fc73 100644
--- a/init/keyword_map.h
+++ b/init/keyword_map.h
@@ -22,6 +22,8 @@
#include <android-base/stringprintf.h>
+#include "result.h"
+
namespace android {
namespace init {
@@ -34,20 +36,17 @@
virtual ~KeywordMap() {
}
- const Function FindFunction(const std::vector<std::string>& args, std::string* err) const {
+ const Result<Function> FindFunction(const std::vector<std::string>& args) const {
using android::base::StringPrintf;
- if (args.empty()) {
- *err = "keyword needed, but not provided";
- return nullptr;
- }
+ if (args.empty()) return Error() << "Keyword needed, but not provided";
+
auto& keyword = args[0];
auto num_args = args.size() - 1;
auto function_info_it = map().find(keyword);
if (function_info_it == map().end()) {
- *err = StringPrintf("invalid keyword '%s'", keyword.c_str());
- return nullptr;
+ return Error() << StringPrintf("Invalid keyword '%s'", keyword.c_str());
}
auto function_info = function_info_it->second;
@@ -55,22 +54,18 @@
auto min_args = std::get<0>(function_info);
auto max_args = std::get<1>(function_info);
if (min_args == max_args && num_args != min_args) {
- *err = StringPrintf("%s requires %zu argument%s",
- keyword.c_str(), min_args,
- (min_args > 1 || min_args == 0) ? "s" : "");
- return nullptr;
+ return Error() << StringPrintf("%s requires %zu argument%s", keyword.c_str(), min_args,
+ (min_args > 1 || min_args == 0) ? "s" : "");
}
if (num_args < min_args || num_args > max_args) {
if (max_args == std::numeric_limits<decltype(max_args)>::max()) {
- *err = StringPrintf("%s requires at least %zu argument%s",
- keyword.c_str(), min_args,
- min_args > 1 ? "s" : "");
+ return Error() << StringPrintf("%s requires at least %zu argument%s",
+ keyword.c_str(), min_args, min_args > 1 ? "s" : "");
} else {
- *err = StringPrintf("%s requires between %zu and %zu arguments",
- keyword.c_str(), min_args, max_args);
+ return Error() << StringPrintf("%s requires between %zu and %zu arguments",
+ keyword.c_str(), min_args, max_args);
}
- return nullptr;
}
return std::get<Function>(function_info);