diff options
author | Andreas Kling <kling@serenityos.org> | 2021-11-16 01:15:21 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-11-17 00:21:13 +0100 |
commit | 216e21a1fa95ffc87c371cdd6262467df8cdb5cd (patch) | |
tree | d58f31c31215cce39557cfcc3f35b7795334aa06 /Userland/Libraries/LibPDF | |
parent | 008355c222980bd3520caeea9848e04dc2230df1 (diff) | |
download | serenity-216e21a1fa95ffc87c371cdd6262467df8cdb5cd.zip |
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.
Diffstat (limited to 'Userland/Libraries/LibPDF')
-rw-r--r-- | Userland/Libraries/LibPDF/Command.h | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibPDF/Document.h | 20 | ||||
-rw-r--r-- | Userland/Libraries/LibPDF/Object.h | 8 | ||||
-rw-r--r-- | Userland/Libraries/LibPDF/Parser.cpp | 12 | ||||
-rw-r--r-- | Userland/Libraries/LibPDF/Renderer.h | 58 | ||||
-rw-r--r-- | Userland/Libraries/LibPDF/Value.h | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibPDF/XRefTable.h | 8 |
7 files changed, 50 insertions, 64 deletions
diff --git a/Userland/Libraries/LibPDF/Command.h b/Userland/Libraries/LibPDF/Command.h index 01f69aedc0..3f9572c742 100644 --- a/Userland/Libraries/LibPDF/Command.h +++ b/Userland/Libraries/LibPDF/Command.h @@ -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()); } }; diff --git a/Userland/Libraries/LibPDF/Document.h b/Userland/Libraries/LibPDF/Document.h index ddd26443cd..02303cee02 100644 --- a/Userland/Libraries/LibPDF/Document.h +++ b/Userland/Libraries/LibPDF/Document.h @@ -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())); } }; diff --git a/Userland/Libraries/LibPDF/Object.h b/Userland/Libraries/LibPDF/Object.h index f3d0fea884..5774434ef0 100644 --- a/Userland/Libraries/LibPDF/Object.h +++ b/Userland/Libraries/LibPDF/Object.h @@ -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); } }; diff --git a/Userland/Libraries/LibPDF/Parser.cpp b/Userland/Libraries/LibPDF/Parser.cpp index 105b5d7f3b..744a88521a 100644 --- a/Userland/Libraries/LibPDF/Parser.cpp +++ b/Userland/Libraries/LibPDF/Parser.cpp @@ -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()); } }; diff --git a/Userland/Libraries/LibPDF/Renderer.h b/Userland/Libraries/LibPDF/Renderer.h index b2daebc19a..eb8e0f2b63 100644 --- a/Userland/Libraries/LibPDF/Renderer.h +++ b/Userland/Libraries/LibPDF/Renderer.h @@ -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()); } }; diff --git a/Userland/Libraries/LibPDF/Value.h b/Userland/Libraries/LibPDF/Value.h index 498fb20d1d..efce1a30b0 100644 --- a/Userland/Libraries/LibPDF/Value.h +++ b/Userland/Libraries/LibPDF/Value.h @@ -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()); } }; diff --git a/Userland/Libraries/LibPDF/XRefTable.h b/Userland/Libraries/LibPDF/XRefTable.h index 5ac0cd355f..efb87f2314 100644 --- a/Userland/Libraries/LibPDF/XRefTable.h +++ b/Userland/Libraries/LibPDF/XRefTable.h @@ -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()); } }; |