NAH 1.0.6
Native Application Host - Library API Reference
Loading...
Searching...
No Matches
nahhost.hpp
Go to the documentation of this file.
1#pragma once
2
24#include "nah/types.hpp"
25#include "nah/contract.hpp"
26#include "nah/host_profile.hpp"
27
28#include <memory>
29#include <optional>
30#include <string>
31#include <vector>
32
33namespace nah {
34
35// ============================================================================
36// App Info
37// ============================================================================
38
42struct AppInfo {
43 std::string id;
44 std::string version;
45 std::string instance_id;
46 std::string install_root;
47 std::string record_path;
48};
49
50// ============================================================================
51// Error Handling (per SPEC L1692-L1738)
52// ============================================================================
53
57enum class ErrorCode {
58 // System / IO
62
63 // Contract composition critical errors (normative)
68
69 // Profile load failures
72};
73
77class Error {
78public:
80 : code_(code), message_(std::move(message)) {}
81
82 Error& withContext(const std::string& context) {
83 message_ = context + ": " + message_;
84 return *this;
85 }
86
87 ErrorCode code() const { return code_; }
88 const std::string& message() const { return message_; }
89 std::string toString() const { return message_; }
90
91private:
92 ErrorCode code_;
93 std::string message_;
94};
95
96// ============================================================================
97// Result Type (per SPEC L1692-L1710)
98// ============================================================================
99
108template<typename T, typename E = Error>
109class Result {
110public:
111 static Result ok(T value) { return Result(std::move(value)); }
112 static Result err(E error) { return Result(std::move(error)); }
113
114 bool isOk() const { return has_value_; }
115 bool isErr() const { return !has_value_; }
116
117 T& value() { return value_.value(); }
118 const T& value() const { return value_.value(); }
119 E& error() { return error_.value(); }
120 const E& error() const { return error_.value(); }
121
123 if (has_value_) return value_.value();
124 return default_value;
125 }
126
127 template<typename F>
129 if (has_value_) {
130 return Result<decltype(func(std::declval<T>())), E>::ok(func(value_.value()));
131 }
132 return Result<decltype(func(std::declval<T>())), E>::err(error_.value());
133 }
134
135 template<typename F>
136 auto flatMap(F func) -> decltype(func(std::declval<T>())) {
137 if (has_value_) {
138 return func(value_.value());
139 }
140 return decltype(func(std::declval<T>()))::err(error_.value());
141 }
142
143private:
144 explicit Result(T value) : has_value_(true), value_(std::move(value)) {}
145 explicit Result(E error) : has_value_(false), error_(std::move(error)) {}
146
147 bool has_value_;
148 std::optional<T> value_;
149 std::optional<E> error_;
150};
151
152template<typename E>
153class Result<void, E> {
154public:
155 static Result ok() { return Result(true, std::nullopt); }
156 static Result err(E error) { return Result(false, std::move(error)); }
157
158 bool isOk() const { return has_value_; }
159 bool isErr() const { return !has_value_; }
160
161 void value() const {}
162 E& error() { return error_.value(); }
163 const E& error() const { return error_.value(); }
164
165private:
166 Result(bool hv, std::optional<E> err) : has_value_(hv), error_(std::move(err)) {}
167 bool has_value_;
168 std::optional<E> error_;
169};
170
171// ============================================================================
172// NahHost Class (per SPEC L1660-L1684)
173// ============================================================================
174
200class NahHost {
201public:
207 static std::unique_ptr<NahHost> create(const std::string& root_path);
208
210 const std::string& root() const { return root_; }
211
213 std::vector<AppInfo> listApplications() const;
214
221 Result<AppInfo> findApplication(const std::string& id,
222 const std::string& version = "") const;
223
226
229
231 std::vector<std::string> listProfiles() const;
232
234 Result<HostProfile> loadProfile(const std::string& name) const;
235
238
248 const std::string& app_id,
249 const std::string& version = "",
250 const std::string& profile = "",
251 bool enable_trace = false) const;
252
259 const CompositionInputs& inputs) const;
260
261private:
262 explicit NahHost(std::string root) : root_(std::move(root)) {}
263
264 Result<HostProfile> resolveActiveProfile(const std::string& explicit_name = "") const;
265
266 std::string root_;
267};
268
269} // namespace nah
Error type with code and message.
Definition nahhost.hpp:77
std::string toString() const
Definition nahhost.hpp:89
Error & withContext(const std::string &context)
Definition nahhost.hpp:82
Error(ErrorCode code, std::string message)
Definition nahhost.hpp:79
const std::string & message() const
Definition nahhost.hpp:88
ErrorCode code() const
Definition nahhost.hpp:87
Result< HostProfile > getActiveHostProfile() const
Get the currently active host profile.
Result< AppInfo > findApplication(const std::string &id, const std::string &version="") const
Find an installed application by ID.
Result< ContractEnvelope > composeContract(const CompositionInputs &inputs) const
Low-level contract composition from explicit inputs.
static std::unique_ptr< NahHost > create(const std::string &root_path)
Create a NahHost instance for a NAH root directory.
Result< void > validateProfile(const HostProfile &profile) const
Validate a host profile.
Result< void > setActiveHostProfile(const std::string &name)
Set the active host profile by name.
std::vector< std::string > listProfiles() const
List all available profile names.
Result< HostProfile > loadProfile(const std::string &name) const
Load a specific profile by name.
const std::string & root() const
Get the NAH root path.
Definition nahhost.hpp:210
Result< ContractEnvelope > getLaunchContract(const std::string &app_id, const std::string &version="", const std::string &profile="", bool enable_trace=false) const
Generate a launch contract for an application.
std::vector< AppInfo > listApplications() const
List all installed applications.
static Result err(E error)
Definition nahhost.hpp:156
static Result ok()
Definition nahhost.hpp:155
const E & error() const
Definition nahhost.hpp:163
Result type for fallible operations.
Definition nahhost.hpp:109
const E & error() const
Definition nahhost.hpp:120
E & error()
Definition nahhost.hpp:119
auto map(F func) -> Result< decltype(func(std::declval< T >())), E >
Definition nahhost.hpp:128
const T & value() const
Definition nahhost.hpp:118
T & value()
Definition nahhost.hpp:117
T valueOr(T default_value) const
Definition nahhost.hpp:122
bool isErr() const
Definition nahhost.hpp:115
static Result ok(T value)
Definition nahhost.hpp:111
static Result err(E error)
Definition nahhost.hpp:112
bool isOk() const
Definition nahhost.hpp:114
auto flatMap(F func) -> decltype(func(std::declval< T >()))
Definition nahhost.hpp:136
ErrorCode
Error codes for NAH operations.
Definition nahhost.hpp:57
Metadata about an installed application.
Definition nahhost.hpp:42
std::string version
Definition nahhost.hpp:44
std::string install_root
Definition nahhost.hpp:46
std::string id
Definition nahhost.hpp:43
std::string instance_id
Definition nahhost.hpp:45
std::string record_path
Definition nahhost.hpp:47