1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:18:11 +00:00

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.
This commit is contained in:
RasmusNylander 2021-12-16 17:46:49 +01:00 committed by Andreas Kling
parent 017135b44e
commit 4e65c4dae4
5 changed files with 10 additions and 22 deletions

View file

@ -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");