Skip to content

Commit 1b622c5

Browse files
Update parser.rs
1 parent 55fba14 commit 1b622c5

File tree

1 file changed

+36
-40
lines changed

1 file changed

+36
-40
lines changed

core/src/parser.rs

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use crate::{
1111
target_os_check::accept_target_os,
1212
visitors::{ImportedType, TypeShareVisitor},
1313
};
14-
use itertools::Either;
1514
use log::debug;
1615
use proc_macro2::Ident;
1716
use std::{
@@ -589,7 +588,7 @@ pub(crate) fn get_field_type_override(attrs: &[syn::Attribute]) -> Option<String
589588
get_name_value_meta_items(attrs, "serialized_as", TYPESHARE).next()
590589
}
591590

592-
pub(crate) fn get_name_value_meta_items<'a>(
591+
fn get_name_value_meta_items<'a>(
593592
attrs: &'a [syn::Attribute],
594593
name: &'a str,
595594
ident: &'static str,
@@ -602,22 +601,49 @@ pub(crate) fn get_name_value_meta_items<'a>(
602601
}
603602
_ => None,
604603
})
605-
.collect::<Vec<_>>()
604+
.chain(
605+
// If we are searching for typeshare attributes then we'll look into cfg_att as well.
606+
(ident == TYPESHARE)
607+
.then(|| {
608+
attrs.iter().flat_map(move |attr| {
609+
get_meta_items(attr, "cfg_attr")
610+
.filter_map(|meta| match meta {
611+
Meta::List(list) if list.path.is_ident(TYPESHARE) => list
612+
.parse_args_with(
613+
Punctuated::<Meta, Token![,]>::parse_terminated,
614+
)
615+
.ok(),
616+
_ => None,
617+
})
618+
.flatten()
619+
.filter_map(|meta| match &meta {
620+
Meta::NameValue(meta_name_value)
621+
if meta_name_value.path.is_ident(name) =>
622+
{
623+
expr_to_string(&meta_name_value.value)
624+
}
625+
_ => None,
626+
})
627+
})
628+
})
629+
.into_iter()
630+
.flatten(),
631+
)
606632
})
607633
}
608634

609635
/// Returns all arguments passed into `#[{ident}(...)]` where `{ident}` can be `serde` or `typeshare` attributes
610636
#[inline(always)]
611637
pub(crate) fn get_meta_items(attr: &syn::Attribute, ident: &str) -> impl Iterator<Item = Meta> {
612-
if attr.path().is_ident(ident) {
613-
Either::Left(
638+
attr.path()
639+
.is_ident(ident)
640+
.then(|| {
614641
attr.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated)
615642
.into_iter()
616-
.flat_map(|punctuated| punctuated.into_iter()),
617-
)
618-
} else {
619-
Either::Right(std::iter::empty())
620-
}
643+
.flat_map(|punctuated| punctuated.into_iter())
644+
})
645+
.into_iter()
646+
.flatten()
621647
}
622648

623649
fn get_ident(
@@ -855,36 +881,6 @@ fn get_decorators(attrs: &[syn::Attribute]) -> DecoratorMap {
855881
.extend(value.split(',').map(|s| s.trim().to_string()));
856882
}
857883

858-
// Check cfg_attr.
859-
let nvps = attrs
860-
.iter()
861-
.flat_map(|attr| get_meta_items(attr, "cfg_attr"))
862-
.flat_map(|meta| match meta {
863-
Meta::List(nvp) if nvp.path.is_ident(TYPESHARE) => nvp
864-
.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated)
865-
.ok(),
866-
_ => None,
867-
})
868-
.flat_map(|nvps| nvps.into_iter());
869-
870-
for meta in nvps {
871-
for kind in decorator_kinds {
872-
match &meta {
873-
Meta::NameValue(meta_name_value)
874-
if meta_name_value.path.is_ident(kind.as_str()) =>
875-
{
876-
if let Some(val) = expr_to_string(&meta_name_value.value) {
877-
decorator_map
878-
.entry(kind)
879-
.or_default()
880-
.extend(val.split(',').map(|s| s.trim().to_string()));
881-
}
882-
}
883-
_ => {}
884-
}
885-
}
886-
}
887-
888884
decorator_map
889885
}
890886

0 commit comments

Comments
 (0)