@@ -95,64 +95,48 @@ impl Operation {
9595 }
9696
9797 /// Bidirectional mapping: `Operation` -> client command code.
98+ ///
99+ /// Delegates to the dispatch table as the single source of truth.
98100 #[ must_use]
99101 pub const fn to_command_code ( & self ) -> Option < u32 > {
100- use crate :: codes;
101102 match self {
102103 Self :: Reserved => None ,
103- Self :: CreateStream => Some ( codes:: CREATE_STREAM_CODE ) ,
104- Self :: UpdateStream => Some ( codes:: UPDATE_STREAM_CODE ) ,
105- Self :: DeleteStream => Some ( codes:: DELETE_STREAM_CODE ) ,
106- Self :: PurgeStream => Some ( codes:: PURGE_STREAM_CODE ) ,
107- Self :: CreateTopic => Some ( codes:: CREATE_TOPIC_CODE ) ,
108- Self :: UpdateTopic => Some ( codes:: UPDATE_TOPIC_CODE ) ,
109- Self :: DeleteTopic => Some ( codes:: DELETE_TOPIC_CODE ) ,
110- Self :: PurgeTopic => Some ( codes:: PURGE_TOPIC_CODE ) ,
111- Self :: CreatePartitions => Some ( codes:: CREATE_PARTITIONS_CODE ) ,
112- Self :: DeletePartitions => Some ( codes:: DELETE_PARTITIONS_CODE ) ,
113- Self :: DeleteSegments => Some ( codes:: DELETE_SEGMENTS_CODE ) ,
114- Self :: CreateConsumerGroup => Some ( codes:: CREATE_CONSUMER_GROUP_CODE ) ,
115- Self :: DeleteConsumerGroup => Some ( codes:: DELETE_CONSUMER_GROUP_CODE ) ,
116- Self :: CreateUser => Some ( codes:: CREATE_USER_CODE ) ,
117- Self :: UpdateUser => Some ( codes:: UPDATE_USER_CODE ) ,
118- Self :: DeleteUser => Some ( codes:: DELETE_USER_CODE ) ,
119- Self :: ChangePassword => Some ( codes:: CHANGE_PASSWORD_CODE ) ,
120- Self :: UpdatePermissions => Some ( codes:: UPDATE_PERMISSIONS_CODE ) ,
121- Self :: CreatePersonalAccessToken => Some ( codes:: CREATE_PERSONAL_ACCESS_TOKEN_CODE ) ,
122- Self :: DeletePersonalAccessToken => Some ( codes:: DELETE_PERSONAL_ACCESS_TOKEN_CODE ) ,
123- Self :: SendMessages => Some ( codes:: SEND_MESSAGES_CODE ) ,
124- Self :: StoreConsumerOffset => Some ( codes:: STORE_CONSUMER_OFFSET_CODE ) ,
104+ Self :: CreateStream
105+ | Self :: UpdateStream
106+ | Self :: DeleteStream
107+ | Self :: PurgeStream
108+ | Self :: CreateTopic
109+ | Self :: UpdateTopic
110+ | Self :: DeleteTopic
111+ | Self :: PurgeTopic
112+ | Self :: CreatePartitions
113+ | Self :: DeletePartitions
114+ | Self :: DeleteSegments
115+ | Self :: CreateConsumerGroup
116+ | Self :: DeleteConsumerGroup
117+ | Self :: CreateUser
118+ | Self :: UpdateUser
119+ | Self :: DeleteUser
120+ | Self :: ChangePassword
121+ | Self :: UpdatePermissions
122+ | Self :: CreatePersonalAccessToken
123+ | Self :: DeletePersonalAccessToken
124+ | Self :: SendMessages
125+ | Self :: StoreConsumerOffset => match crate :: dispatch:: lookup_by_operation ( * self ) {
126+ Some ( meta) => Some ( meta. code ) ,
127+ None => None ,
128+ } ,
125129 }
126130 }
127131
128132 /// Bidirectional mapping: client command code -> `Operation`.
133+ ///
134+ /// Delegates to the dispatch table as the single source of truth.
129135 #[ must_use]
130136 pub const fn from_command_code ( code : u32 ) -> Option < Self > {
131- use crate :: codes;
132- match code {
133- codes:: CREATE_STREAM_CODE => Some ( Self :: CreateStream ) ,
134- codes:: UPDATE_STREAM_CODE => Some ( Self :: UpdateStream ) ,
135- codes:: DELETE_STREAM_CODE => Some ( Self :: DeleteStream ) ,
136- codes:: PURGE_STREAM_CODE => Some ( Self :: PurgeStream ) ,
137- codes:: CREATE_TOPIC_CODE => Some ( Self :: CreateTopic ) ,
138- codes:: UPDATE_TOPIC_CODE => Some ( Self :: UpdateTopic ) ,
139- codes:: DELETE_TOPIC_CODE => Some ( Self :: DeleteTopic ) ,
140- codes:: PURGE_TOPIC_CODE => Some ( Self :: PurgeTopic ) ,
141- codes:: CREATE_PARTITIONS_CODE => Some ( Self :: CreatePartitions ) ,
142- codes:: DELETE_PARTITIONS_CODE => Some ( Self :: DeletePartitions ) ,
143- codes:: DELETE_SEGMENTS_CODE => Some ( Self :: DeleteSegments ) ,
144- codes:: CREATE_CONSUMER_GROUP_CODE => Some ( Self :: CreateConsumerGroup ) ,
145- codes:: DELETE_CONSUMER_GROUP_CODE => Some ( Self :: DeleteConsumerGroup ) ,
146- codes:: CREATE_USER_CODE => Some ( Self :: CreateUser ) ,
147- codes:: UPDATE_USER_CODE => Some ( Self :: UpdateUser ) ,
148- codes:: DELETE_USER_CODE => Some ( Self :: DeleteUser ) ,
149- codes:: CHANGE_PASSWORD_CODE => Some ( Self :: ChangePassword ) ,
150- codes:: UPDATE_PERMISSIONS_CODE => Some ( Self :: UpdatePermissions ) ,
151- codes:: CREATE_PERSONAL_ACCESS_TOKEN_CODE => Some ( Self :: CreatePersonalAccessToken ) ,
152- codes:: DELETE_PERSONAL_ACCESS_TOKEN_CODE => Some ( Self :: DeletePersonalAccessToken ) ,
153- codes:: SEND_MESSAGES_CODE => Some ( Self :: SendMessages ) ,
154- codes:: STORE_CONSUMER_OFFSET_CODE => Some ( Self :: StoreConsumerOffset ) ,
155- _ => None ,
137+ match crate :: dispatch:: lookup_command ( code) {
138+ Some ( meta) => meta. operation ,
139+ None => None ,
156140 }
157141 }
158142}
@@ -204,10 +188,11 @@ mod tests {
204188
205189 #[ test]
206190 fn read_only_commands_have_no_operation ( ) {
207- assert ! ( Operation :: from_command_code( crate :: PING_CODE ) . is_none( ) ) ;
208- assert ! ( Operation :: from_command_code( crate :: GET_STATS_CODE ) . is_none( ) ) ;
209- assert ! ( Operation :: from_command_code( crate :: GET_STREAM_CODE ) . is_none( ) ) ;
210- assert ! ( Operation :: from_command_code( crate :: POLL_MESSAGES_CODE ) . is_none( ) ) ;
191+ use crate :: codes:: { GET_STATS_CODE , GET_STREAM_CODE , PING_CODE , POLL_MESSAGES_CODE } ;
192+ assert ! ( Operation :: from_command_code( PING_CODE ) . is_none( ) ) ;
193+ assert ! ( Operation :: from_command_code( GET_STATS_CODE ) . is_none( ) ) ;
194+ assert ! ( Operation :: from_command_code( GET_STREAM_CODE ) . is_none( ) ) ;
195+ assert ! ( Operation :: from_command_code( POLL_MESSAGES_CODE ) . is_none( ) ) ;
211196 }
212197
213198 #[ test]
0 commit comments