LibMedia: Add a formatter for DecoderErrorCategory

...and use it in the DecoderError formatter, as the category is often
more relevant than the error message.
This commit is contained in:
Zaggy1024 2026-02-10 23:47:50 -06:00 committed by Gregory Bertilson
parent dc95107eb6
commit 6859c708c1

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Gregory Bertilson <zaggy1024@gmail.com>
* Copyright (c) 2022-2026, Gregory Bertilson <gregory@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -18,7 +18,7 @@ namespace Media {
template<typename T>
using DecoderErrorOr = ErrorOr<T, DecoderError>;
enum class DecoderErrorCategory : u32 {
enum class DecoderErrorCategory : u8 {
Aborted,
Unknown,
IO,
@ -83,6 +83,31 @@ private:
Variant<StringView, ByteString> m_description;
};
constexpr StringView decoder_error_category_to_string(DecoderErrorCategory category)
{
switch (category) {
case DecoderErrorCategory::Aborted:
return "Aborted"sv;
case DecoderErrorCategory::Unknown:
return "Unknown"sv;
case DecoderErrorCategory::IO:
return "IO"sv;
case DecoderErrorCategory::NeedsMoreInput:
return "NeedsMoreInput"sv;
case DecoderErrorCategory::EndOfStream:
return "EndOfStream"sv;
case DecoderErrorCategory::Memory:
return "Memory"sv;
case DecoderErrorCategory::Corrupted:
return "Corrupted"sv;
case DecoderErrorCategory::Invalid:
return "Invalid"sv;
case DecoderErrorCategory::NotImplemented:
return "NotImplemented"sv;
}
return "Invalid"sv;
}
#define DECODER_TRY(category, expression) \
({ \
auto&& _result = ((expression)); \
@ -102,11 +127,19 @@ private:
namespace AK {
template<>
struct Formatter<Media::DecoderErrorCategory> : StandardFormatter {
ErrorOr<void> format(FormatBuilder& builder, Media::DecoderErrorCategory const& decoder_error_category)
{
return builder.put_literal(Media::decoder_error_category_to_string(decoder_error_category));
}
};
template<>
struct Formatter<Media::DecoderError> : Formatter<FormatString> {
ErrorOr<void> format(FormatBuilder& builder, Media::DecoderError const& decoder_error)
{
return Formatter<FormatString>::format(builder, "[DecoderError]: {}"sv, decoder_error.description());
return Formatter<FormatString>::format(builder, "[DecoderError] ({}): {}"sv, decoder_error.category(), decoder_error.description());
}
};