blob: a4b7479e68fdb93e6f15a66787eb3afd22e546dc [file] [log] [blame]
Václav Kubernátbabbab92021-01-27 09:25:05 +01001#include <fmt/format.h>
Václav Kubernátcbdc98a2021-01-28 13:24:37 +01002#include <libyang/Tree_Data.hpp>
3#include "utils/libyang.h"
4
Tomáš Peckafd5ebd32021-10-07 09:48:13 +02005namespace velia::utils {
6
Václav Kubernátcbdc98a2021-01-28 13:24:37 +01007const char* getValueAsString(const libyang::S_Data_Node& node)
8{
9 if (!node || node->schema()->nodetype() != LYS_LEAF) {
10 throw std::logic_error("retrieveString: invalid node");
11 }
12
13 return libyang::Data_Node_Leaf_List(node).value_str();
14}
15
Tomáš Peckafd90efb2021-10-07 10:40:44 +020016std::optional<libyang::S_Data_Node> getUniqueSubtree(const libyang::S_Data_Node& start, const char* path)
Václav Kubernátcbdc98a2021-01-28 13:24:37 +010017{
18 auto set = start->find_path(path);
Václav Kubernátcbdc98a2021-01-28 13:24:37 +010019
Tomáš Peckafd90efb2021-10-07 10:40:44 +020020 switch(set->number()) {
21 case 0:
22 return std::nullopt;
23 case 1:
24 return set->data().front();
25 default:
26 throw std::runtime_error(fmt::format("getUniqueSubtree({}, {}): more than one match (got {})", start->path(), path, set->number()));
27 }
Václav Kubernátcbdc98a2021-01-28 13:24:37 +010028}
Tomáš Peckafd5ebd32021-10-07 09:48:13 +020029}