Skip to content

Commit

Permalink
refactor: Factor out union pack switch from event packer.
Browse files Browse the repository at this point in the history
Preparation for it being generated.
  • Loading branch information
iphydf committed Jan 15, 2024
1 parent 6caa7ce commit afc4724
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 47 deletions.
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/docker/tox-bootstrapd.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ec228c5b76d06c25dc2a8dc2ff4ccc1e8748d0915058a7cb56de75d948051001 /usr/local/bin/tox-bootstrapd
50123098ff16637a19253e4ea24c0b7bc7d1ef35f6e7c9f2783bddd303a6c6f3 /usr/local/bin/tox-bootstrapd
102 changes: 56 additions & 46 deletions toxcore/tox_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -564,131 +564,132 @@ void tox_event_destruct(Tox_Event *event, const Memory *mem)
event->data.value = nullptr;
}

bool tox_event_pack(const Tox_Event *event, Bin_Pack *bp)
non_null()
static bool tox_event_type_pack(Tox_Event_Type type, Bin_Pack *bp)
{
assert(event->type != TOX_EVENT_INVALID);

if (!(bin_pack_array(bp, 2) && bin_pack_u32(bp, event->type))) {
return false;
}
return bin_pack_u32(bp, (uint32_t)type);
}

switch (event->type) {
non_null()
static bool tox_event_data_pack(Tox_Event_Type type, const Tox_Event_Data *data, Bin_Pack *bp)
{
switch (type) {
case TOX_EVENT_CONFERENCE_CONNECTED:
return tox_event_conference_connected_pack(event->data.conference_connected, bp);
return tox_event_conference_connected_pack(data->conference_connected, bp);

case TOX_EVENT_CONFERENCE_INVITE:
return tox_event_conference_invite_pack(event->data.conference_invite, bp);
return tox_event_conference_invite_pack(data->conference_invite, bp);

case TOX_EVENT_CONFERENCE_MESSAGE:
return tox_event_conference_message_pack(event->data.conference_message, bp);
return tox_event_conference_message_pack(data->conference_message, bp);

case TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED:
return tox_event_conference_peer_list_changed_pack(event->data.conference_peer_list_changed, bp);
return tox_event_conference_peer_list_changed_pack(data->conference_peer_list_changed, bp);

case TOX_EVENT_CONFERENCE_PEER_NAME:
return tox_event_conference_peer_name_pack(event->data.conference_peer_name, bp);
return tox_event_conference_peer_name_pack(data->conference_peer_name, bp);

case TOX_EVENT_CONFERENCE_TITLE:
return tox_event_conference_title_pack(event->data.conference_title, bp);
return tox_event_conference_title_pack(data->conference_title, bp);

case TOX_EVENT_FILE_CHUNK_REQUEST:
return tox_event_file_chunk_request_pack(event->data.file_chunk_request, bp);
return tox_event_file_chunk_request_pack(data->file_chunk_request, bp);

case TOX_EVENT_FILE_RECV_CHUNK:
return tox_event_file_recv_chunk_pack(event->data.file_recv_chunk, bp);
return tox_event_file_recv_chunk_pack(data->file_recv_chunk, bp);

case TOX_EVENT_FILE_RECV_CONTROL:
return tox_event_file_recv_control_pack(event->data.file_recv_control, bp);
return tox_event_file_recv_control_pack(data->file_recv_control, bp);

case TOX_EVENT_FILE_RECV:
return tox_event_file_recv_pack(event->data.file_recv, bp);
return tox_event_file_recv_pack(data->file_recv, bp);

Check warning on line 605 in toxcore/tox_event.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_event.c#L605

Added line #L605 was not covered by tests

case TOX_EVENT_FRIEND_CONNECTION_STATUS:
return tox_event_friend_connection_status_pack(event->data.friend_connection_status, bp);
return tox_event_friend_connection_status_pack(data->friend_connection_status, bp);

case TOX_EVENT_FRIEND_LOSSLESS_PACKET:
return tox_event_friend_lossless_packet_pack(event->data.friend_lossless_packet, bp);
return tox_event_friend_lossless_packet_pack(data->friend_lossless_packet, bp);

case TOX_EVENT_FRIEND_LOSSY_PACKET:
return tox_event_friend_lossy_packet_pack(event->data.friend_lossy_packet, bp);
return tox_event_friend_lossy_packet_pack(data->friend_lossy_packet, bp);

case TOX_EVENT_FRIEND_MESSAGE:
return tox_event_friend_message_pack(event->data.friend_message, bp);
return tox_event_friend_message_pack(data->friend_message, bp);

case TOX_EVENT_FRIEND_NAME:
return tox_event_friend_name_pack(event->data.friend_name, bp);
return tox_event_friend_name_pack(data->friend_name, bp);

case TOX_EVENT_FRIEND_READ_RECEIPT:
return tox_event_friend_read_receipt_pack(event->data.friend_read_receipt, bp);
return tox_event_friend_read_receipt_pack(data->friend_read_receipt, bp);

case TOX_EVENT_FRIEND_REQUEST:
return tox_event_friend_request_pack(event->data.friend_request, bp);
return tox_event_friend_request_pack(data->friend_request, bp);

case TOX_EVENT_FRIEND_STATUS:
return tox_event_friend_status_pack(event->data.friend_status, bp);
return tox_event_friend_status_pack(data->friend_status, bp);

case TOX_EVENT_FRIEND_STATUS_MESSAGE:
return tox_event_friend_status_message_pack(event->data.friend_status_message, bp);
return tox_event_friend_status_message_pack(data->friend_status_message, bp);

case TOX_EVENT_FRIEND_TYPING:
return tox_event_friend_typing_pack(event->data.friend_typing, bp);
return tox_event_friend_typing_pack(data->friend_typing, bp);

case TOX_EVENT_SELF_CONNECTION_STATUS:
return tox_event_self_connection_status_pack(event->data.self_connection_status, bp);
return tox_event_self_connection_status_pack(data->self_connection_status, bp);

case TOX_EVENT_GROUP_PEER_NAME:
return tox_event_group_peer_name_pack(event->data.group_peer_name, bp);
return tox_event_group_peer_name_pack(data->group_peer_name, bp);

Check warning on line 641 in toxcore/tox_event.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_event.c#L641

Added line #L641 was not covered by tests

case TOX_EVENT_GROUP_PEER_STATUS:
return tox_event_group_peer_status_pack(event->data.group_peer_status, bp);
return tox_event_group_peer_status_pack(data->group_peer_status, bp);

Check warning on line 644 in toxcore/tox_event.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_event.c#L644

Added line #L644 was not covered by tests

case TOX_EVENT_GROUP_TOPIC:
return tox_event_group_topic_pack(event->data.group_topic, bp);
return tox_event_group_topic_pack(data->group_topic, bp);

Check warning on line 647 in toxcore/tox_event.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_event.c#L647

Added line #L647 was not covered by tests

case TOX_EVENT_GROUP_PRIVACY_STATE:
return tox_event_group_privacy_state_pack(event->data.group_privacy_state, bp);
return tox_event_group_privacy_state_pack(data->group_privacy_state, bp);

Check warning on line 650 in toxcore/tox_event.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_event.c#L650

Added line #L650 was not covered by tests

case TOX_EVENT_GROUP_VOICE_STATE:
return tox_event_group_voice_state_pack(event->data.group_voice_state, bp);
return tox_event_group_voice_state_pack(data->group_voice_state, bp);

Check warning on line 653 in toxcore/tox_event.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_event.c#L653

Added line #L653 was not covered by tests

case TOX_EVENT_GROUP_TOPIC_LOCK:
return tox_event_group_topic_lock_pack(event->data.group_topic_lock, bp);
return tox_event_group_topic_lock_pack(data->group_topic_lock, bp);

Check warning on line 656 in toxcore/tox_event.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_event.c#L656

Added line #L656 was not covered by tests

case TOX_EVENT_GROUP_PEER_LIMIT:
return tox_event_group_peer_limit_pack(event->data.group_peer_limit, bp);
return tox_event_group_peer_limit_pack(data->group_peer_limit, bp);

Check warning on line 659 in toxcore/tox_event.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_event.c#L659

Added line #L659 was not covered by tests

case TOX_EVENT_GROUP_PASSWORD:
return tox_event_group_password_pack(event->data.group_password, bp);
return tox_event_group_password_pack(data->group_password, bp);

Check warning on line 662 in toxcore/tox_event.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_event.c#L662

Added line #L662 was not covered by tests

case TOX_EVENT_GROUP_MESSAGE:
return tox_event_group_message_pack(event->data.group_message, bp);
return tox_event_group_message_pack(data->group_message, bp);

Check warning on line 665 in toxcore/tox_event.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_event.c#L665

Added line #L665 was not covered by tests

case TOX_EVENT_GROUP_PRIVATE_MESSAGE:
return tox_event_group_private_message_pack(event->data.group_private_message, bp);
return tox_event_group_private_message_pack(data->group_private_message, bp);

Check warning on line 668 in toxcore/tox_event.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_event.c#L668

Added line #L668 was not covered by tests

case TOX_EVENT_GROUP_CUSTOM_PACKET:
return tox_event_group_custom_packet_pack(event->data.group_custom_packet, bp);
return tox_event_group_custom_packet_pack(data->group_custom_packet, bp);

Check warning on line 671 in toxcore/tox_event.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_event.c#L671

Added line #L671 was not covered by tests

case TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET:
return tox_event_group_custom_private_packet_pack(event->data.group_custom_private_packet, bp);
return tox_event_group_custom_private_packet_pack(data->group_custom_private_packet, bp);

Check warning on line 674 in toxcore/tox_event.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_event.c#L674

Added line #L674 was not covered by tests

case TOX_EVENT_GROUP_INVITE:
return tox_event_group_invite_pack(event->data.group_invite, bp);
return tox_event_group_invite_pack(data->group_invite, bp);

Check warning on line 677 in toxcore/tox_event.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_event.c#L677

Added line #L677 was not covered by tests

case TOX_EVENT_GROUP_PEER_JOIN:
return tox_event_group_peer_join_pack(event->data.group_peer_join, bp);
return tox_event_group_peer_join_pack(data->group_peer_join, bp);

Check warning on line 680 in toxcore/tox_event.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_event.c#L680

Added line #L680 was not covered by tests

case TOX_EVENT_GROUP_PEER_EXIT:
return tox_event_group_peer_exit_pack(event->data.group_peer_exit, bp);
return tox_event_group_peer_exit_pack(data->group_peer_exit, bp);

Check warning on line 683 in toxcore/tox_event.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_event.c#L683

Added line #L683 was not covered by tests

case TOX_EVENT_GROUP_SELF_JOIN:
return tox_event_group_self_join_pack(event->data.group_self_join, bp);
return tox_event_group_self_join_pack(data->group_self_join, bp);

Check warning on line 686 in toxcore/tox_event.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_event.c#L686

Added line #L686 was not covered by tests

case TOX_EVENT_GROUP_JOIN_FAIL:
return tox_event_group_join_fail_pack(event->data.group_join_fail, bp);
return tox_event_group_join_fail_pack(data->group_join_fail, bp);

Check warning on line 689 in toxcore/tox_event.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_event.c#L689

Added line #L689 was not covered by tests

case TOX_EVENT_GROUP_MODERATION:
return tox_event_group_moderation_pack(event->data.group_moderation, bp);
return tox_event_group_moderation_pack(data->group_moderation, bp);

Check warning on line 692 in toxcore/tox_event.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_event.c#L692

Added line #L692 was not covered by tests

case TOX_EVENT_INVALID:
return false;
Expand All @@ -697,6 +698,15 @@ bool tox_event_pack(const Tox_Event *event, Bin_Pack *bp)
return false;
}

bool tox_event_pack(const Tox_Event *event, Bin_Pack *bp)
{
assert(event->type != TOX_EVENT_INVALID);

return bin_pack_array(bp, 2)
&& tox_event_type_pack(event->type, bp)
&& tox_event_data_pack(event->type, &event->data, bp);
}

non_null()
static bool tox_event_type_from_int(uint32_t value, Tox_Event_Type *out)
{
Expand Down

0 comments on commit afc4724

Please sign in to comment.