Mapping Messages with Macros
Throughout the book, we will be using a web framework to match HTTP requests to the right function to be handled. However, there are times where you want to accept a message over a TCP connection and match the handling function based on the message received. (It does not have to be via TCP, I have found this type of macro to be useful for module interfaces or receiving messages via a channel.) In this section, we will implement a simple macro that will reduce the amount of code we need to write when mapping a struct to a function.
To map messages, we initially must define the data contracts that we will be mapping to our functions with the following code:
#[derive(Debug)]
pub struct ContractOne {
input_data: String,
output_data: Option<String>
}
#[derive(Debug)]
pub struct ContractTwo {
input_data: String,
output_data: Option<String>
}
With these data contracts, we have an input field and an output field that is populated when...