summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/other_tools/repo_map/repos_to_setup_map.cpp104
-rw-r--r--src/other_tools/utils/TARGETS1
-rw-r--r--src/other_tools/utils/parse_archive.cpp58
-rw-r--r--src/other_tools/utils/parse_archive.hpp5
4 files changed, 42 insertions, 126 deletions
diff --git a/src/other_tools/repo_map/repos_to_setup_map.cpp b/src/other_tools/repo_map/repos_to_setup_map.cpp
index 48074fd4..29a0715c 100644
--- a/src/other_tools/repo_map/repos_to_setup_map.cpp
+++ b/src/other_tools/repo_map/repos_to_setup_map.cpp
@@ -495,106 +495,28 @@ void DistdirCheckout(ExpressionPtr const& repo_desc,
}
// only do work if repo is archive type
if (kCheckoutTypeMap.at(repo_type_str) == CheckoutType::Archive) {
- // check mandatory fields
- auto repo_desc_content = (*resolved_repo_desc)->At("content");
- if (not repo_desc_content) {
- (*logger)(fmt::format(
- "DistdirCheckout: Mandatory field \"content\" is "
- "missing for repository {}",
- nlohmann::json(dist_repo_name).dump()),
- /*fatal=*/true);
- return;
- }
- if (not repo_desc_content->get()->IsString()) {
- (*logger)(fmt::format("DistdirCheckout: Unsupported value {} "
- "for mandatory field \"content\" for "
- "repository {}",
- repo_desc_content->get()->ToString(),
- nlohmann::json(dist_repo_name).dump()),
- /*fatal=*/true);
- return;
- }
- auto repo_desc_fetch = (*resolved_repo_desc)->At("fetch");
- if (not repo_desc_fetch) {
- (*logger)(fmt::format("DistdirCheckout: Mandatory field "
- "\"fetch\" is missing for repository {}",
- nlohmann::json(dist_repo_name).dump()),
- /*fatal=*/true);
- return;
- }
- if (not repo_desc_fetch->get()->IsString()) {
- (*logger)(fmt::format(
- "DistdirCheckout: Unsupported value {} "
- "for mandatory field \"fetch\" for repository {}",
- repo_desc_fetch->get()->ToString(),
- nlohmann::json(dist_repo_name).dump()),
- /*fatal=*/true);
- return;
- }
- auto repo_desc_distfile =
- (*resolved_repo_desc)->Get("distfile", Expression::none_t{});
- auto repo_desc_sha256 =
- (*resolved_repo_desc)->Get("sha256", Expression::none_t{});
- auto repo_desc_sha512 =
- (*resolved_repo_desc)->Get("sha512", Expression::none_t{});
-
- // check optional mirrors
- auto repo_desc_mirrors =
- (*resolved_repo_desc)->Get("mirrors", Expression::list_t{});
- std::vector<std::string> mirrors{};
- if (repo_desc_mirrors->IsList()) {
- mirrors.reserve(repo_desc_mirrors->List().size());
- for (auto const& elem : repo_desc_mirrors->List()) {
- if (not elem->IsString()) {
- (*logger)(fmt::format(
- "DistdirCheckout: Unsupported list entry "
- "{} in optional field \"mirrors\" for "
- "repository {}",
- elem->ToString(),
- nlohmann::json(dist_repo_name).dump()),
- /*fatal=*/true);
- return;
- }
- mirrors.emplace_back(elem->String());
- }
- }
- else {
- (*logger)(fmt::format("DistdirCheckout: Optional field "
- "\"mirrors\" for repository {} should be "
- "a list of strings, but found: {}",
+ auto const archive =
+ ParseArchiveContent(*resolved_repo_desc, dist_repo_name);
+ if (not archive) {
+ (*logger)(fmt::format("DistdirCheckout: an error occurred "
+ "while parsing repository {}\n{}",
nlohmann::json(dist_repo_name).dump(),
- repo_desc_mirrors->ToString()),
+ archive.error()),
/*fatal=*/true);
return;
}
- ArchiveContent archive = {
- .content = repo_desc_content->get()->String(),
- .distfile =
- repo_desc_distfile->IsString()
- ? std::make_optional(repo_desc_distfile->String())
- : std::nullopt,
- .fetch_url = repo_desc_fetch->get()->String(),
- .mirrors = std::move(mirrors),
- .sha256 = repo_desc_sha256->IsString()
- ? std::make_optional(repo_desc_sha256->String())
- : std::nullopt,
- .sha512 = repo_desc_sha512->IsString()
- ? std::make_optional(repo_desc_sha512->String())
- : std::nullopt,
- .origin = dist_repo_name};
-
// add to distdir content map
auto repo_distfile =
- (archive.distfile ? archive.distfile.value()
- : std::filesystem::path(archive.fetch_url)
- .filename()
- .string());
+ (archive->distfile ? archive->distfile.value()
+ : std::filesystem::path(archive->fetch_url)
+ .filename()
+ .string());
distdir_content_for_id->insert_or_assign(
- repo_distfile, std::make_pair(archive.content, false));
- distdir_content->insert_or_assign(repo_distfile, archive.content);
+ repo_distfile, std::make_pair(archive->content, false));
+ distdir_content->insert_or_assign(repo_distfile, archive->content);
// add to fetch list
- dist_repos_to_fetch->emplace_back(std::move(archive));
+ dist_repos_to_fetch->emplace_back(*std::move(archive));
}
}
// get hash of distdir content
diff --git a/src/other_tools/utils/TARGETS b/src/other_tools/utils/TARGETS
index 27cbb93d..9d4e5734 100644
--- a/src/other_tools/utils/TARGETS
+++ b/src/other_tools/utils/TARGETS
@@ -60,6 +60,7 @@
, "deps":
[ ["src/buildtool/build_engine/expression", "expression"]
, ["src/other_tools/ops_maps", "content_cas_map"]
+ , ["src/utils/cpp", "expected"]
]
, "private-deps": [["@", "fmt", "", "fmt"]]
, "stage": ["src", "other_tools", "utils"]
diff --git a/src/other_tools/utils/parse_archive.cpp b/src/other_tools/utils/parse_archive.cpp
index 1aebe239..a8fc1830 100644
--- a/src/other_tools/utils/parse_archive.cpp
+++ b/src/other_tools/utils/parse_archive.cpp
@@ -18,38 +18,28 @@
#include "fmt/core.h"
-namespace {
auto ParseArchiveContent(ExpressionPtr const& repo_desc,
- std::string const& origin,
- const AsyncMapConsumerLoggerPtr& logger)
- -> std::optional<ArchiveContent> {
-
+ std::string const& origin)
+ -> expected<ArchiveContent, std::string> {
// enforce mandatory fields
auto repo_desc_content = repo_desc->At("content");
if (not repo_desc_content) {
- (*logger)("ArchiveCheckout: Mandatory field \"content\" is missing",
- /*fatal=*/true);
- return std::nullopt;
+ return unexpected<std::string>{
+ "Mandatory field \"content\" is missing"};
}
if (not repo_desc_content->get()->IsString()) {
- (*logger)(fmt::format("ArchiveCheckout: Unsupported value {} for "
- "mandatory field \"content\"",
- repo_desc_content->get()->ToString()),
- /*fatal=*/true);
- return std::nullopt;
+ return unexpected{
+ fmt::format("Unsupported value {} for mandatory field \"content\"",
+ repo_desc_content->get()->ToString())};
}
auto repo_desc_fetch = repo_desc->At("fetch");
if (not repo_desc_fetch) {
- (*logger)("ArchiveCheckout: Mandatory field \"fetch\" is missing",
- /*fatal=*/true);
- return std::nullopt;
+ return unexpected<std::string>{"Mandatory field \"fetch\" is missing"};
}
if (not repo_desc_fetch->get()->IsString()) {
- (*logger)(fmt::format("ArchiveCheckout: Unsupported value {} for "
- "mandatory field \"fetch\"",
- repo_desc_fetch->get()->ToString()),
- /*fatal=*/true);
- return std::nullopt;
+ return unexpected{
+ fmt::format("Unsupported value {} for mandatory field \"fetch\"",
+ repo_desc_fetch->get()->ToString())};
}
auto repo_desc_distfile = repo_desc->Get("distfile", Expression::none_t{});
auto repo_desc_sha256 = repo_desc->Get("sha256", Expression::none_t{});
@@ -61,21 +51,18 @@ auto ParseArchiveContent(ExpressionPtr const& repo_desc,
mirrors.reserve(repo_desc_mirrors->List().size());
for (auto const& elem : repo_desc_mirrors->List()) {
if (not elem->IsString()) {
- (*logger)(fmt::format("ArchiveCheckout: Unsupported list entry "
- "{} in optional field \"mirrors\"",
- elem->ToString()),
- /*fatal=*/true);
- return std::nullopt;
+ return unexpected{fmt::format(
+ "Unsupported list entry {} in optional field \"mirrors\"",
+ elem->ToString())};
}
mirrors.emplace_back(elem->String());
}
}
else {
- (*logger)(fmt::format("ArchiveCheckout: Optional field \"mirrors\" "
- "should be a list of strings, but found: {}",
- repo_desc_mirrors->ToString()),
- /*fatal=*/true);
- return std::nullopt;
+ return unexpected{
+ fmt::format("Optional field \"mirrors\" should be a list of "
+ "strings, but found: {}",
+ repo_desc_mirrors->ToString())};
}
return ArchiveContent{
@@ -104,15 +91,15 @@ auto IsValidFileName(const std::string& s) -> bool {
return true;
}
-} // namespace
-
auto ParseArchiveDescription(ExpressionPtr const& repo_desc,
std::string const& repo_type,
std::string const& origin,
const AsyncMapConsumerLoggerPtr& logger)
-> std::optional<ArchiveRepoInfo> {
- auto archive_content = ParseArchiveContent(repo_desc, origin, logger);
+ auto const archive_content = ParseArchiveContent(repo_desc, origin);
if (not archive_content) {
+ (*logger)(fmt::format("ArchiveCheckout: {}", archive_content.error()),
+ /*fatal=*/true);
return std::nullopt;
}
// additional mandatory fields
@@ -151,8 +138,9 @@ auto ParseForeignFileDescription(ExpressionPtr const& repo_desc,
std::string const& origin,
const AsyncMapConsumerLoggerPtr& logger)
-> std::optional<ForeignFileInfo> {
- auto archive_content = ParseArchiveContent(repo_desc, origin, logger);
+ auto const archive_content = ParseArchiveContent(repo_desc, origin);
if (not archive_content) {
+ (*logger)(archive_content.error(), /*fatal=*/true);
return std::nullopt;
}
auto name = repo_desc->At("name");
diff --git a/src/other_tools/utils/parse_archive.hpp b/src/other_tools/utils/parse_archive.hpp
index 6d356dd4..bf7f02fc 100644
--- a/src/other_tools/utils/parse_archive.hpp
+++ b/src/other_tools/utils/parse_archive.hpp
@@ -20,6 +20,11 @@
#include "src/buildtool/build_engine/expression/expression.hpp"
#include "src/other_tools/ops_maps/content_cas_map.hpp"
+#include "src/utils/cpp/expected.hpp"
+
+auto ParseArchiveContent(ExpressionPtr const& repo_desc,
+ std::string const& origin)
+ -> expected<ArchiveContent, std::string>;
// Parse the description of an archive repository; if an error
// occurs, call the logger with fatal set to true and return std::nullopt