Skip to content

Commit

Permalink
added formatter for std::expected (#3834)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicpoeschko committed Feb 3, 2024
1 parent 9f5f39c commit e5bab8d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 11 deletions.
66 changes: 55 additions & 11 deletions include/fmt/std.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
# endif
#endif

#if FMT_HAS_INCLUDE(<expected>) && FMT_CPLUSPLUS > 202002L
# include <expected>
#endif

#if FMT_CPLUSPLUS > 201703L && FMT_HAS_INCLUDE(<source_location>)
# include <source_location>
#endif
Expand Down Expand Up @@ -242,6 +246,56 @@ struct formatter<std::optional<T>, Char,
FMT_END_NAMESPACE
#endif // __cpp_lib_optional

#if defined(__cpp_lib_expected) || FMT_CPP_LIB_VARIANT

FMT_BEGIN_NAMESPACE
namespace detail {

template <typename Char, typename OutputIt, typename T>
auto write_escaped_alternative(OutputIt out, const T& v) -> OutputIt {
if constexpr (has_to_string_view<T>::value)
return write_escaped_string<Char>(out, detail::to_string_view(v));
if constexpr (std::is_same_v<T, Char>) return write_escaped_char(out, v);
return write<Char>(out, v);
}

} // namespace detail

FMT_END_NAMESPACE
#endif

#ifdef __cpp_lib_expected
FMT_BEGIN_NAMESPACE

FMT_EXPORT
template <typename T, typename E, typename Char>
struct formatter<
std::expected<T, E>, Char,
std::enable_if_t<is_formattable<T, Char> && is_formattable<E, Char>>> {
template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
return ctx.begin();
}

template <typename FormatContext>
auto format(const std::expected<T, E>& value, FormatContext& ctx) const
-> decltype(ctx.out()) {
auto out = ctx.out();

if (value.has_value()) {
out = detail::write<Char>(out, "expected(");
out = detail::write_escaped_alternative<Char>(out, value.value());
} else {
out = detail::write<Char>(out, "unexpected(");
out = detail::write_escaped_alternative<Char>(out, value.error());
}
*out++ = ')';
return out;
}
};
FMT_END_NAMESPACE
#endif // __cpp_lib_expected

#ifdef __cpp_lib_source_location
FMT_BEGIN_NAMESPACE
FMT_EXPORT
Expand Down Expand Up @@ -291,16 +345,6 @@ template <typename T, typename C> class is_variant_formattable_ {
decltype(check(variant_index_sequence<T>{}))::value;
};

template <typename Char, typename OutputIt, typename T>
auto write_variant_alternative(OutputIt out, const T& v) -> OutputIt {
if constexpr (has_to_string_view<T>::value)
return write_escaped_string<Char>(out, detail::to_string_view(v));
else if constexpr (std::is_same_v<T, Char>)
return write_escaped_char(out, v);
else
return write<Char>(out, v);
}

} // namespace detail

template <typename T> struct is_variant_like {
Expand Down Expand Up @@ -346,7 +390,7 @@ struct formatter<
FMT_TRY {
std::visit(
[&](const auto& v) {
out = detail::write_variant_alternative<Char>(out, v);
out = detail::write_escaped_alternative<Char>(out, v);
},
value);
}
Expand Down
20 changes: 20 additions & 0 deletions test/std-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,26 @@ TEST(std_test, optional) {
#endif
}

TEST(std_test, expected) {
#ifdef __cpp_lib_expected
EXPECT_EQ(fmt::format("{}", std::expected<int, int>{1}), "expected(1)");
EXPECT_EQ(fmt::format("{}", std::expected<int, int>{std::unexpected(1)}), "unexpected(1)");
EXPECT_EQ(fmt::format("{}", std::expected<std::string, int>{"test"}), "expected(\"test\")");
EXPECT_EQ(fmt::format("{}", std::expected<int, std::string>{std::unexpected("test")}), "unexpected(\"test\")");
EXPECT_EQ(fmt::format("{}", std::expected<char, int>{'a'}), "expected('a')");
EXPECT_EQ(fmt::format("{}", std::expected<int, char>{std::unexpected('a')}), "unexpected('a')");

struct unformattable1 {};
struct unformattable2 {};
EXPECT_FALSE((fmt::is_formattable<unformattable1>::value));
EXPECT_FALSE((fmt::is_formattable<unformattable2>::value));
EXPECT_FALSE((fmt::is_formattable<std::expected<unformattable1, unformattable2>>::value));
EXPECT_FALSE((fmt::is_formattable<std::expected<unformattable1, int>>::value));
EXPECT_FALSE((fmt::is_formattable<std::expected<int, unformattable2>>::value));
EXPECT_TRUE((fmt::is_formattable<std::expected<int, int>>::value));
#endif
}

namespace my_nso {
enum class my_number {
one,
Expand Down

0 comments on commit e5bab8d

Please sign in to comment.