Solved: 52 - Monadic Expected

This commit is contained in:
2026-08-24 19:43:33 +03:00
parent 45bcd09bc1
commit 64b29cfebe
+12 -7
View File
@@ -3,18 +3,23 @@
namespace cpp26 { namespace cpp26 {
auto parse_id(const std::string& text) -> std::expected<int, ErrorCode> { auto parse_id(const std::string& text) -> std::expected<int, ErrorCode> {
(void)text; int id = 0;
return std::unexpected(ErrorCode::Invalid); if (auto [ptr, ec] = std::from_chars(text.data(), text.data() + text.size(), id);
ec != std::errc{} || ptr != text.data() + text.size()) {
return std::unexpected(ErrorCode::Invalid);
}
return id;
} }
auto load_user(int id) -> std::expected<std::string, ErrorCode> { auto load_user(const int id) -> std::expected<std::string, ErrorCode> {
(void)id; if (id == 999) {
return std::unexpected(ErrorCode::NotFound); return std::unexpected(ErrorCode::NotFound);
}
return "User-" + std::to_string(id);
} }
auto load_user_name(const std::string& text) -> std::expected<std::string, ErrorCode> { auto load_user_name(const std::string& text) -> std::expected<std::string, ErrorCode> {
(void)text; return parse_id(text).and_then(load_user);
return std::unexpected(ErrorCode::Invalid);
} }
} // namespace cpp26 } // namespace cpp26