summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2023-11-06 19:54:16 +0100
committerThomas Haller <thaller@redhat.com>2023-11-15 09:34:46 +0100
commit8ccd1f7bfed399e236b78c29b1f9549fc92afb72 (patch)
tree76132df0cdd9d0c2efc9cd41611d5534e2ed9b68
parent8e1330964d982d901f5c29d899f25e54026a117c (diff)
cli: refactor active_connection_get_state_ord()
Additional logic will be added, that makes the switch() approach more cumbersome. Use a sorted array instead to find the priority.
-rw-r--r--src/nmcli/connections.c28
1 files changed, 17 insertions, 11 deletions
diff --git a/src/nmcli/connections.c b/src/nmcli/connections.c
index df81a70712..b1db014069 100644
--- a/src/nmcli/connections.c
+++ b/src/nmcli/connections.c
@@ -291,23 +291,29 @@ connection_type_to_display(const char *type, NMMetaAccessorGetType get_type)
static int
active_connection_get_state_ord(NMActiveConnection *active)
{
+ static const NMActiveConnectionState ordered_states[] = {
+ NM_ACTIVE_CONNECTION_STATE_UNKNOWN,
+ NM_ACTIVE_CONNECTION_STATE_DEACTIVATED,
+ NM_ACTIVE_CONNECTION_STATE_DEACTIVATING,
+ NM_ACTIVE_CONNECTION_STATE_ACTIVATING,
+ NM_ACTIVE_CONNECTION_STATE_ACTIVATED,
+ };
+ NMActiveConnectionState state;
+ int i;
+
/* returns an integer related to @active's state, that can be used for sorting
* active connections based on their activation state. */
+
if (!active)
return -2;
- switch (nm_active_connection_get_state(active)) {
- case NM_ACTIVE_CONNECTION_STATE_UNKNOWN:
- return 0;
- case NM_ACTIVE_CONNECTION_STATE_DEACTIVATED:
- return 1;
- case NM_ACTIVE_CONNECTION_STATE_DEACTIVATING:
- return 2;
- case NM_ACTIVE_CONNECTION_STATE_ACTIVATING:
- return 3;
- case NM_ACTIVE_CONNECTION_STATE_ACTIVATED:
- return 4;
+ state = nm_active_connection_get_state(active);
+
+ for (i = 0; i < (int) G_N_ELEMENTS(ordered_states); i++) {
+ if (state == ordered_states[i])
+ return i;
}
+
return -1;
}