Skip to content

Commit

Permalink
to_event_definition::from_json -> constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Dec 8, 2024
1 parent 77c590e commit 9d64c22
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 78 deletions.
18 changes: 12 additions & 6 deletions src/share/manipulator/manipulators/basic/basic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@ class basic final : public base, public pqrs::dispatcher::extra::dispatcher_clie
if (value.is_object()) {
try {
to_ = std::vector<to_event_definition>{
value.get<to_event_definition>(),
to_event_definition(value),
};
} catch (const pqrs::json::unmarshal_error& e) {
throw pqrs::json::unmarshal_error(fmt::format("`{0}` error: {1}", key, e.what()));
}

} else if (value.is_array()) {
try {
to_ = value.get<std::vector<to_event_definition>>();
for (const auto& j : value) {
to_.push_back(to_event_definition(j));
}
} catch (const pqrs::json::unmarshal_error& e) {
throw pqrs::json::unmarshal_error(fmt::format("`{0}` entry error: {1}", key, e.what()));
}
Expand All @@ -61,15 +63,17 @@ class basic final : public base, public pqrs::dispatcher::extra::dispatcher_clie
if (value.is_object()) {
try {
to_after_key_up_ = std::vector<to_event_definition>{
value.get<to_event_definition>(),
to_event_definition(value),
};
} catch (const pqrs::json::unmarshal_error& e) {
throw pqrs::json::unmarshal_error(fmt::format("`{0}` error: {1}", key, e.what()));
}

} else if (value.is_array()) {
try {
to_after_key_up_ = value.get<std::vector<to_event_definition>>();
for (const auto& j : value) {
to_after_key_up_.push_back(to_event_definition(j));
}
} catch (const pqrs::json::unmarshal_error& e) {
throw pqrs::json::unmarshal_error(fmt::format("`{0}` entry error: {1}", key, e.what()));
}
Expand All @@ -82,15 +86,17 @@ class basic final : public base, public pqrs::dispatcher::extra::dispatcher_clie
if (value.is_object()) {
try {
to_if_alone_ = std::vector<to_event_definition>{
value.get<to_event_definition>(),
to_event_definition(value),
};
} catch (const pqrs::json::unmarshal_error& e) {
throw pqrs::json::unmarshal_error(fmt::format("`{0}` error: {1}", key, e.what()));
}

} else if (value.is_array()) {
try {
to_if_alone_ = value.get<std::vector<to_event_definition>>();
for (const auto& j : value) {
to_if_alone_.push_back(to_event_definition(j));
}
} catch (const pqrs::json::unmarshal_error& e) {
throw pqrs::json::unmarshal_error(fmt::format("`{0}` entry error: {1}", key, e.what()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,17 @@ class simultaneous_options final {
if (value.is_object()) {
try {
to_after_key_up_ = std::vector<to_event_definition>{
value.get<to_event_definition>(),
to_event_definition(value),
};
} catch (const pqrs::json::unmarshal_error& e) {
throw pqrs::json::unmarshal_error(fmt::format("`{0}` error: {1}", key, e.what()));
}

} else if (value.is_array()) {
try {
to_after_key_up_ = value.get<std::vector<to_event_definition>>();
for (const auto& j : value) {
to_after_key_up_.push_back(to_event_definition(j));
}
} catch (const pqrs::json::unmarshal_error& e) {
throw pqrs::json::unmarshal_error(fmt::format("`{0}` entry error: {1}", key, e.what()));
}
Expand Down
12 changes: 8 additions & 4 deletions src/share/manipulator/manipulators/basic/to_delayed_action.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ class to_delayed_action final : public pqrs::dispatcher::extra::dispatcher_clien
if (value.is_object()) {
try {
to_if_invoked_ = std::vector<to_event_definition>{
value.get<to_event_definition>(),
to_event_definition(value),
};
} catch (const pqrs::json::unmarshal_error& e) {
throw pqrs::json::unmarshal_error(fmt::format("`{0}` error: {1}", key, e.what()));
}

} else if (value.is_array()) {
try {
to_if_invoked_ = value.get<std::vector<to_event_definition>>();
for (const auto& j : value) {
to_if_invoked_.push_back(to_event_definition(j));
}
} catch (const pqrs::json::unmarshal_error& e) {
throw pqrs::json::unmarshal_error(fmt::format("`{0}` entry error: {1}", key, e.what()));
}
Expand All @@ -42,15 +44,17 @@ class to_delayed_action final : public pqrs::dispatcher::extra::dispatcher_clien
if (value.is_object()) {
try {
to_if_canceled_ = std::vector<to_event_definition>{
value.get<to_event_definition>(),
to_event_definition(value),
};
} catch (const pqrs::json::unmarshal_error& e) {
throw pqrs::json::unmarshal_error(fmt::format("`{0}` error: {1}", key, e.what()));
}

} else if (value.is_array()) {
try {
to_if_canceled_ = value.get<std::vector<to_event_definition>>();
for (const auto& j : value) {
to_if_canceled_.push_back(to_event_definition(j));
}
} catch (const pqrs::json::unmarshal_error& e) {
throw pqrs::json::unmarshal_error(fmt::format("`{0}` entry error: {1}", key, e.what()));
}
Expand Down
6 changes: 4 additions & 2 deletions src/share/manipulator/manipulators/basic/to_if_held_down.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ class to_if_held_down final : public pqrs::dispatcher::extra::dispatcher_client
try {
if (json.is_object()) {
to_ = std::vector<to_event_definition>{
json.get<to_event_definition>(),
to_event_definition(json),
};

} else if (json.is_array()) {
to_ = json.get<std::vector<to_event_definition>>();
for (const auto& j : json) {
to_.push_back(to_event_definition(j));
}

} else {
throw pqrs::json::unmarshal_error(fmt::format("json must be object or array, but is `{0}`", pqrs::json::dump_for_error_message(json)));
Expand Down
128 changes: 65 additions & 63 deletions src/share/manipulator/types/to_event_definition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,71 @@ namespace krbn {
namespace manipulator {
class to_event_definition final {
public:
to_event_definition(void) : lazy_(false),
repeat_(true),
halt_(false),
hold_down_milliseconds_(0) {
to_event_definition(void)
: lazy_(false),
repeat_(true),
halt_(false),
hold_down_milliseconds_(0) {
}

to_event_definition(const nlohmann::json& json)
: to_event_definition() {
pqrs::json::requires_object(json, "json");

for (const auto& [key, value] : json.items()) {
if (event_definition_.handle_json(key, value, json)) {
// Do nothing

} else if (key == "modifiers") {
try {
modifiers_ = modifier_definition::make_modifiers(value);
} catch (const pqrs::json::unmarshal_error& e) {
throw pqrs::json::unmarshal_error(fmt::format("`{0}` error: {1}", key, e.what()));
}

} else if (key == "lazy") {
pqrs::json::requires_boolean(value, "`" + key + "`");

lazy_ = value.get<bool>();

} else if (key == "repeat") {
pqrs::json::requires_boolean(value, "`" + key + "`");

repeat_ = value.get<bool>();

} else if (key == "halt") {
pqrs::json::requires_boolean(value, "`" + key + "`");

halt_ = value.get<bool>();

} else if (key == "hold_down_milliseconds" ||
key == "held_down_milliseconds") {
pqrs::json::requires_number(value, "`" + key + "`");

hold_down_milliseconds_ = std::chrono::milliseconds(value.get<int>());

} else {
throw pqrs::json::unmarshal_error(fmt::format("unknown key `{0}` in `{1}`", key, pqrs::json::dump_for_error_message(json)));
}
}

// ----------------------------------------

switch (event_definition_.get_type()) {
case event_definition::type::momentary_switch_event:
case event_definition::type::shell_command:
case event_definition::type::select_input_source:
case event_definition::type::set_variable:
case event_definition::type::set_notification_message:
case event_definition::type::mouse_key:
case event_definition::type::sticky_modifier:
case event_definition::type::software_function:
break;

case event_definition::type::none:
case event_definition::type::any:
throw pqrs::json::unmarshal_error(fmt::format("event type is invalid: `{0}`", pqrs::json::dump_for_error_message(json)));
}
}

virtual ~to_event_definition(void) {
Expand Down Expand Up @@ -110,64 +171,5 @@ class to_event_definition final {
bool halt_;
std::chrono::milliseconds hold_down_milliseconds_;
};

inline void from_json(const nlohmann::json& json, to_event_definition& d) {
pqrs::json::requires_object(json, "json");

for (const auto& [key, value] : json.items()) {
if (d.get_event_definition().handle_json(key, value, json)) {
// Do nothing

} else if (key == "modifiers") {
try {
d.set_modifiers(modifier_definition::make_modifiers(value));
} catch (const pqrs::json::unmarshal_error& e) {
throw pqrs::json::unmarshal_error(fmt::format("`{0}` error: {1}", key, e.what()));
}

} else if (key == "lazy") {
pqrs::json::requires_boolean(value, "`" + key + "`");

d.set_lazy(value.get<bool>());

} else if (key == "repeat") {
pqrs::json::requires_boolean(value, "`" + key + "`");

d.set_repeat(value.get<bool>());

} else if (key == "halt") {
pqrs::json::requires_boolean(value, "`" + key + "`");

d.set_halt(value.get<bool>());

} else if (key == "hold_down_milliseconds" ||
key == "held_down_milliseconds") {
pqrs::json::requires_number(value, "`" + key + "`");

d.set_hold_down_milliseconds(std::chrono::milliseconds(value.get<int>()));

} else {
throw pqrs::json::unmarshal_error(fmt::format("unknown key `{0}` in `{1}`", key, pqrs::json::dump_for_error_message(json)));
}
}

// ----------------------------------------

switch (d.get_event_definition().get_type()) {
case event_definition::type::momentary_switch_event:
case event_definition::type::shell_command:
case event_definition::type::select_input_source:
case event_definition::type::set_variable:
case event_definition::type::set_notification_message:
case event_definition::type::mouse_key:
case event_definition::type::sticky_modifier:
case event_definition::type::software_function:
break;

case event_definition::type::none:
case event_definition::type::any:
throw pqrs::json::unmarshal_error(fmt::format("event type is invalid: `{0}`", pqrs::json::dump_for_error_message(json)));
}
}
} // namespace manipulator
} // namespace krbn
2 changes: 1 addition & 1 deletion tests/src/manipulator_types/src/errors_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void handle_json(const nlohmann::json& json) {
} else if (c == "modifier_definition") {
krbn::manipulator::modifier_definition::make_modifiers(json.at("input"));
} else if (c == "to_event_definition") {
json.at("input").get<krbn::manipulator::to_event_definition>();
krbn::manipulator::to_event_definition(json.at("input"));
} else {
expect(false);
}
Expand Down

0 comments on commit 9d64c22

Please sign in to comment.