Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement macro-based message codec #2

Open
ygunayer opened this issue Nov 2, 2018 · 0 comments
Open

Implement macro-based message codec #2

ygunayer opened this issue Nov 2, 2018 · 0 comments
Assignees

Comments

@ygunayer
Copy link
Member

ygunayer commented Nov 2, 2018

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
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants