diff options
author | Oliver Reiche <oliver.reiche@huawei.com> | 2024-06-27 17:45:29 +0200 |
---|---|---|
committer | Oliver Reiche <oliver.reiche@huawei.com> | 2024-06-28 10:45:56 +0200 |
commit | dfb361e44d01242eeefa7b554405d37402626766 (patch) | |
tree | 007a54ef3ee1c698d7c25c44de01fcf52831e075 /src | |
parent | 6f331bfa27faa92f848163827ee799b3e2de73c1 (diff) | |
download | justbuild-dfb361e44d01242eeefa7b554405d37402626766.tar.gz |
Use (un)expected for network fetch
Diffstat (limited to 'src')
-rw-r--r-- | src/other_tools/ops_maps/content_cas_map.cpp | 8 | ||||
-rw-r--r-- | src/other_tools/utils/TARGETS | 1 | ||||
-rw-r--r-- | src/other_tools/utils/content.hpp | 15 |
3 files changed, 11 insertions, 13 deletions
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 <optional> #include <string> #include <utility> // std::move -#include <variant> #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<std::string> const& mirrors, CAInfoPtr const& ca_info, MirrorsPtr const& additional_mirrors) noexcept - -> std::variant<std::string, std::string> { + -> expected<std::string, std::string> { // keep all remotes tried, to report in case fetch fails std::string remotes_buffer{}; std::optional<std::string> data{std::nullopt}; @@ -81,10 +80,10 @@ // add local mirror to buffer remotes_buffer.append(fmt::format("\n> {}", mirror)); } - return data ? std::variant<std::string, std::string>(std::in_place_index<1>, - *data) - : std::variant<std::string, std::string>(std::in_place_index<0>, - remotes_buffer); + if (not data) { + return unexpected{remotes_buffer}; + } + return *data; } template <Hasher::HashType type> |