From 81008062a7cab4cd50308011d60bf13cc4913907 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Sat, 14 Jan 2023 18:25:51 -0500 Subject: [PATCH] Spreadsheet: Port from `Result` to `ErrorOr` --- .../Applications/Spreadsheet/ImportDialog.cpp | 28 ++++++------------- .../Applications/Spreadsheet/ImportDialog.h | 3 +- .../Applications/Spreadsheet/Workbook.cpp | 6 ++-- Userland/Applications/Spreadsheet/Workbook.h | 5 ++-- 4 files changed, 15 insertions(+), 27 deletions(-) diff --git a/Userland/Applications/Spreadsheet/ImportDialog.cpp b/Userland/Applications/Spreadsheet/ImportDialog.cpp index b8ca906983..65197a6cd5 100644 --- a/Userland/Applications/Spreadsheet/ImportDialog.cpp +++ b/Userland/Applications/Spreadsheet/ImportDialog.cpp @@ -175,13 +175,13 @@ void CSVImportDialogPage::update_preview() m_data_preview_table_view->update(); } -Result, DeprecatedString> ImportDialog::make_and_run_for(GUI::Window& parent, StringView mime, Core::File& file, Workbook& workbook) +ErrorOr, DeprecatedString> ImportDialog::make_and_run_for(GUI::Window& parent, StringView mime, Core::File& file, Workbook& workbook) { auto wizard = GUI::WizardDialog::construct(&parent); wizard->set_title("File Import Wizard"); wizard->set_icon(GUI::Icon::default_icon("app-spreadsheet"sv).bitmap_for_size(16)); - auto import_xsv = [&]() -> Result, DeprecatedString> { + auto import_xsv = [&]() -> ErrorOr, DeprecatedString> { auto contents = file.read_all(); CSVImportDialogPage page { contents }; wizard->replace_page(page.page()); @@ -203,29 +203,19 @@ Result, DeprecatedString> ImportDialog::make_and_run_ } return sheets; - } else { - return DeprecatedString { "CSV Import was cancelled" }; } + + return DeprecatedString { "CSV Import was cancelled" }; }; - auto import_worksheet = [&]() -> Result, DeprecatedString> { + auto import_worksheet = [&]() -> ErrorOr, DeprecatedString> { auto json_value_option = JsonParser(file.read_all()).parse(); - if (json_value_option.is_error()) { - StringBuilder sb; - sb.append("Failed to parse "sv); - sb.append(file.filename()); - - return sb.to_deprecated_string(); - } + if (json_value_option.is_error()) + return DeprecatedString::formatted("Failed to parse {}", file.filename()); auto& json_value = json_value_option.value(); - if (!json_value.is_array()) { - StringBuilder sb; - sb.append("Did not find a spreadsheet in "sv); - sb.append(file.filename()); - - return sb.to_deprecated_string(); - } + if (!json_value.is_array()) + return DeprecatedString::formatted("Did not find a spreadsheet in {}", file.filename()); NonnullRefPtrVector sheets; diff --git a/Userland/Applications/Spreadsheet/ImportDialog.h b/Userland/Applications/Spreadsheet/ImportDialog.h index 4d92220712..bbf7f79529 100644 --- a/Userland/Applications/Spreadsheet/ImportDialog.h +++ b/Userland/Applications/Spreadsheet/ImportDialog.h @@ -7,7 +7,6 @@ #pragma once #include "Readers/XSV.h" -#include #include #include #include @@ -56,7 +55,7 @@ private: }; struct ImportDialog { - static Result, DeprecatedString> make_and_run_for(GUI::Window& parent, StringView mime, Core::File& file, Workbook&); + static ErrorOr, DeprecatedString> make_and_run_for(GUI::Window& parent, StringView mime, Core::File& file, Workbook&); }; } diff --git a/Userland/Applications/Spreadsheet/Workbook.cpp b/Userland/Applications/Spreadsheet/Workbook.cpp index af2da6a599..751d4235fb 100644 --- a/Userland/Applications/Spreadsheet/Workbook.cpp +++ b/Userland/Applications/Spreadsheet/Workbook.cpp @@ -52,7 +52,7 @@ bool Workbook::set_filename(DeprecatedString const& filename) return true; } -Result Workbook::open_file(Core::File& file) +ErrorOr Workbook::open_file(Core::File& file) { auto mime = Core::guess_mime_type_based_on_filename(file.filename()); @@ -61,7 +61,7 @@ Result Workbook::open_file(Core::File& file) set_filename(file.filename()); - return true; + return {}; } ErrorOr Workbook::write_to_file(Core::File& file) @@ -78,7 +78,7 @@ ErrorOr Workbook::write_to_file(Core::File& file) return {}; } -Result Workbook::import_file(Core::File& file) +ErrorOr Workbook::import_file(Core::File& file) { auto mime = Core::guess_mime_type_based_on_filename(file.filename()); diff --git a/Userland/Applications/Spreadsheet/Workbook.h b/Userland/Applications/Spreadsheet/Workbook.h index 6529b064f7..007cf10ebb 100644 --- a/Userland/Applications/Spreadsheet/Workbook.h +++ b/Userland/Applications/Spreadsheet/Workbook.h @@ -9,7 +9,6 @@ #include "Forward.h" #include "Spreadsheet.h" #include -#include namespace Spreadsheet { @@ -17,10 +16,10 @@ class Workbook { public: Workbook(NonnullRefPtrVector&& sheets, GUI::Window& parent_window); - Result open_file(Core::File&); + ErrorOr open_file(Core::File&); ErrorOr write_to_file(Core::File&); - Result import_file(Core::File&); + ErrorOr import_file(Core::File&); DeprecatedString const& current_filename() const { return m_current_filename; } bool set_filename(DeprecatedString const& filename);