This repository was archived by the owner on Oct 10, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 13
This repository was archived by the owner on Oct 10, 2019. It is now read-only.
Use quote!
macro #2
Copy link
Copy link
Closed
Description
I think the code would be a lot less confusing, especially with the double braces {{
}}
all over the place. Let me know if there is a reason you chose not to do it this way originally.
Before
pub fn enum_body(name: &str, variants: &[Variant]) -> String {
let mut body = String::new();
write!(body, "
if type_.name() != \"{}\" {{
return false;
}}
match *type_.kind() {{
::postgres::types::Kind::Enum(ref variants) => {{
if variants.len() != {} {{
return false;
}}
variants.iter().all(|v| {{
match &**v {{", name, variants.len()).unwrap();
for variant in variants {
write!(body, "
\"{}\" => true,", variant.name).unwrap();
}
write!(body, "
_ => false,
}}
}})
}}
_ => false,
}}").unwrap();
body
}
After
pub fn enum_body(name: &str, variants: &[Variant]) -> Tokens {
let num_variants = variants.len();
let variant_names = variants.iter().map(|v| &v.name);
quote! {
if type_.name() != #name {
return false;
}
match *type_.kind() {
::postgres::types::Kind::Enum(ref variants) => {
if variants.len() != #num_variants {
return false;
}
variants.iter().all(|v| {
match &**v {
#(
#variant_names => true,
)*
_ => false,
}
})
}
_ => false,
}
}
}
Metadata
Metadata
Assignees
Labels
No labels