ladybird/Userland/Libraries/LibAudio/LoaderError.h
Ali Mohammad Pur 5e1499d104 Everywhere: Rename {Deprecated => Byte}String
This commit un-deprecates DeprecatedString, and repurposes it as a byte
string.
As the null state has already been removed, there are no other
particularly hairy blockers in repurposing this type as a byte string
(what it _really_ is).

This commit is auto-generated:
  $ xs=$(ack -l \bDeprecatedString\b\|deprecated_string AK Userland \
    Meta Ports Ladybird Tests Kernel)
  $ perl -pie 's/\bDeprecatedString\b/ByteString/g;
    s/deprecated_string/byte_string/g' $xs
  $ clang-format --style=file -i \
    $(git diff --name-only | grep \.cpp\|\.h)
  $ gn format $(git ls-files '*.gn' '*.gni')
2023-12-17 18:25:10 +03:30

97 lines
2.7 KiB
C++

/*
* Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/DeprecatedFlyString.h>
#include <AK/Error.h>
#include <errno.h>
namespace Audio {
struct LoaderError {
enum class Category : u32 {
// The error category is unknown.
Unknown = 0,
IO,
// The read file doesn't follow the file format.
Format,
// Equivalent to an ASSERT(), except non-crashing.
Internal,
// The loader encountered something in the format that is not yet implemented.
Unimplemented,
};
Category category { Category::Unknown };
// Binary index: where in the file the error occurred.
size_t index { 0 };
DeprecatedFlyString description { ByteString::empty() };
constexpr LoaderError() = default;
LoaderError(Category category, size_t index, DeprecatedFlyString description)
: category(category)
, index(index)
, description(move(description))
{
}
LoaderError(DeprecatedFlyString description)
: description(move(description))
{
}
LoaderError(Category category, DeprecatedFlyString description)
: category(category)
, description(move(description))
{
}
LoaderError(LoaderError&) = default;
LoaderError(LoaderError&&) = default;
LoaderError(Error&& error)
{
if (error.is_errno()) {
auto code = error.code();
description = ByteString::formatted("{} ({})", strerror(code), code);
if (code == EBADF || code == EBUSY || code == EEXIST || code == EIO || code == EISDIR || code == ENOENT || code == ENOMEM || code == EPIPE)
category = Category::IO;
} else {
description = error.string_literal();
}
}
};
}
namespace AK {
template<>
struct Formatter<Audio::LoaderError> : Formatter<FormatString> {
ErrorOr<void> format(FormatBuilder& builder, Audio::LoaderError const& error)
{
StringView category;
switch (error.category) {
case Audio::LoaderError::Category::Unknown:
category = "Unknown"sv;
break;
case Audio::LoaderError::Category::IO:
category = "I/O"sv;
break;
case Audio::LoaderError::Category::Format:
category = "Format"sv;
break;
case Audio::LoaderError::Category::Internal:
category = "Internal"sv;
break;
case Audio::LoaderError::Category::Unimplemented:
category = "Unimplemented"sv;
break;
}
return Formatter<FormatString>::format(builder, "{} error: {} (at {})"sv, category, error.description, error.index);
}
};
}