mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-30 10:47:35 +02:00
added formatter for std::expected (#3834)
This commit is contained in:
@ -38,6 +38,10 @@
|
|||||||
# endif
|
# endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if FMT_HAS_INCLUDE(<expected>) && FMT_CPLUSPLUS > 202002L
|
||||||
|
# include <expected>
|
||||||
|
#endif
|
||||||
|
|
||||||
#if FMT_CPLUSPLUS > 201703L && FMT_HAS_INCLUDE(<source_location>)
|
#if FMT_CPLUSPLUS > 201703L && FMT_HAS_INCLUDE(<source_location>)
|
||||||
# include <source_location>
|
# include <source_location>
|
||||||
#endif
|
#endif
|
||||||
@ -242,6 +246,56 @@ struct formatter<std::optional<T>, Char,
|
|||||||
FMT_END_NAMESPACE
|
FMT_END_NAMESPACE
|
||||||
#endif // __cpp_lib_optional
|
#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
|
#ifdef __cpp_lib_source_location
|
||||||
FMT_BEGIN_NAMESPACE
|
FMT_BEGIN_NAMESPACE
|
||||||
FMT_EXPORT
|
FMT_EXPORT
|
||||||
@ -291,16 +345,6 @@ template <typename T, typename C> class is_variant_formattable_ {
|
|||||||
decltype(check(variant_index_sequence<T>{}))::value;
|
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
|
} // namespace detail
|
||||||
|
|
||||||
template <typename T> struct is_variant_like {
|
template <typename T> struct is_variant_like {
|
||||||
@ -346,7 +390,7 @@ struct formatter<
|
|||||||
FMT_TRY {
|
FMT_TRY {
|
||||||
std::visit(
|
std::visit(
|
||||||
[&](const auto& v) {
|
[&](const auto& v) {
|
||||||
out = detail::write_variant_alternative<Char>(out, v);
|
out = detail::write_escaped_alternative<Char>(out, v);
|
||||||
},
|
},
|
||||||
value);
|
value);
|
||||||
}
|
}
|
||||||
|
@ -102,6 +102,26 @@ TEST(std_test, optional) {
|
|||||||
#endif
|
#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 {
|
namespace my_nso {
|
||||||
enum class my_number {
|
enum class my_number {
|
||||||
one,
|
one,
|
||||||
|
Reference in New Issue
Block a user