mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 16:37:35 +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:
parent
017135b44e
commit
4e65c4dae4
5 changed files with 10 additions and 22 deletions
|
@ -17,13 +17,11 @@ namespace Keyboard {
|
||||||
#ifndef KERNEL
|
#ifndef KERNEL
|
||||||
// The Kernel explicitly and exclusively links only this file into it.
|
// 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`.
|
// 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);
|
auto result = TRY(CharacterMapFile::load_from_file(map_name));
|
||||||
if (!result.has_value())
|
|
||||||
return {};
|
|
||||||
|
|
||||||
return CharacterMap(map_name, result.value());
|
return CharacterMap(map_name, result);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ class CharacterMap {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CharacterMap(const String& map_name, const CharacterMapData& map_data);
|
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
|
#ifndef KERNEL
|
||||||
int set_system_map();
|
int set_system_map();
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
|
|
||||||
namespace Keyboard {
|
namespace Keyboard {
|
||||||
|
|
||||||
Optional<CharacterMapData> CharacterMapFile::load_from_file(const String& filename)
|
ErrorOr<CharacterMapData> CharacterMapFile::load_from_file(const String& filename)
|
||||||
{
|
{
|
||||||
auto path = filename;
|
auto path = filename;
|
||||||
if (!path.ends_with(".json")) {
|
if (!path.ends_with(".json")) {
|
||||||
|
@ -22,20 +22,10 @@ Optional<CharacterMapData> CharacterMapFile::load_from_file(const String& filena
|
||||||
path = full_path.to_string();
|
path = full_path.to_string();
|
||||||
}
|
}
|
||||||
|
|
||||||
auto file = Core::File::construct(path);
|
auto file = TRY(Core::File::open(path, Core::OpenMode::ReadOnly));
|
||||||
file->open(Core::OpenMode::ReadOnly);
|
|
||||||
if (!file->is_open()) {
|
|
||||||
dbgln("Failed to open {}: {}", path, file->error_string());
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
auto file_contents = file->read_all();
|
auto file_contents = file->read_all();
|
||||||
auto json_result = JsonValue::from_string(file_contents);
|
auto json_result = TRY(JsonValue::from_string(file_contents));
|
||||||
if (json_result.is_error()) {
|
const auto& json = json_result.as_object();
|
||||||
dbgln("Failed to load character map from file {}", path);
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
auto json = json_result.value().as_object();
|
|
||||||
|
|
||||||
Vector<u32> map = read_map(json, "map");
|
Vector<u32> map = read_map(json, "map");
|
||||||
Vector<u32> shift_map = read_map(json, "shift_map");
|
Vector<u32> shift_map = read_map(json, "shift_map");
|
||||||
|
|
|
@ -14,7 +14,7 @@ namespace Keyboard {
|
||||||
class CharacterMapFile {
|
class CharacterMapFile {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static Optional<CharacterMapData> load_from_file(const String& filename);
|
static ErrorOr<CharacterMapData> load_from_file(const String& filename);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static Vector<u32> read_map(const JsonObject& json, const String& name);
|
static Vector<u32> read_map(const JsonObject& json, const String& name);
|
||||||
|
|
|
@ -34,7 +34,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
}
|
}
|
||||||
|
|
||||||
auto character_map = Keyboard::CharacterMap::load_from_file(path);
|
auto character_map = Keyboard::CharacterMap::load_from_file(path);
|
||||||
if (!character_map.has_value()) {
|
if (character_map.is_error()) {
|
||||||
warnln("Cannot read keymap {}", path);
|
warnln("Cannot read keymap {}", path);
|
||||||
warnln("Hint: Must be either a keymap name (e.g. 'en-us') or a full path.");
|
warnln("Hint: Must be either a keymap name (e.g. 'en-us') or a full path.");
|
||||||
return 1;
|
return 1;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue