Skip to content

Commit

Permalink
refactor: Use enum-from-int rule from tokstyle.
Browse files Browse the repository at this point in the history
These functions are a bit clearer and don't need to change if enum
values change.

See TokTok/hs-tokstyle#212 for the relevant
linter implementation.
  • Loading branch information
iphydf authored and freylax committed Jan 13, 2024
1 parent 993d979 commit 5c07105
Show file tree
Hide file tree
Showing 17 changed files with 361 additions and 259 deletions.
14 changes: 7 additions & 7 deletions auto_tests/tox_dispatch_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,13 @@ static void test_tox_events(void)
static void fake_test_unpack(void)
{
// TODO(Green-Sky): add proper unpack tests and/or implement ngc events
(void)tox_unpack_group_privacy_state;
(void)tox_unpack_group_privacy_state;
(void)tox_unpack_group_voice_state;
(void)tox_unpack_group_topic_lock;
(void)tox_unpack_group_join_fail;
(void)tox_unpack_group_mod_event;
(void)tox_unpack_group_exit_type;
(void)tox_group_privacy_state_unpack;
(void)tox_group_privacy_state_unpack;
(void)tox_group_voice_state_unpack;
(void)tox_group_topic_lock_unpack;
(void)tox_group_join_fail_unpack;
(void)tox_group_mod_event_unpack;
(void)tox_group_exit_type_unpack;
}

int main(void)
Expand Down
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 @@
21cf23b1a2e46712663dc4f8daa322991af51b9e82626a127cf1bc8dc583b598 /usr/local/bin/tox-bootstrapd
f117aa0e2cf3f1b42f7e38beec55cb970025692c5753b1159aa22c6e52281393 /usr/local/bin/tox-bootstrapd
8 changes: 6 additions & 2 deletions testing/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ sh_test(
"-Wno-callback-names",
"-Wno-enum-names",
"+RTS",
"-N3",
"-N4",
"-RTS",
],
data = CIMPLE_FILES,
Expand All @@ -32,7 +32,11 @@ sh_test(
"-Iexternal/libvpx",
"-Iexternal/opus/include",
"-Ihs-tokstyle/include",
] + ["$(locations %s)" % f for f in CIMPLE_FILES],
] + ["$(locations %s)" % f for f in CIMPLE_FILES] + [
"+RTS",
"-N4",
"-RTS",
],
data = CIMPLE_FILES + [
"//c-toxcore/third_party:headers",
"//hs-tokstyle:headers",
Expand Down
44 changes: 30 additions & 14 deletions toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -757,17 +757,34 @@ int m_set_statusmessage(Messenger *m, const uint8_t *status, uint16_t length)
return 0;
}

static Userstatus userstatus_from_int(uint8_t status)
non_null()
static bool userstatus_from_int(uint8_t status, Userstatus *out)
{
switch (status) {
case 0:
return USERSTATUS_NONE;
case 1:
return USERSTATUS_AWAY;
case 2:
return USERSTATUS_BUSY;
default:
return USERSTATUS_INVALID;
case USERSTATUS_NONE: {
*out = USERSTATUS_NONE;
return true;
}

case USERSTATUS_AWAY: {
*out = USERSTATUS_AWAY;
return true;
}

case USERSTATUS_BUSY: {
*out = USERSTATUS_BUSY;
return true;
}

case USERSTATUS_INVALID: {
*out = USERSTATUS_INVALID;
return true;
}

default: {
*out = USERSTATUS_INVALID;
return false;
}
}
}

Expand All @@ -781,7 +798,7 @@ int m_set_userstatus(Messenger *m, uint8_t status)
return 0;
}

m->userstatus = userstatus_from_int(status);
userstatus_from_int(status, &m->userstatus);

for (uint32_t i = 0; i < m->numfriends; ++i) {
m->friendlist[i].userstatus_sent = false;
Expand Down Expand Up @@ -937,7 +954,7 @@ static int set_friend_statusmessage(const Messenger *m, int32_t friendnumber, co
non_null()
static void set_friend_userstatus(const Messenger *m, int32_t friendnumber, uint8_t status)
{
m->friendlist[friendnumber].userstatus = userstatus_from_int(status);
userstatus_from_int(status, &m->friendlist[friendnumber].userstatus);
}

non_null()
Expand Down Expand Up @@ -2095,9 +2112,8 @@ static int m_handle_packet_userstatus(Messenger *m, const int i, const uint8_t *
return 0;
}

const Userstatus status = userstatus_from_int(data[0]);

if (status == USERSTATUS_INVALID) {
Userstatus status;
if (!userstatus_from_int(data[0], &status)) {
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion toxcore/events/conference_invite.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ static bool tox_event_conference_invite_unpack(
}

return bin_unpack_u32(bu, &event->friend_number)
&& tox_unpack_conference_type(bu, &event->type)
&& tox_conference_type_unpack(bu, &event->type)
&& bin_unpack_bin(bu, &event->cookie, &event->cookie_length);
}

Expand Down
2 changes: 1 addition & 1 deletion toxcore/events/conference_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ static bool tox_event_conference_message_unpack(

return bin_unpack_u32(bu, &event->conference_number)
&& bin_unpack_u32(bu, &event->peer_number)
&& tox_unpack_message_type(bu, &event->type)
&& tox_message_type_unpack(bu, &event->type)
&& bin_unpack_bin(bu, &event->message, &event->message_length);
}

Expand Down
2 changes: 1 addition & 1 deletion toxcore/events/file_recv_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ static bool tox_event_file_recv_control_unpack(

return bin_unpack_u32(bu, &event->friend_number)
&& bin_unpack_u32(bu, &event->file_number)
&& tox_unpack_file_control(bu, &event->control);
&& tox_file_control_unpack(bu, &event->control);
}


Expand Down
2 changes: 1 addition & 1 deletion toxcore/events/friend_connection_status.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ static bool tox_event_friend_connection_status_unpack(
}

return bin_unpack_u32(bu, &event->friend_number)
&& tox_unpack_connection(bu, &event->connection_status);
&& tox_connection_unpack(bu, &event->connection_status);
}


Expand Down
2 changes: 1 addition & 1 deletion toxcore/events/friend_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ static bool tox_event_friend_message_unpack(
}

return bin_unpack_u32(bu, &event->friend_number)
&& tox_unpack_message_type(bu, &event->type)
&& tox_message_type_unpack(bu, &event->type)
&& bin_unpack_bin(bu, &event->message, &event->message_length);
}

Expand Down
2 changes: 1 addition & 1 deletion toxcore/events/friend_status.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ static bool tox_event_friend_status_unpack(
}

return bin_unpack_u32(bu, &event->friend_number)
&& tox_unpack_user_status(bu, &event->status);
&& tox_user_status_unpack(bu, &event->status);
}


Expand Down
2 changes: 1 addition & 1 deletion toxcore/events/self_connection_status.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static bool tox_event_self_connection_status_unpack(
Tox_Event_Self_Connection_Status *event, Bin_Unpack *bu)
{
assert(event != nullptr);
return tox_unpack_connection(bu, &event->connection_status);
return tox_connection_unpack(bu, &event->connection_status);
}


Expand Down
4 changes: 2 additions & 2 deletions toxcore/group_chats.c
Original file line number Diff line number Diff line change
Expand Up @@ -1280,8 +1280,8 @@ static uint16_t unpack_gc_shared_state(GC_SharedState *shared_state, const uint8
memcpy(&voice_state, data + len_processed, sizeof(uint8_t));
len_processed += sizeof(uint8_t);

shared_state->voice_state = group_voice_state_from_int(voice_state);
shared_state->privacy_state = group_privacy_state_from_int(privacy_state);
group_voice_state_from_int(voice_state, &shared_state->voice_state);
group_privacy_state_from_int(privacy_state, &shared_state->privacy_state);

return len_processed;
}
Expand Down
55 changes: 37 additions & 18 deletions toxcore/group_pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,48 @@
#include "ccompat.h"
#include "util.h"

Group_Privacy_State group_privacy_state_from_int(uint8_t value)
bool group_privacy_state_from_int(uint8_t value, Group_Privacy_State *out)
{
switch (value) {
case 0:
return GI_PUBLIC;
case 1:
return GI_PRIVATE;
default:
return GI_PUBLIC;
case GI_PUBLIC: {
*out = GI_PUBLIC;
return true;
}

case GI_PRIVATE: {
*out = GI_PRIVATE;
return true;
}

default: {
*out = GI_PUBLIC;
return false;
}
}
}

Group_Voice_State group_voice_state_from_int(uint8_t value)
bool group_voice_state_from_int(uint8_t value, Group_Voice_State *out)
{
switch (value) {
case 0:
return GV_ALL;
case 1:
return GV_MODS;
case 2:
return GV_FOUNDER;
default:
return GV_ALL;
case GV_ALL: {
*out = GV_ALL;
return true;
}

case GV_MODS: {
*out = GV_MODS;
return true;
}

case GV_FOUNDER: {
*out = GV_FOUNDER;
return true;
}

default: {
*out = GV_ALL;
return false;
}
}
}

Expand Down Expand Up @@ -69,8 +88,8 @@ static bool load_unpack_state_values(GC_Chat *chat, Bin_Unpack *bu)
}

chat->connection_state = manually_disconnected ? CS_DISCONNECTED : CS_CONNECTING;
chat->shared_state.privacy_state = group_privacy_state_from_int(privacy_state);
chat->shared_state.voice_state = group_voice_state_from_int(voice_state);
group_privacy_state_from_int(privacy_state, &chat->shared_state.privacy_state);
group_voice_state_from_int(voice_state, &chat->shared_state.voice_state);

// we always load saved groups as private in case the group became private while we were offline.
// this will have no detrimental effect if the group is public, as the correct privacy
Expand Down
6 changes: 4 additions & 2 deletions toxcore/group_pack.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ void gc_save_pack_group(const GC_Chat *chat, Bin_Pack *bp);
non_null()
bool gc_load_unpack_group(GC_Chat *chat, Bin_Unpack *bu);

Group_Privacy_State group_privacy_state_from_int(uint8_t value);
Group_Voice_State group_voice_state_from_int(uint8_t value);
non_null()
bool group_privacy_state_from_int(uint8_t value, Group_Privacy_State *out);
non_null()
bool group_voice_state_from_int(uint8_t value, Group_Voice_State *out);

#endif // GROUP_PACK_H
4 changes: 2 additions & 2 deletions toxcore/tox_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ bool tox_events_pack(const Tox_Events *events, Bin_Pack *bp)
}

non_null()
static bool tox_event_unpack(Tox_Events *events, Bin_Unpack *bu)
static bool tox_events_unpack_event(Tox_Events *events, Bin_Unpack *bu)
{
uint32_t size;
if (!bin_unpack_array(bu, &size)) {
Expand Down Expand Up @@ -210,7 +210,7 @@ bool tox_events_unpack(Tox_Events *events, Bin_Unpack *bu)
}

for (uint32_t i = 0; i < size; ++i) {
if (!tox_event_unpack(events, bu)) {
if (!tox_events_unpack_event(events, bu)) {
return false;
}
}
Expand Down
Loading

0 comments on commit 5c07105

Please sign in to comment.