Solved: 52 - Monadic Expected
This commit is contained in:
@@ -3,18 +3,23 @@
|
||||
namespace cpp26 {
|
||||
|
||||
auto parse_id(const std::string& text) -> std::expected<int, ErrorCode> {
|
||||
(void)text;
|
||||
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;
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user