summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/buildtool/common/TARGETS2
-rw-r--r--src/buildtool/common/location.cpp16
-rw-r--r--src/buildtool/common/location.hpp9
-rw-r--r--src/buildtool/main/serve.cpp6
-rw-r--r--src/other_tools/just_mr/rc.cpp12
5 files changed, 23 insertions, 22 deletions
diff --git a/src/buildtool/common/TARGETS b/src/buildtool/common/TARGETS
index c20ea4e5..dd8fdbd8 100644
--- a/src/buildtool/common/TARGETS
+++ b/src/buildtool/common/TARGETS
@@ -150,7 +150,7 @@
, "name": ["location"]
, "hdrs": ["location.hpp"]
, "srcs": ["location.cpp"]
- , "deps": [["@", "json", "", "json"]]
+ , "deps": [["@", "json", "", "json"], ["src/utils/cpp", "expected"]]
, "stage": ["src", "buildtool", "common"]
, "private-deps":
[ ["@", "fmt", "", "fmt"]
diff --git a/src/buildtool/common/location.cpp b/src/buildtool/common/location.cpp
index bc3a57dc..a266069d 100644
--- a/src/buildtool/common/location.cpp
+++ b/src/buildtool/common/location.cpp
@@ -22,9 +22,10 @@
auto ReadLocationObject(nlohmann::json const& location,
std::optional<std::filesystem::path> const& ws_root)
- -> std::variant<std::string, std::optional<location_res_t>> {
+ -> expected<std::optional<location_res_t>, std::string> {
if (not location.contains("path") or not location.contains("root")) {
- return fmt::format("Malformed location object: {}", location.dump(-1));
+ return unexpected(
+ fmt::format("Malformed location object: {}", location.dump(-1)));
}
auto root = location["root"].get<std::string>();
auto path = location["path"].get<std::string>();
@@ -37,7 +38,7 @@ auto ReadLocationObject(nlohmann::json const& location,
Logger::Log(LogLevel::Warning,
"Not in workspace root, ignoring location {}.",
location.dump(-1));
- return std::nullopt;
+ return std::optional<location_res_t>{std::nullopt};
}
root_path = *ws_root;
}
@@ -47,8 +48,9 @@ auto ReadLocationObject(nlohmann::json const& location,
if (root == "system") {
root_path = FileSystemManager::GetCurrentDirectory().root_path();
}
- return std::make_pair(std::filesystem::weakly_canonical(
- std::filesystem::absolute(root_path / path)),
- std::filesystem::weakly_canonical(
- std::filesystem::absolute(root_path / base)));
+ return std::optional<location_res_t>{
+ std::make_pair(std::filesystem::weakly_canonical(
+ std::filesystem::absolute(root_path / path)),
+ std::filesystem::weakly_canonical(
+ std::filesystem::absolute(root_path / base)))};
}
diff --git a/src/buildtool/common/location.hpp b/src/buildtool/common/location.hpp
index 2e9d3841..c3788339 100644
--- a/src/buildtool/common/location.hpp
+++ b/src/buildtool/common/location.hpp
@@ -17,17 +17,16 @@
#include <string>
#include <unordered_set>
#include <utility>
-#include <variant>
#include "nlohmann/json.hpp"
+#include "src/utils/cpp/expected.hpp"
using location_res_t = std::pair<std::filesystem::path, std::filesystem::path>;
/// \brief Parse a location object stored in a JSON object.
-/// \returns An error + data union, where at index 0 is the string message on
-/// fatal failure, and at index 1 is an optional parsed location (nullopt if
-/// location should be ignored) on success.
+/// \returns optional parsed location (nullopt if location should be ignored) on
+/// success or unexpected error as string.
[[nodiscard]] auto ReadLocationObject(
nlohmann::json const& location,
std::optional<std::filesystem::path> const& ws_root)
- -> std::variant<std::string, std::optional<location_res_t>>;
+ -> expected<std::optional<location_res_t>, std::string>;
diff --git a/src/buildtool/main/serve.cpp b/src/buildtool/main/serve.cpp
index 0eaf174e..e21eac0f 100644
--- a/src/buildtool/main/serve.cpp
+++ b/src/buildtool/main/serve.cpp
@@ -45,11 +45,11 @@ namespace {
-> std::optional<std::pair<std::filesystem::path, std::filesystem::path>> {
if (location.IsNotNull()) {
auto res = ReadLocationObject(location->ToJson(), std::nullopt);
- if (res.index() == 0) {
- Logger::Log(LogLevel::Error, std::get<0>(res));
+ if (not res) {
+ Logger::Log(LogLevel::Error, res.error());
std::exit(kExitFailure);
}
- return std::get<1>(res);
+ return *res;
}
return std::nullopt;
}
diff --git a/src/other_tools/just_mr/rc.cpp b/src/other_tools/just_mr/rc.cpp
index 89fb8ef2..0a5a171a 100644
--- a/src/other_tools/just_mr/rc.cpp
+++ b/src/other_tools/just_mr/rc.cpp
@@ -37,11 +37,11 @@ namespace {
-> std::optional<std::pair<std::filesystem::path, std::filesystem::path>> {
if (location.IsNotNull()) {
auto res = ReadLocationObject(location->ToJson(), ws_root);
- if (res.index() == 0) {
- Logger::Log(LogLevel::Error, std::get<0>(res));
+ if (not res) {
+ Logger::Log(LogLevel::Error, res.error());
std::exit(kExitConfigError);
}
- return std::get<1>(res);
+ return *res;
}
return std::nullopt;
}
@@ -52,11 +52,11 @@ namespace {
std::optional<std::filesystem::path> const& ws_root)
-> std::optional<std::pair<std::filesystem::path, std::filesystem::path>> {
auto res = ReadLocationObject(location, ws_root);
- if (res.index() == 0) {
- Logger::Log(LogLevel::Error, std::get<0>(res));
+ if (not res) {
+ Logger::Log(LogLevel::Error, res.error());
std::exit(kExitConfigError);
}
- return std::get<1>(res);
+ return *res;
}
[[nodiscard]] auto ReadOptionalLocationList(