AK+LibTimeZone: Add debug only formatter for Optional

I found this handy for debugging, and so might others.

This now also adds a formatter for TimeZone::TimeZone. This is needed
for FormatIfSupported<Optional<TimeZone::TimeZone>> to compile. As
FormatIfSupported sees a formatter for Optional exists, but not that
there's not one for TimeZone::TimeZone.
This commit is contained in:
MacDue 2023-04-18 18:31:00 +01:00 committed by Andreas Kling
parent fc1fd907b4
commit 454ecf24ea
Notes: sideshowbarker 2024-07-17 00:25:35 +09:00
2 changed files with 21 additions and 0 deletions

View File

@ -715,6 +715,18 @@ struct Formatter<ErrorOr<T, ErrorType>> : Formatter<FormatString> {
}
};
template<typename T>
struct Formatter<Optional<T>> : Formatter<FormatString> {
static constexpr bool is_debug_only() { return true; }
ErrorOr<void> format(FormatBuilder& builder, Optional<T> const& optional)
{
if (optional.has_value())
return Formatter<FormatString>::format(builder, "Optional({})"sv, *optional);
return builder.put_literal("OptionalNone"sv);
}
};
} // namespace AK
#if USING_AK_GLOBALLY

View File

@ -9,6 +9,7 @@
#include <AK/Array.h>
#include <AK/DeprecatedString.h>
#include <AK/Error.h>
#include <AK/Format.h>
#include <AK/Optional.h>
#include <AK/StringView.h>
#include <AK/Time.h>
@ -74,3 +75,11 @@ StringView region_to_string(Region region);
Vector<StringView> time_zones_in_region(StringView region);
}
template<>
struct AK::Formatter<TimeZone::TimeZone> : Formatter<FormatString> {
ErrorOr<void> format(FormatBuilder& builder, TimeZone::TimeZone const& time_zone)
{
return Formatter<FormatString>::format(builder, TimeZone::time_zone_to_string(time_zone));
}
};