-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.hpp
More file actions
193 lines (152 loc) · 5.38 KB
/
Copy patherror.hpp
File metadata and controls
193 lines (152 loc) · 5.38 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#pragma once
#include <string>
#include <string_view>
#include <system_error>
#include <variant>
// C++23 std::expected or fallback
#ifdef HAS_CPP23_EXPECTED
#include <expected>
namespace sqlite_vec_cpp {
template <typename T, typename E> using Expected = std::expected<T, E>;
template <typename E> using Unexpected = std::unexpected<E>;
} // namespace sqlite_vec_cpp
#else
// Fallback: simplified expected implementation for now
// TODO: Use tl::expected or implement full expected<T,E>
namespace sqlite_vec_cpp {
template <typename E> class Unexpected {
public:
explicit Unexpected(E error) : error_(std::move(error)) {}
const E& error() const& { return error_; }
E& error() & { return error_; }
E&& error() && { return std::move(error_); }
private:
E error_;
};
template <typename T, typename E> class Expected {
public:
explicit Expected(T value) : storage_(std::in_place_index<0>, std::move(value)) {}
explicit Expected(Unexpected<E> error)
: storage_(std::in_place_index<1>, std::move(error.error())) {}
explicit Expected(E error) : storage_(std::in_place_index<1>, std::move(error)) {}
~Expected() = default;
Expected(const Expected&) = delete;
Expected& operator=(const Expected&) = delete;
Expected(Expected&&) noexcept = default;
Expected& operator=(Expected&&) noexcept = default;
bool has_value() const noexcept { return storage_.index() == 0; }
explicit operator bool() const noexcept { return has_value(); }
T& value() & {
if (!has_value())
throw std::runtime_error("Expected: no value");
return std::get<0>(storage_);
}
const T& value() const& {
if (!has_value())
throw std::runtime_error("Expected: no value");
return std::get<0>(storage_);
}
T&& value() && {
if (!has_value())
throw std::runtime_error("Expected: no value");
return std::move(std::get<0>(storage_));
}
E& error() & {
if (has_value())
throw std::runtime_error("Expected: has value");
return std::get<1>(storage_);
}
const E& error() const& {
if (has_value())
throw std::runtime_error("Expected: has value");
return std::get<1>(storage_);
}
T& operator*() & { return value(); }
const T& operator*() const& { return value(); }
T&& operator*() && { return std::move(value()); }
T* operator->() { return &value(); }
const T* operator->() const { return &value(); }
private:
std::variant<T, E> storage_;
};
// Specialization for void
template <typename E> class Expected<void, E> {
public:
Expected() : has_value_(true) {}
explicit Expected(Unexpected<E> error) : has_value_(false), error_(std::move(error.error())) {}
explicit Expected(E error) : has_value_(false), error_(std::move(error)) {}
bool has_value() const noexcept { return has_value_; }
explicit operator bool() const noexcept { return has_value_; }
void value() const {
if (!has_value_)
throw std::runtime_error("Expected<void>: no value");
}
E& error() & {
if (has_value_)
throw std::runtime_error("Expected<void>: has value");
return error_;
}
const E& error() const& {
if (has_value_)
throw std::runtime_error("Expected<void>: has value");
return error_;
}
private:
bool has_value_;
E error_;
};
} // namespace sqlite_vec_cpp
#endif
namespace sqlite_vec_cpp {
/// Error codes for sqlite-vec-cpp operations
enum class ErrorCode {
Success = 0,
InvalidArgument,
InvalidDimensions,
DimensionMismatch,
InvalidElementType,
UnsupportedOperation,
MemoryAllocation,
SQLiteError,
ExtensionNotLoaded,
ParseError,
InternalError,
};
/// Error information
struct Error {
ErrorCode code{ErrorCode::InternalError};
std::string message;
int sqlite_code{0}; // Optional: store SQLite error code if applicable
// Default constructor (needed for Expected)
Error() = default;
Error(ErrorCode c, std::string msg, int sql_code = 0)
: code(c), message(std::move(msg)), sqlite_code(sql_code) {}
[[nodiscard]] const char* what() const noexcept { return message.c_str(); }
[[nodiscard]] static Error invalid_argument(std::string msg) {
return Error{ErrorCode::InvalidArgument, std::move(msg)};
}
[[nodiscard]] static Error dimension_mismatch(std::size_t expected, std::size_t actual) {
return Error{ErrorCode::DimensionMismatch, "Dimension mismatch: expected " +
std::to_string(expected) + ", got " +
std::to_string(actual)};
}
[[nodiscard]] static Error sqlite_error(std::string msg, int code) {
return Error{ErrorCode::SQLiteError, std::move(msg), code};
}
};
/// Result type for operations that may fail
template <typename T> using Result = Expected<T, Error>;
/// Void result (operation succeeded or error)
using VoidResult = Expected<void, Error>;
/// Helper to create success result
inline VoidResult ok() {
return VoidResult{};
}
/// Helper to create error result
template <typename T> inline Result<T> err(Error error) {
return Result<T>{Unexpected{std::move(error)}};
}
inline VoidResult err_void(Error error) {
return VoidResult{Unexpected{std::move(error)}};
}
} // namespace sqlite_vec_cpp