From 107e15c5bc74dafddfcaf5ad9964eac3c76c0888 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Sun, 15 Jan 2023 00:30:51 -0500 Subject: [PATCH] LibGUI: Base `write_to_file(StringView path)` on the stream overload `write_to_file(StringView path)` was based on the `Core::File` overload. The return type also changed from `bool` to `ErrorOr` to ease error propagation. --- Userland/DevTools/HackStudio/EditorWrapper.cpp | 2 +- Userland/Libraries/LibGUI/TextEditor.cpp | 12 ++++-------- Userland/Libraries/LibGUI/TextEditor.h | 2 +- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/Userland/DevTools/HackStudio/EditorWrapper.cpp b/Userland/DevTools/HackStudio/EditorWrapper.cpp index bb407f6446..d6c91d5ad2 100644 --- a/Userland/DevTools/HackStudio/EditorWrapper.cpp +++ b/Userland/DevTools/HackStudio/EditorWrapper.cpp @@ -79,7 +79,7 @@ void EditorWrapper::save() }); file_picker_action->activate(); } - editor().write_to_file(filename()); + editor().write_to_file(filename()).release_value_but_fixme_should_propagate_errors(); update_diff(); editor().update(); } diff --git a/Userland/Libraries/LibGUI/TextEditor.cpp b/Userland/Libraries/LibGUI/TextEditor.cpp index a2324ba4e1..3d570148da 100644 --- a/Userland/Libraries/LibGUI/TextEditor.cpp +++ b/Userland/Libraries/LibGUI/TextEditor.cpp @@ -1458,15 +1458,11 @@ void TextEditor::timer_event(Core::TimerEvent&) update_cursor(); } -bool TextEditor::write_to_file(DeprecatedString const& path) +ErrorOr TextEditor::write_to_file(StringView path) { - auto file = Core::File::construct(path); - if (!file->open(Core::OpenMode::WriteOnly | Core::OpenMode::Truncate)) { - warnln("Error opening {}: {}", path, strerror(file->error())); - return false; - } - - return write_to_file(*file); + auto file = TRY(Core::Stream::File::open(path, Core::Stream::OpenMode::Write | Core::Stream::OpenMode::Truncate)); + TRY(write_to_file(*file)); + return {}; } bool TextEditor::write_to_file(Core::File& file) diff --git a/Userland/Libraries/LibGUI/TextEditor.h b/Userland/Libraries/LibGUI/TextEditor.h index 8a9773cc89..d071251ea4 100644 --- a/Userland/Libraries/LibGUI/TextEditor.h +++ b/Userland/Libraries/LibGUI/TextEditor.h @@ -129,7 +129,7 @@ public: void insert_at_cursor_or_replace_selection(StringView); void replace_all_text_without_resetting_undo_stack(StringView text); - bool write_to_file(DeprecatedString const& path); + ErrorOr write_to_file(StringView path); bool write_to_file(Core::File&); ErrorOr write_to_file(Core::Stream::File&); bool has_selection() const { return m_selection.is_valid(); }