6 #ifndef KAGOME_OUTCOME_HPP 7 #define KAGOME_OUTCOME_HPP 9 #include <fmt/format.h> 10 #include <libp2p/outcome/outcome.hpp> 13 using libp2p::outcome::failure;
14 using libp2p::outcome::result;
15 using libp2p::outcome::success;
19 struct fmt::formatter<
std::error_code> {
21 constexpr
auto parse(format_parse_context &ctx) -> decltype(ctx.begin()) {
23 auto it = ctx.begin(), end = ctx.end();
26 if (it != end && *it !=
'}') {
27 throw format_error(
"invalid format");
35 template <
typename FormatContext>
36 auto format(
const std::error_code &ec, FormatContext &ctx)
37 -> decltype(ctx.out()) {
40 return format_to(ctx.out(), ec.message());
44 template <
typename Result,
typename Failure>
45 struct fmt::formatter<
outcome::result<Result, Failure>> {
47 constexpr
auto parse(format_parse_context &ctx) -> decltype(ctx.begin()) {
49 auto it = ctx.begin(), end = ctx.end();
52 if (it != end && *it !=
'}') {
53 throw format_error(
"invalid format");
61 template <
typename FormatContext>
62 auto format(
const outcome::result<Result, Failure> &res, FormatContext &ctx)
63 -> decltype(ctx.out()) {
66 if (res.has_value()) {
67 if constexpr (not std::is_same_v<Result, void>) {
68 return format_to(ctx.out(), res.has_value());
70 return format_to(ctx.out(),
"success");
73 return format_to(ctx.out(), res.error().message());
78 #endif // KAGOME_OUTCOME_HPP