Porytiles
Loading...
Searching...
No Matches
image_load_error.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <string>
5#include <utility>
6#include <variant>
7
10
11namespace porytiles2 {
12
13class ImageLoadError final : public Error {
14 public:
16
17 struct ChannelCount {
19 };
20
22 std::string load_error_;
23 };
24
25 ImageLoadError(Type type, std::string filename, std::variant<std::monostate, ChannelCount, OtherLoadError> params)
26 : type_{type}, filename_{std::move(filename)}, params_{std::move(params)}
27 {
28 }
29
30 // Convenience constructors for specific error types
31 static ImageLoadError file_not_found(const std::string &filename)
32 {
33 return ImageLoadError{Type::file_not_found, filename, std::monostate{}};
34 }
35
36 static ImageLoadError unsupported_channel_count(const std::string &filename, int channel_count)
37 {
39 }
40
41 static ImageLoadError other_load_error(const std::string &filename, const std::string &error_msg)
42 {
44 }
45
46 [[nodiscard]] Type type() const
47 {
48 return type_;
49 }
50 [[nodiscard]] const std::string &filename() const
51 {
52 return filename_;
53 }
54 [[nodiscard]] const std::variant<std::monostate, ChannelCount, OtherLoadError> &params() const
55 {
56 return params_;
57 }
58
59 [[nodiscard]] std::vector<std::string> details(const TextFormatter &formatter) const override
60 {
61 std::string message;
62 switch (type_) {
64 message = formatter.style(filename_ + ":", Style::bold) + " file not found";
65 break;
67 auto channel_count = std::get<ChannelCount>(params_).channel_count_;
68 message = formatter.style(filename_ + ":", Style::bold) +
69 " unsupported channel count: " + formatter.style(std::to_string(channel_count), Style::bold);
70 break;
71 }
73 auto load_error = std::get<OtherLoadError>(params_).load_error_;
74 message = formatter.style(filename_ + ":", Style::bold) + " could not be loaded: '" + load_error + "'";
75 break;
76 }
77 default:
78 panic("unhandled ImageLoadError type");
79 }
80 return {message};
81 }
82
83 [[nodiscard]] std::unique_ptr<Error> clone() const override
84 {
85 return std::make_unique<ImageLoadError>(this->type_, this->filename_, this->params_);
86 }
87
88 private:
89 Type type_;
90 std::string filename_;
91 std::variant<std::monostate, ChannelCount, OtherLoadError> params_;
92};
93
94} // namespace porytiles2
Abstract interface for all error types used in ChainableResult error chains.
Definition error.hpp:26
const std::string & filename() const
std::vector< std::string > details(const TextFormatter &formatter) const override
Returns a formatted multi-line string representation of the error.
const std::variant< std::monostate, ChannelCount, OtherLoadError > & params() const
static ImageLoadError other_load_error(const std::string &filename, const std::string &error_msg)
static ImageLoadError file_not_found(const std::string &filename)
std::unique_ptr< Error > clone() const override
Creates a polymorphic copy of this error.
static ImageLoadError unsupported_channel_count(const std::string &filename, int channel_count)
ImageLoadError(Type type, std::string filename, std::variant< std::monostate, ChannelCount, OtherLoadError > params)
Abstract base class for applying text styling with context-aware formatting.
virtual std::string style(const std::string &text, Style styles) const =0
Applies styling to a text string.
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.
Definition panic.hpp:53
@ bold
Bold text formatting.