From dfb361e44d01242eeefa7b554405d37402626766 Mon Sep 17 00:00:00 2001 From: Oliver Reiche Date: Thu, 27 Jun 2024 17:45:29 +0200 Subject: Use (un)expected for network fetch --- src/other_tools/ops_maps/content_cas_map.cpp | 8 +++----- src/other_tools/utils/TARGETS | 1 + src/other_tools/utils/content.hpp | 15 +++++++-------- 3 files changed, 11 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/other_tools/ops_maps/content_cas_map.cpp b/src/other_tools/ops_maps/content_cas_map.cpp index 651e83fb..013f49c6 100644 --- a/src/other_tools/ops_maps/content_cas_map.cpp +++ b/src/other_tools/ops_maps/content_cas_map.cpp @@ -41,15 +41,13 @@ void FetchFromNetwork(ArchiveContent const& key, return; } // now do the actual fetch - auto res = NetworkFetchWithMirrors( + auto data = NetworkFetchWithMirrors( key.fetch_url, key.mirrors, ca_info, additional_mirrors); - auto* data = - std::get_if<1>(&res); // get pointer to fetched data, or nullptr - if (data == nullptr) { + if (not data) { (*logger)(fmt::format("Failed to fetch a file with id {} from provided " "remotes:{}", key.content, - std::get<0>(res)), + data.error()), /*fatal=*/true); return; } diff --git a/src/other_tools/utils/TARGETS b/src/other_tools/utils/TARGETS index e210f901..27cbb93d 100644 --- a/src/other_tools/utils/TARGETS +++ b/src/other_tools/utils/TARGETS @@ -48,6 +48,7 @@ , ["src/buildtool/crypto", "hasher"] , ["src/buildtool/logging", "log_level"] , ["src/other_tools/just_mr", "mirrors"] + , ["src/utils/cpp", "expected"] ] , "stage": ["src", "other_tools", "utils"] } diff --git a/src/other_tools/utils/content.hpp b/src/other_tools/utils/content.hpp index 15c582c4..31dd7fc1 100644 --- a/src/other_tools/utils/content.hpp +++ b/src/other_tools/utils/content.hpp @@ -18,7 +18,6 @@ #include #include #include // std::move -#include #include "src/buildtool/common/user_structs.hpp" #include "src/buildtool/crypto/hasher.hpp" @@ -26,6 +25,7 @@ #include "src/other_tools/just_mr/mirrors.hpp" #include "src/other_tools/utils/curl_easy_handle.hpp" #include "src/other_tools/utils/curl_url_handle.hpp" +#include "src/utils/cpp/expected.hpp" // Utilities related to the content of an archive @@ -44,14 +44,13 @@ /// \brief Fetches a file from the internet and stores its content in memory. /// Tries not only a given remote, but also all associated remote locations. -/// \returns An error + data union, with the error message at index 0 and the -/// fetched data at index 1. +/// \returns The fetched data on success or an unexpected error as string. [[nodiscard]] static auto NetworkFetchWithMirrors( std::string const& fetch_url, std::vector const& mirrors, CAInfoPtr const& ca_info, MirrorsPtr const& additional_mirrors) noexcept - -> std::variant { + -> expected { // keep all remotes tried, to report in case fetch fails std::string remotes_buffer{}; std::optional data{std::nullopt}; @@ -81,10 +80,10 @@ // add local mirror to buffer remotes_buffer.append(fmt::format("\n> {}", mirror)); } - return data ? std::variant(std::in_place_index<1>, - *data) - : std::variant(std::in_place_index<0>, - remotes_buffer); + if (not data) { + return unexpected{remotes_buffer}; + } + return *data; } template -- cgit v1.2.3