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