Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added formatter for std::expected #3834

Merged
merged 5 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions include/fmt/std.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
# if FMT_HAS_INCLUDE(<optional>)
# include <optional>
# endif
# if FMT_HAS_INCLUDE(<expected>)
# include <expected>
# endif
#endif
dominicpoeschko marked this conversation as resolved.
Show resolved Hide resolved

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

#ifdef __cpp_lib_expected
FMT_BEGIN_NAMESPACE
namespace detail {

template <typename Char, typename OutputIt, typename T>
auto write_expected_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
dominicpoeschko marked this conversation as resolved.
Show resolved Hide resolved
return write<Char>(out, v);
}

} // namespace detail

FMT_EXPORT
template <typename T, typename E, typename Char>
struct formatter<std::expected<T, E>, Char,
std::enable_if_t<std::conjunction_v<
is_formattable<T, Char>, is_formattable<E, Char>>>> {
dominicpoeschko marked this conversation as resolved.
Show resolved Hide resolved
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_expected_alternative<Char>(out, value.value());
} else {
out = detail::write<Char>(out, "unexpected(");
out = detail::write_expected_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
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
Loading