diff options
author | RasmusNylander <43042651+rasmusnylander@users.noreply.github.com> | 2021-12-16 17:46:49 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-01-06 17:54:03 +0100 |
commit | 4e65c4dae4d79d533fa1a3622544a9a0eaeb8c88 (patch) | |
tree | edd004c19854b927b891d84fc15bfa13413953e0 /Userland/Libraries/LibKeyboard | |
parent | 017135b44e91b0d9527e8f56212647c49a6faaf7 (diff) | |
download | serenity-4e65c4dae4d79d533fa1a3622544a9a0eaeb8c88.zip |
LibKeyboard: Change some Optional<T> returns to ErrorOr<T>
Makes CharacterMapFile::load_from_file and CharacterMap::load_from_file
return ErrorOr instead of Optional. This makes them a little nicer to
use and a little easier to read, as they seem to have been approximating
this.
Diffstat (limited to 'Userland/Libraries/LibKeyboard')
-rw-r--r-- | Userland/Libraries/LibKeyboard/CharacterMap.cpp | 8 | ||||
-rw-r--r-- | Userland/Libraries/LibKeyboard/CharacterMap.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibKeyboard/CharacterMapFile.cpp | 18 | ||||
-rw-r--r-- | Userland/Libraries/LibKeyboard/CharacterMapFile.h | 2 |
4 files changed, 9 insertions, 21 deletions
diff --git a/Userland/Libraries/LibKeyboard/CharacterMap.cpp b/Userland/Libraries/LibKeyboard/CharacterMap.cpp index d5b76c710b..4d661f15bd 100644 --- a/Userland/Libraries/LibKeyboard/CharacterMap.cpp +++ b/Userland/Libraries/LibKeyboard/CharacterMap.cpp @@ -17,13 +17,11 @@ namespace Keyboard { #ifndef KERNEL // The Kernel explicitly and exclusively links only this file into it. // Thus, we cannot even include a reference to the symbol `CharacterMapFile::load_from_file`. -Optional<CharacterMap> CharacterMap::load_from_file(const String& map_name) +ErrorOr<CharacterMap> CharacterMap::load_from_file(const String& map_name) { - auto result = CharacterMapFile::load_from_file(map_name); - if (!result.has_value()) - return {}; + auto result = TRY(CharacterMapFile::load_from_file(map_name)); - return CharacterMap(map_name, result.value()); + return CharacterMap(map_name, result); } #endif diff --git a/Userland/Libraries/LibKeyboard/CharacterMap.h b/Userland/Libraries/LibKeyboard/CharacterMap.h index b1338953f1..b4d00477db 100644 --- a/Userland/Libraries/LibKeyboard/CharacterMap.h +++ b/Userland/Libraries/LibKeyboard/CharacterMap.h @@ -19,7 +19,7 @@ class CharacterMap { public: CharacterMap(const String& map_name, const CharacterMapData& map_data); - static Optional<CharacterMap> load_from_file(const String& filename); + static ErrorOr<CharacterMap> load_from_file(const String& filename); #ifndef KERNEL int set_system_map(); diff --git a/Userland/Libraries/LibKeyboard/CharacterMapFile.cpp b/Userland/Libraries/LibKeyboard/CharacterMapFile.cpp index 88f2df9b41..233e94e793 100644 --- a/Userland/Libraries/LibKeyboard/CharacterMapFile.cpp +++ b/Userland/Libraries/LibKeyboard/CharacterMapFile.cpp @@ -11,7 +11,7 @@ namespace Keyboard { -Optional<CharacterMapData> CharacterMapFile::load_from_file(const String& filename) +ErrorOr<CharacterMapData> CharacterMapFile::load_from_file(const String& filename) { auto path = filename; if (!path.ends_with(".json")) { @@ -22,20 +22,10 @@ Optional<CharacterMapData> CharacterMapFile::load_from_file(const String& filena path = full_path.to_string(); } - auto file = Core::File::construct(path); - file->open(Core::OpenMode::ReadOnly); - if (!file->is_open()) { - dbgln("Failed to open {}: {}", path, file->error_string()); - return {}; - } - + auto file = TRY(Core::File::open(path, Core::OpenMode::ReadOnly)); auto file_contents = file->read_all(); - auto json_result = JsonValue::from_string(file_contents); - if (json_result.is_error()) { - dbgln("Failed to load character map from file {}", path); - return {}; - } - auto json = json_result.value().as_object(); + auto json_result = TRY(JsonValue::from_string(file_contents)); + const auto& json = json_result.as_object(); Vector<u32> map = read_map(json, "map"); Vector<u32> shift_map = read_map(json, "shift_map"); diff --git a/Userland/Libraries/LibKeyboard/CharacterMapFile.h b/Userland/Libraries/LibKeyboard/CharacterMapFile.h index c11800d612..986278f71a 100644 --- a/Userland/Libraries/LibKeyboard/CharacterMapFile.h +++ b/Userland/Libraries/LibKeyboard/CharacterMapFile.h @@ -14,7 +14,7 @@ namespace Keyboard { class CharacterMapFile { public: - static Optional<CharacterMapData> load_from_file(const String& filename); + static ErrorOr<CharacterMapData> load_from_file(const String& filename); private: static Vector<u32> read_map(const JsonObject& json, const String& name); |