blob: 4d52122eb8170933d5f1efe9b8d0775534ecfff4 [file] [log] [blame]
#include <fmt/format.h>
#include <libyang-cpp/DataNode.hpp>
#include <libyang-cpp/Set.hpp>
#include "utils/libyang.h"
namespace velia::utils {
const char* getValueAsString(const libyang::DataNode& node)
{
if (node.schema().nodeType() != libyang::NodeType::Leaf) {
throw std::logic_error("retrieveString: invalid node");
}
return node.asTerm().valueStr().data();
}
std::optional<libyang::DataNode> getUniqueSubtree(const libyang::DataNode& start, const std::string& path)
{
auto set = start.findXPath(path);
switch(set.size()) {
case 0:
return std::nullopt;
case 1:
return set.front();
default:
throw std::runtime_error(fmt::format("getUniqueSubtree({}, {}): more than one match (got {})", start.path(), path, set.size()));
}
}
}