|
| 1 | +// SPDX-FileCopyrightText: 2026 Vector Informatik GmbH |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +#include "YamlParserUtils.hpp" |
| 6 | + |
| 7 | +#include "silkit/participant/exception.hpp" |
| 8 | + |
| 9 | +#include <sstream> |
| 10 | + |
| 11 | + |
| 12 | +namespace { |
| 13 | + |
| 14 | +auto RapidyamlAllocate(size_t length, void* hint, void* userData) -> void*; |
| 15 | +void RapidyamlFree(void* ptr, size_t length, void* userData); |
| 16 | +void RapidyamlError(const char* message, size_t length, ryml::Location location, void* userData); |
| 17 | + |
| 18 | +} // namespace |
| 19 | + |
| 20 | + |
| 21 | +namespace VSilKit { |
| 22 | + |
| 23 | +auto ParseCapabilities(const std::string& input) -> std::vector<std::map<std::string, std::string>> |
| 24 | +{ |
| 25 | + std::vector<std::map<std::string, std::string>> result; |
| 26 | + auto&& cinput = ryml::to_csubstr(input); |
| 27 | + auto t = ryml::parse_in_arena(cinput); |
| 28 | + |
| 29 | + auto root = t.crootref(); |
| 30 | + if (!root.is_seq()) |
| 31 | + { |
| 32 | + throw SilKit::ConfigurationError{"First element in Capabilities string is not a sequence"}; |
| 33 | + } |
| 34 | + if (root.has_children()) |
| 35 | + { |
| 36 | + for (auto&& child : root.children()) |
| 37 | + { |
| 38 | + if (!child.is_map()) |
| 39 | + { |
| 40 | + throw SilKit::ConfigurationError{"Capabilities should be a sequence of map objects."}; |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + root >> result; |
| 45 | + return result; |
| 46 | +} |
| 47 | + |
| 48 | +auto MakeConfigurationError(ryml::Location location, const std::string_view message) -> SilKit::ConfigurationError |
| 49 | +{ |
| 50 | + std::ostringstream s; |
| 51 | + |
| 52 | + s << "error parsing configuration"; |
| 53 | + if (location.name.empty()) |
| 54 | + { |
| 55 | + s << " string: "; |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + s << " file " << location.name << ": "; |
| 60 | + } |
| 61 | + |
| 62 | + s << "line " << (location.line + 1) << " column " << location.col << ": " << message; |
| 63 | + |
| 64 | + return SilKit::ConfigurationError{s.str()}; |
| 65 | +} |
| 66 | + |
| 67 | +auto GetRapidyamlCallbacks() -> ryml::Callbacks |
| 68 | +{ |
| 69 | + return ryml::Callbacks{nullptr, RapidyamlAllocate, RapidyamlFree, RapidyamlError}; |
| 70 | +} |
| 71 | + |
| 72 | +} // namespace VSilKit |
| 73 | + |
| 74 | + |
| 75 | +namespace { |
| 76 | + |
| 77 | +auto RapidyamlAllocate(const size_t length, void* /*hint*/, void* /*userData*/) -> void* |
| 78 | +{ |
| 79 | + return std::malloc(length); |
| 80 | +} |
| 81 | + |
| 82 | +void RapidyamlFree(void* ptr, size_t /*length*/, void* /*userData*/) |
| 83 | +{ |
| 84 | + std::free(ptr); |
| 85 | +} |
| 86 | + |
| 87 | +void RapidyamlError(const char* message, const size_t length, ryml::Location location, void* /*userData*/) |
| 88 | +{ |
| 89 | + const std::string_view rapidyamlMessage{message, length}; |
| 90 | + throw VSilKit::MakeConfigurationError(location, message); |
| 91 | +} |
| 92 | + |
| 93 | +} // namespace |
0 commit comments