Created
May 31, 2021 06:33
-
-
Save vedantroy/84bb030ae11d8b2b3bacef175412af57 to your computer and use it in GitHub Desktop.
Macro Shenanigans
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
macro_rules! expected_variants_join { | |
($x:ident) => (stringify!($x)); | |
($x:ident, $($y:ident),*) => { concat!(stringify!($x), " ", expected_variants!($($y),*)) }; | |
} | |
macro_rules! expected_variants { | |
($x:ident) => { concat!("Expected: ", stringify!($x)) }; | |
($x:ident, $($y:ident),*) => { concat!("Expected one of: ", stringify!($x), " ", expected_variants_join!($($y),*)) }; | |
} | |
macro_rules! check_variants { | |
($v:expr, $($variant:ident),*) => {{ | |
let v = &($v).data.borrow().value; | |
let is_match = $(matches!(v, NodeValue::$variant { .. })) || +; | |
if !is_match { return Err(WranglerError::UnexpectedNodeType(concat!(expected_variants!($($variant),*)), format!("{:?}", v))) } | |
}}; | |
} | |
fn document<'a>(node: &'a AstNode<'a>) -> Result<(), WranglerError> { | |
check_variants!(node, Document); | |
let node = node.data.borrow(); | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment