Add ClientMessage and ServerMessage sum types with typed deserialization dispatch
Introduce two unified enums to the protocol module that wrap all message
variants for type-safe routing in the connection handler state machine.
ClientMessage covers the five messages the client can send (Handshake,
ArchiveRequest, ArchiveComplete, RestoreRequest, StatusRequest) and
ServerMessage covers the six server responses (HandshakeAck, ArchiveAccept,
RestoreAccept, RestoreComplete, StatusResponse, Error). Each variant
delegates serialization to the inner struct via serde’s internally-tagged
enum representation using a ‘type’ discriminator field.
The message_type method on each enum maps variants to their MessageType
discriminant byte, enabling the frame encoder to write the correct header
without the caller tracking the mapping manually. Two deserialization
functions, decode_client_message and decode_server_message, take a raw
payload and a MessageType discriminant and return the typed enum. They
validate that the discriminant matches the expected direction, rejecting
client message types when a server message is expected and vice versa,
catching protocol-level dispatch bugs at the decoding layer rather than
deeper in handler logic.
The MessageType enum is relocated from the frame module to the protocol
module since it is a protocol concern, resolving a circular import between
the two modules. Frame encoding continues to reference MessageType for the
frame header type byte. The from_byte constructor maps all 13 assigned
discriminants with explicit match arms for stability.
Eight new tests cover ClientMessage and ServerMessage JSON round-trips,
direction-mismatch rejection in decode_client_message and
decode_server_message, and message_type discriminant mapping for both
enums, adding to the existing serialization and discriminant tests.
Comments 0
No comments yet. Be the first!
Sign in to join the conversation.