NAH 1.0.6
Native Application Host - Library API Reference
Loading...
Searching...
No Matches
warnings.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "nah/types.hpp"
5
6#include <string>
7#include <vector>
8#include <unordered_map>
9
10namespace nah {
11
12// ============================================================================
13// Warning Helpers
14// ============================================================================
15
16// Create warning fields map from vector of warning objects
17std::unordered_map<std::string, std::string> create_warning_fields(
18 const std::vector<WarningObject>& warnings);
19
20// ============================================================================
21// Warning Collector (per SPEC Warning System)
22// ============================================================================
23
25public:
26 // Default constructor with no policy
27 WarningCollector() = default;
28
29 // Constructor with policy map
30 explicit WarningCollector(const std::unordered_map<std::string, WarningAction>& policy)
31 : policy_(policy) {}
32
33 // Constructor with profile pointer (for backward compatibility)
34 explicit WarningCollector(const HostProfile* profile);
35
36 // Set/update the profile for warning policy
37 void set_profile(const HostProfile* profile);
38
39 // Emit a warning with fields
40 void emit(Warning warning, const std::unordered_map<std::string, std::string>& fields);
41
42 // Emit a warning with no fields
44
45 // Emit a warning with context string (convenience)
46 void emit_with_context(Warning warning, const std::string& context);
47
48 // Emit a warning by key string (for dynamic warning keys)
49 void emit(const std::string& warning_key, std::unordered_map<std::string, std::string> fields = {});
50
51 // Apply override to warning policy (per SPEC NAH_OVERRIDE_WARNINGS_*)
52 void apply_override(const std::string& warning_key, WarningAction action);
53
54 // Get all emitted warnings after policy application
55 // Warnings with action "ignore" are excluded
56 std::vector<WarningObject> get_warnings() const;
57
58 // Check if any warning was upgraded to error
59 bool has_errors() const;
60
61 // Check if any effective warnings remain (excluding ignored)
63
64 // Clear all collected warnings
65 void clear();
66
67private:
68 struct CollectedWarning {
69 std::string key;
70 std::unordered_map<std::string, std::string> fields;
71 WarningAction effective_action;
72 };
73
74 std::unordered_map<std::string, WarningAction> policy_;
75 std::vector<CollectedWarning> warnings_;
76 std::unordered_map<std::string, WarningAction> overrides_;
77
78 WarningAction get_effective_action(const std::string& key) const;
79};
80
81// ============================================================================
82// Convenience functions for emitting specific warnings
83// ============================================================================
84
85namespace warnings {
86
87// missing_env_var (per SPEC L1087)
88inline std::unordered_map<std::string, std::string> missing_env_var(
89 const std::string& var_name,
90 const std::string& source_path) {
91 return {{"missing", var_name}, {"source_path", source_path}};
92}
93
94// override_denied (per SPEC L1088)
95inline std::unordered_map<std::string, std::string> override_denied(
96 const std::string& target,
97 const std::string& source_kind,
98 const std::string& source_ref) {
99 return {{"target", target}, {"source_kind", source_kind}, {"source_ref", source_ref}};
100}
101
102// override_invalid (per SPEC L1089)
103inline std::unordered_map<std::string, std::string> override_invalid(
104 const std::string& target,
105 const std::string& reason,
106 const std::string& source_kind,
107 const std::string& source_ref) {
108 return {{"target", target}, {"reason", reason},
109 {"source_kind", source_kind}, {"source_ref", source_ref}};
110}
111
112// capability_missing (per SPEC L1090)
113inline std::unordered_map<std::string, std::string> capability_missing(
114 const std::string& capability) {
115 return {{"capability", capability}};
116}
117
118// capability_malformed (per SPEC L1091)
119inline std::unordered_map<std::string, std::string> capability_malformed(
120 const std::string& permission) {
121 return {{"permission", permission}};
122}
123
124// capability_unknown (per SPEC L1092)
125inline std::unordered_map<std::string, std::string> capability_unknown(
126 const std::string& operation) {
127 return {{"operation", operation}};
128}
129
130// invalid_library_path (per SPEC L1093)
131inline std::unordered_map<std::string, std::string> invalid_library_path(
132 const std::string& value,
133 const std::string& source_path) {
134 return {{"value", value}, {"source_path", source_path}};
135}
136
137// invalid_configuration (per SPEC L1086)
138inline std::unordered_map<std::string, std::string> invalid_configuration(
139 const std::string& reason,
140 const std::string& source_path,
141 const std::string& fields = "") {
142 std::unordered_map<std::string, std::string> result = {
143 {"reason", reason}, {"source_path", source_path}
144 };
145 if (!fields.empty()) {
146 result["fields"] = fields;
147 }
148 return result;
149}
150
151// profile_parse_error
152inline std::unordered_map<std::string, std::string> profile_parse_error(
153 const std::string& source_path) {
154 return {{"source_path", source_path}};
155}
156
157} // namespace warnings
158
159} // namespace nah
Result type for fallible operations.
Definition nahhost.hpp:109
void emit(Warning warning)
WarningCollector(const std::unordered_map< std::string, WarningAction > &policy)
Definition warnings.hpp:30
bool has_effective_warnings() const
void apply_override(const std::string &warning_key, WarningAction action)
void set_profile(const HostProfile *profile)
std::vector< WarningObject > get_warnings() const
WarningCollector(const HostProfile *profile)
bool has_errors() const
void emit(const std::string &warning_key, std::unordered_map< std::string, std::string > fields={})
void emit(Warning warning, const std::unordered_map< std::string, std::string > &fields)
WarningCollector()=default
void emit_with_context(Warning warning, const std::string &context)
WarningAction
Definition types.hpp:73
Warning
Definition types.hpp:14
std::unordered_map< std::string, std::string > create_warning_fields(const std::vector< WarningObject > &warnings)