48enum class Type : std::uint_fast8_t {
65[[nodiscard]]
auto from_string(std::string_view text)
66 -> std::expected<Type, std::string>;
72[[nodiscard, gnu::const]]
constexpr auto to_char(
73 Type type,
bool uppercase =
true)
85struct std::formatter<
chess::pieces::Type> final {
86 template <
typename ParseContext>
87 constexpr auto parse(ParseContext& ctx) ->
typename ParseContext::iterator
92 template <
typename FormatContext>
95 ->
typename FormatContext::iterator
97 return std::format_to(ctx.out(),
"{}", to_char(piece));
120constexpr auto to_char(
const Type type,
const bool uppercase) ->
char
123 static constexpr std::string_view upperChars {
"PNBRQK" };
125 return upperChars.at(std::to_underlying(type));
128 static constexpr std::string_view lowerChars {
"pnbrqk" };
130 return lowerChars.at(std::to_underlying(type));
134 -> std::expected<Type, std::string>
136 if (text.length() != 1uz)
137 text = text.substr(0uz, 1uz);
139 switch (text.front()) {
140 case 'p': [[fallthrough]];
143 case 'n': [[fallthrough]];
146 case 'b': [[fallthrough]];
149 case 'r': [[fallthrough]];
152 case 'q': [[fallthrough]];
155 case 'k': [[fallthrough]];
159 return std::unexpected {
161 "Cannot parse piece type from invalid input string: {}",
auto from_string(std::string_view text) -> std::expected< Type, std::string >
constexpr auto to_char(Type type, bool uppercase=true) -> char