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 porytiles {
10
17class Error {
18 public:
19 virtual ~Error() = default;
20
29 [[nodiscard]] virtual std::vector<std::string> details(const TextFormatter &formatter) const = 0;
30
31 [[nodiscard]] virtual std::string join(const TextFormatter &formatter, const std::string &delimiter = "\n") const
32 {
33 const auto lines = details(formatter);
34 if (lines.empty()) {
35 return "";
36 }
37
38 std::string result;
39 for (std::size_t i = 0; i < lines.size(); ++i) {
40 if (i > 0) {
41 result += delimiter;
42 }
43 result += lines[i];
44 }
45 return result;
46 }
47
48 [[nodiscard]] virtual std::unique_ptr<Error> clone() const = 0;
49};
50
57class FormattableError final : public Error {
58 public:
59 FormattableError() = default;
60
61 explicit FormattableError(std::string text)
62 {
63 if (!text.empty()) {
64 text_.push_back(std::move(text));
65 }
66 }
67
68 explicit FormattableError(std::string text, std::vector<FormatParam> params)
69 {
70 if (!text.empty() || !params.empty()) {
71 text_.push_back(std::move(text));
72 params_.push_back(std::move(params));
73 }
74 }
75
76 template <typename FirstParam, typename... RestParams>
77 requires(
78 !std::is_same_v<std::decay_t<FirstParam>, std::vector<FormatParam>> &&
79 std::is_same_v<std::decay_t<FirstParam>, FormatParam> &&
80 (std::is_same_v<std::decay_t<RestParams>, FormatParam> && ...))
81 explicit FormattableError(std::string text, FirstParam &&first, RestParams &&...rest)
82 {
83 text_.push_back(std::move(text));
84
85 std::vector<FormatParam> line_params;
86 line_params.reserve(1 + sizeof...(RestParams));
87 line_params.push_back(std::forward<FirstParam>(first));
88 (line_params.push_back(std::forward<RestParams>(rest)), ...);
89 params_.push_back(std::move(line_params));
90 }
91
92 explicit FormattableError(std::vector<std::string> lines) : text_{std::move(lines)} {}
93
94 explicit FormattableError(std::vector<std::string> lines, std::vector<std::vector<FormatParam>> params)
95 : text_{std::move(lines)}, params_{std::move(params)}
96 {
97 }
98
99 [[nodiscard]] std::vector<std::string> details(const TextFormatter &formatter) const override
100 {
101 std::vector<std::string> result;
102 result.reserve(text_.size());
103
104 for (std::size_t i = 0; i < text_.size(); ++i) {
105 if (i < params_.size() && !params_[i].empty()) {
106 result.push_back(formatter.format(text_[i], params_[i]));
107 }
108 else {
109 result.push_back(text_[i]);
110 }
111 }
112
113 return result;
114 }
115
116 [[nodiscard]] bool has_details() const
117 {
118 for (const auto &line : text_) {
119 if (!line.empty()) {
120 return true;
121 }
122 }
123 return false;
124 }
125
126 [[nodiscard]] std::unique_ptr<Error> clone() const override
127 {
128 return std::make_unique<FormattableError>(text_, params_);
129 }
130
131 private:
132 std::vector<std::string> text_;
133 std::vector<std::vector<FormatParam>> params_;
134};
135
136} // namespace porytiles
Abstract interface for all error types used in ChainableResult error chains.
Definition error.hpp:17
virtual ~Error()=default
virtual std::string join(const TextFormatter &formatter, const std::string &delimiter="\n") const
Definition error.hpp:31
virtual std::vector< std::string > details(const TextFormatter &formatter) const =0
Returns a formatted multi-line string representation of the error.
virtual std::unique_ptr< Error > clone() const =0
A text parameter with associated styling for formatted output.
General-purpose error implementation with formatted message support.
Definition error.hpp:57
FormattableError(std::string text)
Definition error.hpp:61
std::unique_ptr< Error > clone() const override
Definition error.hpp:126
std::vector< std::string > details(const TextFormatter &formatter) const override
Returns a formatted multi-line string representation of the error.
Definition error.hpp:99
FormattableError(std::vector< std::string > lines)
Definition error.hpp:92
FormattableError(std::string text, FirstParam &&first, RestParams &&...rest)
Definition error.hpp:81
FormattableError(std::vector< std::string > lines, std::vector< std::vector< FormatParam > > params)
Definition error.hpp:94
FormattableError(std::string text, std::vector< FormatParam > params)
Definition error.hpp:68
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.