Spreadsheet: Port from Result to ErrorOr

This commit is contained in:
Lucas CHOLLET 2023-01-14 18:25:51 -05:00 committed by Ali Mohammad Pur
parent 4952cdfe2b
commit 81008062a7
Notes: sideshowbarker 2024-07-17 22:01:16 +09:00
4 changed files with 15 additions and 27 deletions

View File

@ -175,13 +175,13 @@ void CSVImportDialogPage::update_preview()
m_data_preview_table_view->update();
}
Result<NonnullRefPtrVector<Sheet>, DeprecatedString> ImportDialog::make_and_run_for(GUI::Window& parent, StringView mime, Core::File& file, Workbook& workbook)
ErrorOr<NonnullRefPtrVector<Sheet>, 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<NonnullRefPtrVector<Sheet>, DeprecatedString> {
auto import_xsv = [&]() -> ErrorOr<NonnullRefPtrVector<Sheet>, DeprecatedString> {
auto contents = file.read_all();
CSVImportDialogPage page { contents };
wizard->replace_page(page.page());
@ -203,29 +203,19 @@ Result<NonnullRefPtrVector<Sheet>, DeprecatedString> ImportDialog::make_and_run_
}
return sheets;
} else {
return DeprecatedString { "CSV Import was cancelled" };
}
return DeprecatedString { "CSV Import was cancelled" };
};
auto import_worksheet = [&]() -> Result<NonnullRefPtrVector<Sheet>, DeprecatedString> {
auto import_worksheet = [&]() -> ErrorOr<NonnullRefPtrVector<Sheet>, 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<Sheet> sheets;

View File

@ -7,7 +7,6 @@
#pragma once
#include "Readers/XSV.h"
#include <AK/Result.h>
#include <AK/StringView.h>
#include <LibGUI/Forward.h>
#include <LibGUI/Wizards/WizardPage.h>
@ -56,7 +55,7 @@ private:
};
struct ImportDialog {
static Result<NonnullRefPtrVector<Sheet>, DeprecatedString> make_and_run_for(GUI::Window& parent, StringView mime, Core::File& file, Workbook&);
static ErrorOr<NonnullRefPtrVector<Sheet>, DeprecatedString> make_and_run_for(GUI::Window& parent, StringView mime, Core::File& file, Workbook&);
};
}

View File

@ -52,7 +52,7 @@ bool Workbook::set_filename(DeprecatedString const& filename)
return true;
}
Result<bool, DeprecatedString> Workbook::open_file(Core::File& file)
ErrorOr<void, DeprecatedString> Workbook::open_file(Core::File& file)
{
auto mime = Core::guess_mime_type_based_on_filename(file.filename());
@ -61,7 +61,7 @@ Result<bool, DeprecatedString> Workbook::open_file(Core::File& file)
set_filename(file.filename());
return true;
return {};
}
ErrorOr<void> Workbook::write_to_file(Core::File& file)
@ -78,7 +78,7 @@ ErrorOr<void> Workbook::write_to_file(Core::File& file)
return {};
}
Result<bool, DeprecatedString> Workbook::import_file(Core::File& file)
ErrorOr<bool, DeprecatedString> Workbook::import_file(Core::File& file)
{
auto mime = Core::guess_mime_type_based_on_filename(file.filename());

View File

@ -9,7 +9,6 @@
#include "Forward.h"
#include "Spreadsheet.h"
#include <AK/NonnullOwnPtrVector.h>
#include <AK/Result.h>
namespace Spreadsheet {
@ -17,10 +16,10 @@ class Workbook {
public:
Workbook(NonnullRefPtrVector<Sheet>&& sheets, GUI::Window& parent_window);
Result<bool, DeprecatedString> open_file(Core::File&);
ErrorOr<void, DeprecatedString> open_file(Core::File&);
ErrorOr<void> write_to_file(Core::File&);
Result<bool, DeprecatedString> import_file(Core::File&);
ErrorOr<bool, DeprecatedString> import_file(Core::File&);
DeprecatedString const& current_filename() const { return m_current_filename; }
bool set_filename(DeprecatedString const& filename);