#ifndef msgpack_test_utils_4573e6627d8efe78 #define msgpack_test_utils_4573e6627d8efe78 // clang-format off : Must include fmt/format before extra/formatters. #include #include "parselink/msgpack/extra/formatters.h" // clang-format on #include #include #include #include #include namespace test { template inline constexpr std::array as_bytes(T&& t) { return std::bit_cast>(std::forward(t)); } template inline constexpr std::array make_bytes( Bytes&&... bytes) noexcept { return {std::byte(std::forward(bytes))...}; } } // namespace test // This formatter allows expected values to be formatted for both its expected // and unexpected types. template struct fmt::formatter> { int width = 0; fmt::formatter t_fmtr; fmt::formatter e_fmtr; constexpr auto parse(auto& ctx) { using char_type = typename std::remove_reference_t::char_type; auto delim = char_type{'|'}; auto close_brace = char_type{'}'}; std::array fmtbuf; if (ctx.begin() == ctx.end() || *ctx.begin() == '}') { return ctx.begin(); } bool t_parsed = false; auto itr = ctx.begin(); auto buf = fmtbuf.begin(); while (*itr != close_brace) { if (*itr == delim) { if (t_parsed) { fmt::throw_format_error("multiple delims encountered"); } t_parsed = true; *buf = close_brace; auto str = basic_string_view( fmtbuf.data(), std::distance(fmtbuf.begin(), buf)); basic_format_parse_context subparser(str, 0); t_fmtr.parse(subparser); buf = fmtbuf.begin(); } else { *buf = *itr; ++buf; } ++itr; } *buf = close_brace; auto str = basic_string_view( fmtbuf.data(), std::distance(fmtbuf.begin(), buf)); basic_format_parse_context subparser(str, 0); if (t_parsed) { e_fmtr.parse(subparser); } else { t_fmtr.parse(subparser); } ctx.advance_to(itr); return itr; } auto format(tl::expected const& v, auto& ctx) const { if (v) { // return fmt::format_to(ctx.out(), "{}", v.value()); return t_fmtr.format(v.value(), ctx); } else { return e_fmtr.format(v.error(), ctx); } } }; #endif // msgpack_test_utils_4573e6627d8efe78