From 67cab970091d5b23c07890deb29018c7eeb4edbc Mon Sep 17 00:00:00 2001 From: Oliver Reiche Date: Thu, 27 Jun 2024 17:07:41 +0200 Subject: Use (un)expected for reading location objects --- src/buildtool/common/TARGETS | 2 +- src/buildtool/common/location.cpp | 16 +++++++++------- src/buildtool/common/location.hpp | 9 ++++----- src/buildtool/main/serve.cpp | 6 +++--- src/other_tools/just_mr/rc.cpp | 12 ++++++------ 5 files changed, 23 insertions(+), 22 deletions(-) (limited to 'src') 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 const& ws_root) - -> std::variant> { + -> expected, 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(); auto path = location["path"].get(); @@ -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{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{ + 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 #include #include -#include #include "nlohmann/json.hpp" +#include "src/utils/cpp/expected.hpp" using location_res_t = std::pair; /// \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 const& ws_root) - -> std::variant>; + -> expected, 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> { 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> { 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 const& ws_root) -> std::optional> { 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( -- cgit v1.2.3