1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 11:07:46 +00:00

AK: Convert AK::Format formatting helpers to returning ErrorOr<void>

This isn't a complete conversion to ErrorOr<void>, but a good chunk.
The end goal here is to propagate buffer allocation failures to the
caller, and allow the use of TRY() with formatting functions.
This commit is contained in:
Andreas Kling 2021-11-16 01:15:21 +01:00
parent 008355c222
commit 216e21a1fa
87 changed files with 450 additions and 448 deletions

View file

@ -166,7 +166,7 @@ namespace AK {
template<>
struct Formatter<PDF::Command> : Formatter<StringView> {
void format(FormatBuilder& format_builder, PDF::Command const& command)
ErrorOr<void> format(FormatBuilder& format_builder, PDF::Command const& command)
{
StringBuilder builder;
builder.appendff("{} ({})",
@ -180,7 +180,7 @@ struct Formatter<PDF::Command> : Formatter<StringView> {
builder.append(" ]");
}
Formatter<StringView>::format(format_builder, builder.to_string());
return Formatter<StringView>::format(format_builder, builder.to_string());
}
};

View file

@ -143,9 +143,9 @@ namespace AK {
template<>
struct Formatter<PDF::Rectangle> : Formatter<StringView> {
void format(FormatBuilder& builder, PDF::Rectangle const& rectangle)
ErrorOr<void> format(FormatBuilder& builder, PDF::Rectangle const& rectangle)
{
Formatter<StringView>::format(builder,
return Formatter<StringView>::format(builder,
String::formatted("Rectangle {{ ll=({}, {}), ur=({}, {}) }}",
rectangle.lower_left_x,
rectangle.lower_left_y,
@ -156,7 +156,7 @@ struct Formatter<PDF::Rectangle> : Formatter<StringView> {
template<>
struct Formatter<PDF::Page> : Formatter<StringView> {
void format(FormatBuilder& builder, PDF::Page const& page)
ErrorOr<void> format(FormatBuilder& builder, PDF::Page const& page)
{
constexpr auto fmt_string = "Page {{\n resources={}\n contents={}\n media_box={}\n crop_box={}\n user_unit={}\n rotate={}\n}}";
auto str = String::formatted(fmt_string,
@ -166,13 +166,13 @@ struct Formatter<PDF::Page> : Formatter<StringView> {
page.crop_box,
page.user_unit,
page.rotate);
Formatter<StringView>::format(builder, str);
return Formatter<StringView>::format(builder, str);
}
};
template<>
struct Formatter<PDF::Destination> : Formatter<StringView> {
void format(FormatBuilder& builder, PDF::Destination const& destination)
ErrorOr<void> format(FormatBuilder& builder, PDF::Destination const& destination)
{
String type_str;
switch (destination.type) {
@ -207,21 +207,21 @@ struct Formatter<PDF::Destination> : Formatter<StringView> {
param_builder.appendff("{} ", param);
auto str = String::formatted("{{ type={} page={} params={} }}", type_str, destination.page, param_builder.to_string());
Formatter<StringView>::format(builder, str);
return Formatter<StringView>::format(builder, str);
}
};
template<>
struct Formatter<PDF::OutlineItem> : Formatter<StringView> {
void format(FormatBuilder& builder, PDF::OutlineItem const& item)
ErrorOr<void> format(FormatBuilder& builder, PDF::OutlineItem const& item)
{
Formatter<StringView>::format(builder, item.to_string(0));
return Formatter<StringView>::format(builder, item.to_string(0));
}
};
template<>
struct Formatter<PDF::OutlineDict> : Formatter<StringView> {
void format(FormatBuilder& builder, PDF::OutlineDict const& dict)
ErrorOr<void> format(FormatBuilder& builder, PDF::OutlineDict const& dict)
{
StringBuilder child_builder;
child_builder.append('[');
@ -229,7 +229,7 @@ struct Formatter<PDF::OutlineDict> : Formatter<StringView> {
child_builder.appendff("{}\n", child.to_string(2));
child_builder.append(" ]");
Formatter<StringView>::format(builder,
return Formatter<StringView>::format(builder,
String::formatted("OutlineDict {{\n count={}\n children={}\n}}", dict.count, child_builder.to_string()));
}
};

View file

@ -63,17 +63,17 @@ namespace AK {
template<PDF::IsObject T>
struct Formatter<T> : Formatter<StringView> {
void format(FormatBuilder& builder, T const& object)
ErrorOr<void> format(FormatBuilder& builder, T const& object)
{
Formatter<StringView>::format(builder, object.to_string(0));
return Formatter<StringView>::format(builder, object.to_string(0));
}
};
template<PDF::IsObject T>
struct Formatter<NonnullRefPtr<T>> : Formatter<T> {
void format(FormatBuilder& builder, NonnullRefPtr<T> const& object)
ErrorOr<void> format(FormatBuilder& builder, NonnullRefPtr<T> const& object)
{
Formatter<T>::format(builder, *object);
return Formatter<T>::format(builder, *object);
}
};

View file

@ -1187,7 +1187,7 @@ namespace AK {
template<>
struct Formatter<PDF::Parser::LinearizationDictionary> : Formatter<StringView> {
void format(FormatBuilder& format_builder, PDF::Parser::LinearizationDictionary const& dict)
ErrorOr<void> format(FormatBuilder& format_builder, PDF::Parser::LinearizationDictionary const& dict)
{
StringBuilder builder;
builder.append("{\n");
@ -1202,13 +1202,13 @@ struct Formatter<PDF::Parser::LinearizationDictionary> : Formatter<StringView> {
builder.appendff(" offset_of_main_xref_table={}\n", dict.offset_of_main_xref_table);
builder.appendff(" first_page={}\n", dict.first_page);
builder.append('}');
Formatter<StringView>::format(format_builder, builder.to_string());
return Formatter<StringView>::format(format_builder, builder.to_string());
}
};
template<>
struct Formatter<PDF::Parser::PageOffsetHintTable> : Formatter<StringView> {
void format(FormatBuilder& format_builder, PDF::Parser::PageOffsetHintTable const& table)
ErrorOr<void> format(FormatBuilder& format_builder, PDF::Parser::PageOffsetHintTable const& table)
{
StringBuilder builder;
builder.append("{\n");
@ -1226,13 +1226,13 @@ struct Formatter<PDF::Parser::PageOffsetHintTable> : Formatter<StringView> {
builder.appendff(" bits_required_for_fraction_numerator={}\n", table.bits_required_for_fraction_numerator);
builder.appendff(" shared_object_reference_fraction_denominator={}\n", table.shared_object_reference_fraction_denominator);
builder.append('}');
Formatter<StringView>::format(format_builder, builder.to_string());
return Formatter<StringView>::format(format_builder, builder.to_string());
}
};
template<>
struct Formatter<PDF::Parser::PageOffsetHintTableEntry> : Formatter<StringView> {
void format(FormatBuilder& format_builder, PDF::Parser::PageOffsetHintTableEntry const& entry)
ErrorOr<void> format(FormatBuilder& format_builder, PDF::Parser::PageOffsetHintTableEntry const& entry)
{
StringBuilder builder;
builder.append("{\n");
@ -1250,7 +1250,7 @@ struct Formatter<PDF::Parser::PageOffsetHintTableEntry> : Formatter<StringView>
builder.appendff(" page_content_stream_offset_number={}\n", entry.page_content_stream_offset_number);
builder.appendff(" page_content_stream_length_number={}\n", entry.page_content_stream_length_number);
builder.append('}');
Formatter<StringView>::format(format_builder, builder.to_string());
return Formatter<StringView>::format(format_builder, builder.to_string());
}
};

View file

@ -135,43 +135,37 @@ namespace AK {
template<>
struct Formatter<PDF::LineCapStyle> : Formatter<StringView> {
void format(FormatBuilder& builder, PDF::LineCapStyle const& style)
ErrorOr<void> format(FormatBuilder& builder, PDF::LineCapStyle const& style)
{
switch (style) {
case PDF::LineCapStyle::ButtCap:
Formatter<StringView>::format(builder, "LineCapStyle::ButtCap");
break;
return Formatter<StringView>::format(builder, "LineCapStyle::ButtCap");
case PDF::LineCapStyle::RoundCap:
Formatter<StringView>::format(builder, "LineCapStyle::RoundCap");
break;
return Formatter<StringView>::format(builder, "LineCapStyle::RoundCap");
case PDF::LineCapStyle::SquareCap:
Formatter<StringView>::format(builder, "LineCapStyle::SquareCap");
break;
return Formatter<StringView>::format(builder, "LineCapStyle::SquareCap");
}
}
};
template<>
struct Formatter<PDF::LineJoinStyle> : Formatter<StringView> {
void format(FormatBuilder& builder, PDF::LineJoinStyle const& style)
ErrorOr<void> format(FormatBuilder& builder, PDF::LineJoinStyle const& style)
{
switch (style) {
case PDF::LineJoinStyle::Miter:
Formatter<StringView>::format(builder, "LineJoinStyle::Miter");
break;
return Formatter<StringView>::format(builder, "LineJoinStyle::Miter");
case PDF::LineJoinStyle::Round:
Formatter<StringView>::format(builder, "LineJoinStyle::Round");
break;
return Formatter<StringView>::format(builder, "LineJoinStyle::Round");
case PDF::LineJoinStyle::Bevel:
Formatter<StringView>::format(builder, "LineJoinStyle::Bevel");
break;
return Formatter<StringView>::format(builder, "LineJoinStyle::Bevel");
}
}
};
template<>
struct Formatter<PDF::LineDashPattern> : Formatter<StringView> {
void format(FormatBuilder& format_builder, PDF::LineDashPattern const& pattern)
ErrorOr<void> format(FormatBuilder& format_builder, PDF::LineDashPattern const& pattern)
{
StringBuilder builder;
builder.append("[");
@ -191,40 +185,32 @@ struct Formatter<PDF::LineDashPattern> : Formatter<StringView> {
template<>
struct Formatter<PDF::TextRenderingMode> : Formatter<StringView> {
void format(FormatBuilder& builder, PDF::TextRenderingMode const& style)
ErrorOr<void> format(FormatBuilder& builder, PDF::TextRenderingMode const& style)
{
switch (style) {
case PDF::TextRenderingMode::Fill:
Formatter<StringView>::format(builder, "TextRenderingMode::Fill");
break;
return Formatter<StringView>::format(builder, "TextRenderingMode::Fill");
case PDF::TextRenderingMode::Stroke:
Formatter<StringView>::format(builder, "TextRenderingMode::Stroke");
break;
return Formatter<StringView>::format(builder, "TextRenderingMode::Stroke");
case PDF::TextRenderingMode::FillThenStroke:
Formatter<StringView>::format(builder, "TextRenderingMode::FillThenStroke");
break;
return Formatter<StringView>::format(builder, "TextRenderingMode::FillThenStroke");
case PDF::TextRenderingMode::Invisible:
Formatter<StringView>::format(builder, "TextRenderingMode::Invisible");
break;
return Formatter<StringView>::format(builder, "TextRenderingMode::Invisible");
case PDF::TextRenderingMode::FillAndClip:
Formatter<StringView>::format(builder, "TextRenderingMode::FillAndClip");
break;
return Formatter<StringView>::format(builder, "TextRenderingMode::FillAndClip");
case PDF::TextRenderingMode::StrokeAndClip:
Formatter<StringView>::format(builder, "TextRenderingMode::StrokeAndClip");
break;
return Formatter<StringView>::format(builder, "TextRenderingMode::StrokeAndClip");
case PDF::TextRenderingMode::FillStrokeAndClip:
Formatter<StringView>::format(builder, "TextRenderingMode::FillStrokeAndClip");
break;
return Formatter<StringView>::format(builder, "TextRenderingMode::FillStrokeAndClip");
case PDF::TextRenderingMode::Clip:
Formatter<StringView>::format(builder, "TextRenderingMode::Clip");
break;
return Formatter<StringView>::format(builder, "TextRenderingMode::Clip");
}
}
};
template<>
struct Formatter<PDF::TextState> : Formatter<StringView> {
void format(FormatBuilder& format_builder, PDF::TextState const& state)
ErrorOr<void> format(FormatBuilder& format_builder, PDF::TextState const& state)
{
StringBuilder builder;
builder.append("TextState {\n");
@ -239,13 +225,13 @@ struct Formatter<PDF::TextState> : Formatter<StringView> {
builder.appendff(" rise={}\n", state.rise);
builder.appendff(" knockout={}\n", state.knockout);
builder.append(" }");
Formatter<StringView>::format(format_builder, builder.to_string());
return Formatter<StringView>::format(format_builder, builder.to_string());
}
};
template<>
struct Formatter<PDF::GraphicsState> : Formatter<StringView> {
void format(FormatBuilder& format_builder, PDF::GraphicsState const& state)
ErrorOr<void> format(FormatBuilder& format_builder, PDF::GraphicsState const& state)
{
StringBuilder builder;
builder.append("GraphicsState {\n");
@ -259,7 +245,7 @@ struct Formatter<PDF::GraphicsState> : Formatter<StringView> {
builder.appendff(" line_dash_pattern={}\n", state.line_dash_pattern);
builder.appendff(" text_state={}\n", state.text_state);
builder.append("}");
Formatter<StringView>::format(format_builder, builder.to_string());
return Formatter<StringView>::format(format_builder, builder.to_string());
}
};

View file

@ -85,9 +85,9 @@ namespace AK {
template<>
struct Formatter<PDF::Value> : Formatter<StringView> {
void format(FormatBuilder& builder, PDF::Value const& value)
ErrorOr<void> format(FormatBuilder& builder, PDF::Value const& value)
{
Formatter<StringView>::format(builder, value.to_string());
return Formatter<StringView>::format(builder, value.to_string());
}
};

View file

@ -101,9 +101,9 @@ namespace AK {
template<>
struct Formatter<PDF::XRefEntry> : Formatter<StringView> {
void format(FormatBuilder& builder, PDF::XRefEntry const& entry)
ErrorOr<void> format(FormatBuilder& builder, PDF::XRefEntry const& entry)
{
Formatter<StringView>::format(builder,
return Formatter<StringView>::format(builder,
String::formatted("XRefEntry {{ offset={} generation={} used={} }}",
entry.byte_offset,
entry.generation_number,
@ -113,14 +113,14 @@ struct Formatter<PDF::XRefEntry> : Formatter<StringView> {
template<>
struct Formatter<PDF::XRefTable> : Formatter<StringView> {
void format(FormatBuilder& format_builder, PDF::XRefTable const& table)
ErrorOr<void> format(FormatBuilder& format_builder, PDF::XRefTable const& table)
{
StringBuilder builder;
builder.append("XRefTable {");
for (auto& entry : table.m_entries)
builder.appendff("\n {}", entry);
builder.append("\n}");
Formatter<StringView>::format(format_builder, builder.to_string());
return Formatter<StringView>::format(format_builder, builder.to_string());
}
};