I want to make a macro in rust to generate a custom syntax array in order to use in a match:
The match at it's baseline:
match &self.text[self.offset..] {
[b'f', b'o', b'r', b' ', ..] => {
Some(CToken::new(CTokenKind::For, self.line, self.column))
}
_ => None,
}
I would like the macro to generate the slice for the match branch, as follows:
match &self.text[self.offset..] {
mm!(b"for") => {
Some(CToken::new(CTokenKind::For, self.line, self.column))
}
_ => None,
}
I would like this macro because the keywords can get very long and it reduces a lot the readability of the code.
I have tried to implement a macro but can't get it right.
I succeeded to generate the array however my macro takes u8 elements and not a whole string:
macro_rules! mm {
($($ch:literal), *) => {
[$($ch,)* b' ', ..]
}
}
Using this macro I can use my macro as follows:
mm!(b'f', b'o', b'r') => ...
However it does not changes anything, so I would like the macro to take a whole b"my string here"