feat(xtask): impl as_ast_kind method for each variant (#5491)

This commit is contained in:
IWANABETHATGUY 2024-09-05 16:17:08 +00:00
parent a96866d951
commit cedf7a4daa
2 changed files with 1374 additions and 2 deletions

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,7 @@
use convert_case::{Case, Casing};
use itertools::Itertools;
use quote::quote;
use syn::{parse_quote, Arm, Ident, Type, Variant};
use quote::{format_ident, quote};
use syn::{parse_quote, Arm, Ident, ImplItemFn, Type, Variant};
use crate::{
codegen::{generated_header, LateCtx},
@ -154,6 +155,24 @@ impl Generator for AstKindGenerator {
.map(|(ident, _)| parse_quote!(Self :: #ident(it) => it.span()))
.collect_vec();
let as_ast_kind_impls: Vec<ImplItemFn> = have_kinds
.iter()
.map(|(ident, typ)| {
let snake_case_name =
format_ident!("as_{}", ident.to_string().to_case(Case::Snake));
parse_quote!(
///@@line_break
pub fn #snake_case_name(&self) -> Option<&#typ> {
if let Self::#ident(v) = self {
Some(*v)
} else {
None
}
}
)
})
.collect_vec();
let header = generated_header!();
GeneratorOutput(
@ -189,6 +208,11 @@ impl Generator for AstKindGenerator {
}
}
}
///@@line_break
impl<'a> AstKind<'a> {
#(#as_ast_kind_impls)*
}
},
)
}