summaryrefslogtreecommitdiff
path: root/src/buildtool/storage/backend_description.cpp
diff options
context:
space:
mode:
authorPaul Cristian Sarbu <paul.cristian.sarbu@huawei.com>2024-07-09 12:26:58 +0200
committerPaul Cristian Sarbu <paul.cristian.sarbu@huawei.com>2024-07-16 17:51:12 +0200
commitb095fb67afbd8fe72122cad22d3cbde8b2bf897c (patch)
tree4c217ac02b85dd805c4a5543e88c419632eac511 /src/buildtool/storage/backend_description.cpp
parent6c95b7c6e20467270da26cd5d10111bb9307c065 (diff)
downloadjustbuild-b095fb67afbd8fe72122cad22d3cbde8b2bf897c.tar.gz
Create separate library for DescribeBackend
Diffstat (limited to 'src/buildtool/storage/backend_description.cpp')
-rw-r--r--src/buildtool/storage/backend_description.cpp59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/buildtool/storage/backend_description.cpp b/src/buildtool/storage/backend_description.cpp
new file mode 100644
index 00000000..456f79c6
--- /dev/null
+++ b/src/buildtool/storage/backend_description.cpp
@@ -0,0 +1,59 @@
+// Copyright 2024 Huawei Cloud Computing Technology Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "src/buildtool/storage/backend_description.hpp"
+
+#include <exception>
+
+#include "fmt/core.h"
+#include "nlohmann/json.hpp"
+
+auto DescribeBackend(std::optional<ServerAddress> const& address,
+ ExecutionProperties const& properties,
+ std::vector<DispatchEndpoint> const& dispatch) noexcept
+ -> expected<std::string, std::string> {
+ auto description = nlohmann::json{
+ {"remote_address", address ? address->ToJson() : nlohmann::json{}},
+ {"platform_properties", properties}};
+ if (not dispatch.empty()) {
+ try {
+ // only add the dispatch list, if not empty, so that keys remain
+ // not only more readable, but also backwards compatible with
+ // earlier versions.
+ auto dispatch_list = nlohmann::json::array();
+ for (auto const& [props, endpoint] : dispatch) {
+ auto entry = nlohmann::json::array();
+ entry.push_back(nlohmann::json(props));
+ entry.push_back(endpoint.ToJson());
+ dispatch_list.push_back(entry);
+ }
+ description["endpoint dispatch list"] = dispatch_list;
+ } catch (std::exception const& e) {
+ return unexpected{fmt::format(
+ "Failed to serialize endpoint dispatch list:\n{}", e.what())};
+ }
+ }
+ try {
+ // json::dump with json::error_handler_t::replace will not throw an
+ // exception if invalid UTF-8 sequences are detected in the input.
+ // Instead, it will replace them with the UTF-8 replacement
+ // character, but still it needs to be inside a try-catch clause to
+ // ensure the noexcept modifier of the enclosing function.
+ return description.dump(
+ 2, ' ', false, nlohmann::json::error_handler_t::replace);
+ } catch (std::exception const& e) {
+ return unexpected{fmt::format(
+ "Failed to dump backend description to JSON:\n{}", e.what())};
+ }
+}