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 {
20 name_ = "TILES_OUTPUT_PAL";
21 func_ = [](const std::string &str) {
22 if (!porytiles2::tiles_pal_mode_from_str(str).has_value()) {
23 return std::string{"invalid 'tiles.png' output palette mode: " + str};
24 }
25 return std::string{};
26 };
27 }
28};
29
30class NotAlreadyAFileValidator final : public CLI::Validator {
31 static constexpr auto kHint = "PATH";
32
33 public:
34 explicit NotAlreadyAFileValidator() : Validator{kHint}
35 {
36 name_ = "NOT_ALREADY_A_FILE";
37 func_ = [](const std::string &str) {
38 if (std::filesystem::exists(str) && std::filesystem::is_regular_file(str)) {
39 return std::string{"file already exists: " + str};
40 }
41 return std::string{};
42 };
43 }
44};
45
46class RgbStringValidator final : public CLI::Validator {
47 static constexpr auto kHint = "R,G,B";
48
49 public:
50 explicit RgbStringValidator() : Validator{kHint}
51 {
52 name_ = "RGB_STRING";
53 func_ = [](const std::string &str) {
54 const std::vector<std::string> color_components = porytiles2::split(str, ",");
55 if (color_components.size() != 3) {
56 return std::string{"invalid rgb string: " + str};
57 }
58
59 const auto red_result = porytiles2::parse_int<int>(color_components[0]);
60 const auto green_result = porytiles2::parse_int<int>(color_components[1]);
61 const auto blue_result = porytiles2::parse_int<int>(color_components[2]);
62
63 if (!red_result.has_value()) {
64 return std::string{"invalid rgb red component: " + red_result.error()};
65 }
66 if (!green_result.has_value()) {
67 return std::string{"invalid rgb green component: " + green_result.error()};
68 }
69 if (!blue_result.has_value()) {
70 return std::string{"invalid rgb blue component: " + blue_result.error()};
71 }
72
73 const auto red = red_result.value();
74 const auto green = green_result.value();
75 const auto blue = blue_result.value();
76
77 if (red < 0 || red > 255) {
78 return fmt::format("rgb red component out of range: {}", red);
79 }
80 if (green < 0 || green > 255) {
81 return fmt::format("rgb green component out of range: {}", green);
82 }
83 if (blue < 0 || blue > 255) {
84 return fmt::format("rgb blue component out of range: {}", blue);
85 }
86
87 return std::string{};
88 };
89 }
90};
91
92class DiagnosticIsWarningValidator final : public CLI::Validator {
93 static constexpr auto kHint = "DIAG";
94
95 public:
96 explicit DiagnosticIsWarningValidator() : Validator{kHint}
97 {
98 name_ = "DIAGNOSTIC_IS_WARNING";
99 std::unordered_set<std::string> warning_diags;
100 // for (const auto name : porytiles2::all_diag_names(porytiles2::DiagLevel::warning)) {
101 // warning_diags.insert(name);
102 // }
103 func_ = [warning_diags](const std::string &str) {
104 if (warning_diags.contains(str)) {
105 return std::string{};
106 }
107 return std::string{"invalid warning diagnostic: " + str};
108 };
109 }
110};
std::vector< std::string > split(std::string input, const std::string &delimiter)
Splits a string into tokens based on a delimiter.
std::optional< TilesPalMode > tiles_pal_mode_from_str(const std::string &str)