Skip to content

Commit

Permalink
refactor(ast_tools): simplify code in AstKindGenerator (#6774)
Browse files Browse the repository at this point in the history
Combine multiple filters into one.

The first 2 filters were doing the same thing, and combining the remaining 2 makes the code easier to read.
  • Loading branch information
overlookmotel committed Oct 22, 2024
1 parent 0b6db0e commit 4689865
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions tasks/ast_tools/src/generators/ast_kind.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use convert_case::{Case, Casing};
use itertools::Itertools;
use quote::{format_ident, quote};
use syn::{parse_quote, Arm, Ident, ImplItemFn, Type, Variant};
use syn::{parse_quote, Arm, ImplItemFn, Variant};

use super::define_generator;
use crate::{
codegen::{generated_header, LateCtx},
output,
schema::{GetIdent, ToType, TypeDef},
schema::{GetIdent, ToType},
Generator, GeneratorOutput,
};

Expand Down Expand Up @@ -81,20 +81,20 @@ pub const BLACK_LIST: [&str; 61] = [

impl Generator for AstKindGenerator {
fn generate(&mut self, ctx: &LateCtx) -> GeneratorOutput {
let have_kinds: Vec<(Ident, Type)> = ctx
let have_kinds = ctx
.schema()
.into_iter()
.filter(|it| it.visitable())
.filter(
|maybe_kind| matches!(maybe_kind, kind @ (TypeDef::Enum(_) | TypeDef::Struct(_)) if kind.visitable())
)
.filter(|def| {
let is_visitable = def.visitable();
let is_blacklisted = BLACK_LIST.contains(&def.name().as_str());
is_visitable && !is_blacklisted
})
.map(|def| {
let ident = def.ident();
let typ = def.to_type();
(ident, typ)
})
.filter(|(ident, _)| !BLACK_LIST.contains(&ident.to_string().as_str()))
.collect();
.collect_vec();

let types: Vec<Variant> =
have_kinds.iter().map(|(ident, _)| parse_quote!(#ident)).collect_vec();
Expand Down

0 comments on commit 4689865

Please sign in to comment.