Porytiles
Loading...
Searching...
No Matches
validators.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <expected>
4#include <filesystem>
5#include <unordered_set>
6
7#include <CLI/CLI.hpp>
8#include <fmt/format.h>
9
13
14class TilesPalModeValidator final : public CLI::Validator {
15 static constexpr auto kHint = "MODE";
16
17 public:
18 explicit TilesPalModeValidator() : Validator{kHint} {
19 name_ = "TILES_OUTPUT_PAL";
20 func_ = [](const std::string &str) {
21 if (!porytiles::TilesPalModeFromStr(str).has_value()) {
22 return std::string{"invalid 'tiles.png' output palette mode: " + str};
23 }
24 return std::string{};
25 };
26 }
27};
28
29class NotAlreadyAFileValidator final : public CLI::Validator {
30 static constexpr auto kHint = "PATH";
31
32 public:
33 explicit NotAlreadyAFileValidator() : Validator{kHint} {
34 name_ = "NOT_ALREADY_A_FILE";
35 func_ = [](const std::string &str) {
36 if (std::filesystem::exists(str) && std::filesystem::is_regular_file(str)) {
37 return std::string{"file already exists: " + str};
38 }
39 return std::string{};
40 };
41 }
42};
43
44class RgbStringValidator final : public CLI::Validator {
45 static constexpr auto kHint = "R,G,B";
46
47 public:
48 explicit RgbStringValidator() : Validator{kHint} {
49 name_ = "RGB_STRING";
50 func_ = [](const std::string &str) {
51 const std::vector<std::string> color_components = porytiles::split(str, ",");
52 if (color_components.size() != 3) {
53 return std::string{"invalid rgb string: " + str};
54 }
55
56 const auto red_result = porytiles::ParseInt<int>(color_components[0]);
57 const auto green_result = porytiles::ParseInt<int>(color_components[1]);
58 const auto blue_result = porytiles::ParseInt<int>(color_components[2]);
59
60 if (!red_result.has_value()) {
61 return std::string{"invalid rgb red component: " + red_result.error()};
62 }
63 if (!green_result.has_value()) {
64 return std::string{"invalid rgb green component: " + green_result.error()};
65 }
66 if (!blue_result.has_value()) {
67 return std::string{"invalid rgb blue component: " + blue_result.error()};
68 }
69
70 const auto red = red_result.value();
71 const auto green = green_result.value();
72 const auto blue = blue_result.value();
73
74 if (red < 0 || red > 255) {
75 return fmt::format("rgb red component out of range: {}", red);
76 }
77 if (green < 0 || green > 255) {
78 return fmt::format("rgb green component out of range: {}", green);
79 }
80 if (blue < 0 || blue > 255) {
81 return fmt::format("rgb blue component out of range: {}", blue);
82 }
83
84 return std::string{};
85 };
86 }
87};
88
89class DiagnosticIsWarningValidator final : public CLI::Validator {
90 static constexpr auto kHint = "DIAG";
91
92 public:
93 explicit DiagnosticIsWarningValidator() : Validator{kHint} {
94 name_ = "DIAGNOSTIC_IS_WARNING";
95 std::unordered_set<std::string> warning_diags;
97 warning_diags.insert(name);
98 }
99 func_ = [warning_diags](const std::string &str) {
100 if (warning_diags.contains(str)) {
101 return std::string{};
102 }
103 return std::string{"invalid warning diagnostic: " + str};
104 };
105 }
106};
std::vector< const char * > AllDiagNames()
Gets an iterable view of all DiagTempl names in the internal table.
std::optional< TilesPalMode > TilesPalModeFromStr(const std::string &str)
std::vector< std::string > split(std::string input, const std::string &delimiter)
Definition utilities.cpp:17