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 {
auto parse_id(const std::string& text) -> std::expected<int, ErrorCode> {
(void)text;
return std::unexpected(ErrorCode::Invalid);
int id = 0;
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> {
(void)id;
return std::unexpected(ErrorCode::NotFound);
auto load_user(const int id) -> std::expected<std::string, ErrorCode> {
if (id == 999) {
return std::unexpected(ErrorCode::NotFound);
}
return "User-" + std::to_string(id);
}
auto load_user_name(const std::string& text) -> std::expected<std::string, ErrorCode> {
(void)text;
return std::unexpected(ErrorCode::Invalid);
return parse_id(text).and_then(load_user);
}
} // namespace cpp26