Skip to content

Implement macro-based message codec #2

@ygunayer

Description

@ygunayer

Our current implementation of the message codec process is too verbose and complicated, and it doesn't even have the encoding phase.

We need to wrap this implementation in custom macros in order to provide a more readable and maintainable codec phase.

We imagine that given a structure like the following, our macros should be able to auto-generate all implementation details of the codec phase for the said structure.

Keep in mind that this is pseudocode so the exact syntax is TBD)

with_codec!(
    #[derive(Debug, PartialEq, Eq)]
    pub enum Command {
        command!("AUTH",
            Authenticate {
                arg!(id_like!(),
                    token: String
                )
            }
        ),

        command!("SEND",
            SendMessage {
                arg!(id_like!(),
                    recipient: String
                ),
                arg!(base64_encoded!(),
                    message: String
                )
            }
        ),

        command!("PING",
            Ping { }
        ),

        command!("PONG",
            Pong { }
        )
    }
);

With this in place, we should be able to simply a string into a command instance, or construct a string from a command instance using one-liners like as follows:

...
let command_string = "AUTH|1234123412341234";
let command = Command::Authenticate { token: "1234123412341234" };

let decoded = CommandCodec::decode(command_string);
assert_eq!(decoded, command); // should be true

let encoded = CommandParser::encode(command);
assert_eq!(encoded, command_string); // should be true
...

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions