Porytiles
Loading...
Searching...
No Matches
error.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <string>
5#include <vector>
6
8
9namespace porytiles2 {
10
26class Error {
27 public:
28 virtual ~Error() = default;
29
45 [[nodiscard]] virtual std::vector<std::string> details(const TextFormatter &formatter) const = 0;
46
59 [[nodiscard]] virtual std::string join(const TextFormatter &formatter, const std::string &delimiter = "\n") const
60 {
61 const auto lines = details(formatter);
62 if (lines.empty()) {
63 return "";
64 }
65
66 std::string result;
67 for (std::size_t i = 0; i < lines.size(); ++i) {
68 if (i > 0) {
69 result += delimiter;
70 }
71 result += lines[i];
72 }
73 return result;
74 }
75
86 [[nodiscard]] virtual std::unique_ptr<Error> clone() const = 0;
87};
88
117class FormattableError final : public Error {
118 public:
127 FormattableError() = default;
128
139 explicit FormattableError(std::string text)
140 {
141 if (!text.empty()) {
142 text_.push_back(std::move(text));
143 }
144 }
145
157 explicit FormattableError(std::string text, std::vector<FormatParam> params)
158 {
159 if (!text.empty() || !params.empty()) {
160 text_.push_back(std::move(text));
161 params_.push_back(std::move(params));
162 }
163 }
164
184 template <typename FirstParam, typename... RestParams>
185 requires(
186 !std::is_same_v<std::decay_t<FirstParam>, std::vector<FormatParam>> &&
187 std::is_same_v<std::decay_t<FirstParam>, FormatParam> &&
188 (std::is_same_v<std::decay_t<RestParams>, FormatParam> && ...))
189 explicit FormattableError(std::string text, FirstParam &&first, RestParams &&...rest)
190 {
191 text_.push_back(std::move(text));
192
193 std::vector<FormatParam> line_params;
194 line_params.reserve(1 + sizeof...(RestParams));
195 line_params.push_back(std::forward<FirstParam>(first));
196 (line_params.push_back(std::forward<RestParams>(rest)), ...);
197 params_.push_back(std::move(line_params));
198 }
199
209 explicit FormattableError(std::vector<std::string> lines) : text_{std::move(lines)} {}
210
225 explicit FormattableError(std::vector<std::string> lines, std::vector<std::vector<FormatParam>> params)
226 : text_{std::move(lines)}, params_{std::move(params)}
227 {
228 }
229
242 [[nodiscard]] std::vector<std::string> details(const TextFormatter &formatter) const override
243 {
244 std::vector<std::string> result;
245 result.reserve(text_.size());
246
247 for (std::size_t i = 0; i < text_.size(); ++i) {
248 if (i < params_.size() && !params_[i].empty()) {
249 result.push_back(formatter.format(text_[i], params_[i]));
250 }
251 else {
252 result.push_back(text_[i]);
253 }
254 }
255
256 return result;
257 }
258
270 [[nodiscard]] bool has_details() const
271 {
272 for (const auto &line : text_) {
273 if (!line.empty()) {
274 return true;
275 }
276 }
277 return false;
278 }
279
289 [[nodiscard]] std::unique_ptr<Error> clone() const override
290 {
291 return std::make_unique<FormattableError>(text_, params_);
292 }
293
294 private:
295 std::vector<std::string> text_;
296 std::vector<std::vector<FormatParam>> params_;
297};
298
299} // namespace porytiles2
Abstract interface for all error types used in ChainableResult error chains.
Definition error.hpp:26
virtual std::unique_ptr< Error > clone() const =0
Creates a polymorphic copy of this error.
virtual ~Error()=default
virtual std::string join(const TextFormatter &formatter, const std::string &delimiter="\n") const
Joins the error details into a single string with a specified delimiter.
Definition error.hpp:59
virtual std::vector< std::string > details(const TextFormatter &formatter) const =0
Returns a formatted multi-line string representation of the error.
A text parameter with associated styling for formatted output.
General-purpose error implementation with formatted message support.
Definition error.hpp:117
FormattableError(std::string text)
Constructs a FormattableError with a plain text message.
Definition error.hpp:139
std::unique_ptr< Error > clone() const override
Creates a polymorphic copy of this FormattableError.
Definition error.hpp:289
FormattableError(std::vector< std::string > lines)
Constructs a FormattableError with multiple plain text lines.
Definition error.hpp:209
FormattableError(std::vector< std::string > lines, std::vector< std::vector< FormatParam > > params)
Constructs a FormattableError with multiple formatted lines.
Definition error.hpp:225
std::vector< std::string > details(const TextFormatter &formatter) const override
Returns the formatted error message lines with appropriate styling.
Definition error.hpp:242
FormattableError(std::string text, std::vector< FormatParam > params)
Constructs a FormattableError with a format string and styled parameters.
Definition error.hpp:157
FormattableError()=default
Constructs an empty FormattableError with no message.
FormattableError(std::string text, FirstParam &&first, RestParams &&...rest)
Constructs a FormattableError with a format string and variadic styled parameters.
Definition error.hpp:189
bool has_details() const
Checks whether this FormattableError contains any message content.
Definition error.hpp:270
Abstract base class for applying text styling with context-aware formatting.
virtual std::string format(const std::string &format_str, const std::vector< FormatParam > &params) const
Formats a string with styled parameters using fmtlib syntax.