Skip to content

Commit 1ad0539

Browse files
Remove cfg_attr(feature = 'impl', etc.) attributes
1 parent 6769557 commit 1ad0539

File tree

1 file changed

+55
-22
lines changed

1 file changed

+55
-22
lines changed

dylo-cli/src/main.rs

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ fn process_mod(mod_info: ModInfo, force: bool) -> std::io::Result<()> {
266266

267267
let mut con_items: Vec<Item> = ast.items.clone();
268268
let mut spec_items: Vec<Item> = Default::default();
269-
transform_macro_items(&mut con_items, &mut spec_items);
269+
transform_ast(&mut con_items, &mut spec_items);
270270

271271
let duration = start.elapsed();
272272

@@ -522,42 +522,75 @@ impl InterfaceType {
522522
}
523523
}
524524

525-
fn transform_macro_items(items: &mut Vec<Item>, added_items: &mut Vec<Item>) {
526-
items.retain(|item| {
525+
fn transform_ast(items: &mut Vec<Item>, added_items: &mut Vec<Item>) {
526+
items.retain_mut(|item| {
527527
let mut keep = true;
528-
if let Item::Impl(imp) = item {
529-
for attr in &imp.attrs {
530-
if attr.path().segments.len() == 2
531-
&& attr.path().segments[0].ident == "dylo"
532-
&& attr.path().segments[1].ident == "export"
533-
{
534-
let iface_typ = if let Ok(_meta) = attr.meta.require_path_only() {
535-
Some(InterfaceType::Sync)
536-
} else if let Ok(list) = attr.meta.require_list() {
537-
if list.tokens.to_string().contains("nonsync") {
538-
Some(InterfaceType::NonSync)
528+
529+
match item {
530+
Item::Impl(imp) => {
531+
for attr in &imp.attrs {
532+
if attr.path().segments.len() == 2
533+
&& attr.path().segments[0].ident == "dylo"
534+
&& attr.path().segments[1].ident == "export"
535+
{
536+
let iface_typ = if let Ok(_meta) = attr.meta.require_path_only() {
537+
Some(InterfaceType::Sync)
538+
} else if let Ok(list) = attr.meta.require_list() {
539+
if list.tokens.to_string().contains("nonsync") {
540+
Some(InterfaceType::NonSync)
541+
} else {
542+
None
543+
}
539544
} else {
540545
None
541-
}
542-
} else {
543-
None
544-
};
546+
};
545547

546-
if let Some(iface_typ) = iface_typ {
547-
let tokens = (&imp).into_token_stream();
548-
added_items.push(declare_trait(&tokens, &iface_typ)[0].clone());
548+
if let Some(iface_typ) = iface_typ {
549+
let tokens = (&imp).into_token_stream();
550+
added_items.push(declare_trait(&tokens, &iface_typ)[0].clone());
551+
}
552+
keep = false
549553
}
550-
keep = false
551554
}
552555
}
556+
Item::Enum(enm) => {
557+
filter_out_cfg_attr_impl(&mut enm.attrs);
558+
}
559+
Item::Struct(stru) => {
560+
filter_out_cfg_attr_impl(&mut stru.attrs);
561+
for f in stru.fields.iter_mut() {
562+
filter_out_cfg_attr_impl(&mut f.attrs);
563+
}
564+
}
565+
_ => {
566+
// ignore
567+
}
553568
}
569+
554570
if should_remove_item(item) {
555571
keep = false
556572
}
557573
keep
558574
});
559575
}
560576

577+
fn filter_out_cfg_attr_impl(attrs: &mut Vec<Attribute>) {
578+
attrs.retain_mut(|attr| {
579+
if let syn::Meta::List(list) = &attr.meta {
580+
if let Some(path_segment) = list.path.segments.first() {
581+
if path_segment.ident == "cfg_attr" {
582+
if let Some(nested) = list.tokens.to_string().split(",").next() {
583+
if nested.contains("feature") && nested.contains("\"impl\"") {
584+
return false;
585+
}
586+
}
587+
}
588+
}
589+
}
590+
true
591+
})
592+
}
593+
561594
fn declare_trait(tokens: &proc_macro2::TokenStream, iface_typ: &InterfaceType) -> Vec<Item> {
562595
let mut added_items = Vec::new();
563596
let file = syn::parse2::<syn::File>(tokens.clone()).unwrap();

0 commit comments

Comments
 (0)