-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathEnum.cpp
More file actions
131 lines (119 loc) · 4.36 KB
/
Copy pathEnum.cpp
File metadata and controls
131 lines (119 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Copyright (C) 2021 CESNET, https://photonics.cesnet.cz/
*
* Written by Václav Kubernát <kubernat@cesnet.cz>
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <ostream>
#include <libyang-cpp/Enum.hpp>
#include <libyang/libyang.h>
#include <string>
namespace libyang {
LIBYANG_CPP_EXPORT std::ostream& operator<<(std::ostream& os, const NodeType& type)
{
switch (type) {
case NodeType::Action:
return os << "action";
case NodeType::AnyData:
return os << "anydata";
case NodeType::AnyXML:
return os << "anyxml";
case NodeType::Augment:
return os << "augment";
case NodeType::Case:
return os << "case";
case NodeType::Choice:
return os << "choice";
case NodeType::Container:
return os << "container";
case NodeType::Grouping:
return os << "grouping";
case NodeType::Input:
return os << "input";
case NodeType::Leaf:
return os << "leaf";
case NodeType::Leaflist:
return os << "leaflist";
case NodeType::List:
return os << "list";
case NodeType::Notification:
return os << "notification";
case NodeType::Output:
return os << "output";
case NodeType::RPC:
return os << "rpc";
case NodeType::Uses:
return os << "uses";
case NodeType::Unknown:
break;
}
return os << "[unknown node type]";
}
#define CHECK_AND_STRINGIFY(CPP_ENUM, C_ENUM) \
static_assert(static_cast<std::underlying_type_t<decltype(CPP_ENUM)>>(CPP_ENUM) == (C_ENUM)); \
case CPP_ENUM: \
return #C_ENUM
std::string stringify(const ErrorCode err)
{
switch (err) {
CHECK_AND_STRINGIFY(ErrorCode::Success, LY_SUCCESS);
CHECK_AND_STRINGIFY(ErrorCode::MemoryFailure, LY_EMEM);
CHECK_AND_STRINGIFY(ErrorCode::SyscallFail, LY_ESYS);
CHECK_AND_STRINGIFY(ErrorCode::InvalidValue, LY_EINVAL);
CHECK_AND_STRINGIFY(ErrorCode::ItemAlreadyExists, LY_EEXIST);
CHECK_AND_STRINGIFY(ErrorCode::NotFound, LY_ENOTFOUND);
CHECK_AND_STRINGIFY(ErrorCode::InternalError, LY_EINT);
CHECK_AND_STRINGIFY(ErrorCode::ValidationFailure, LY_EVALID);
CHECK_AND_STRINGIFY(ErrorCode::OperationDenied, LY_EDENIED);
CHECK_AND_STRINGIFY(ErrorCode::OperationIncomplete, LY_EINCOMPLETE);
CHECK_AND_STRINGIFY(ErrorCode::RecompileRequired, LY_ERECOMPILE);
CHECK_AND_STRINGIFY(ErrorCode::Negative, LY_ENOT);
CHECK_AND_STRINGIFY(ErrorCode::Unknown, LY_EOTHER);
CHECK_AND_STRINGIFY(ErrorCode::PluginError, LY_EPLUGIN);
}
return "[unknown error code (" + std::to_string(static_cast<std::underlying_type_t<decltype(err)>>(err)) + ")]";
}
std::string stringify(const ValidationErrorCode err)
{
switch (err) {
CHECK_AND_STRINGIFY(ValidationErrorCode::Success, LYVE_SUCCESS);
CHECK_AND_STRINGIFY(ValidationErrorCode::Syntax, LYVE_SYNTAX);
CHECK_AND_STRINGIFY(ValidationErrorCode::YangSyntax, LYVE_SYNTAX_YANG);
CHECK_AND_STRINGIFY(ValidationErrorCode::YinSyntax, LYVE_SYNTAX_YIN);
CHECK_AND_STRINGIFY(ValidationErrorCode::Reference, LYVE_REFERENCE);
CHECK_AND_STRINGIFY(ValidationErrorCode::Xpath, LYVE_XPATH);
CHECK_AND_STRINGIFY(ValidationErrorCode::Semantics, LYVE_SEMANTICS);
CHECK_AND_STRINGIFY(ValidationErrorCode::XmlSyntax, LYVE_SYNTAX_XML);
CHECK_AND_STRINGIFY(ValidationErrorCode::JsonSyntax, LYVE_SYNTAX_JSON);
CHECK_AND_STRINGIFY(ValidationErrorCode::Data, LYVE_DATA);
CHECK_AND_STRINGIFY(ValidationErrorCode::Other, LYVE_OTHER);
}
return "[unknown validation error code (" + std::to_string(static_cast<std::underlying_type_t<decltype(err)>>(err)) + ")]";
}
std::string stringify(const LogLevel level)
{
switch (level) {
CHECK_AND_STRINGIFY(LogLevel::Error, LY_LLERR);
CHECK_AND_STRINGIFY(LogLevel::Warning, LY_LLWRN);
CHECK_AND_STRINGIFY(LogLevel::Verbose, LY_LLVRB);
CHECK_AND_STRINGIFY(LogLevel::Debug, LY_LLDBG);
}
return "[unknown log level (" + std::to_string(static_cast<std::underlying_type_t<decltype(level)>>(level)) + ")]";
}
LIBYANG_CPP_EXPORT std::ostream& operator<<(std::ostream& os, const ErrorCode& err)
{
os << stringify(err);
return os;
}
LIBYANG_CPP_EXPORT std::ostream& operator<<(std::ostream& os, const ValidationErrorCode& err)
{
os << stringify(err);
return os;
}
LIBYANG_CPP_EXPORT std::ostream& operator<<(std::ostream& os, const LogLevel& level)
{
os << stringify(level);
return os;
}
}