Skip to content

Commit

Permalink
chore: #[di] attribute used on a field or arguments can be duplicated
Browse files Browse the repository at this point in the history
  • Loading branch information
ZihanType committed Sep 11, 2023
1 parent 4a5256f commit b4c9620
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 49 deletions.
27 changes: 4 additions & 23 deletions rudi-macro/src/commons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,30 +204,11 @@ fn generate_only_one_field_or_argument_resolve_method(
field_or_argument_ty: &Type,
scope: Scope,
) -> syn::Result<ResolveOne> {
let attr = match FieldOrArgumentAttribute::from_attrs(attrs)? {
Some(attr) => attr,
None => {
let ident = format_ident!("owned_{}", index);

let resolve = match color {
Color::Async => parse_quote! {
let #ident = cx.resolve_with_name_async("").await;
},
Color::Sync => parse_quote! {
let #ident = cx.resolve_with_name("");
},
};

return Ok(ResolveOne {
stmt: ResolveOneValue::Owned { resolve },
variable: ident,
});
}
};
let attr = FieldOrArgumentAttribute::from_attrs(attrs)?;

match (scope, &attr.ref_) {
(Scope::Singleton, _) => {}
(/* not singleton */ _, Some((span, _))) => {
match (&attr.ref_, scope) {
(_, Scope::Singleton) => {}
(Some((span, _)), /* not singleton */ _) => {
return Err(syn::Error::new(
*span,
"only support `ref` argument in `#[Singleton]` item",
Expand Down
34 changes: 8 additions & 26 deletions rudi-macro/src/field_or_argument_attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,17 @@ impl FieldOrArgumentAttribute {

Err(meta.error("the argument must be one of: `name`, `option`, `default`, `vec`, `ref`"))
}
}

impl TryFrom<&Attribute> for FieldOrArgumentAttribute {
type Error = syn::Error;

fn try_from(attr: &Attribute) -> Result<Self, Self::Error> {
let mut field_or_argument_attr = FieldOrArgumentAttribute::default();
attr.parse_nested_meta(|meta| field_or_argument_attr.parse(meta))?;
fn parse_attr(&mut self, attr: &Attribute) -> syn::Result<()> {
attr.parse_nested_meta(|meta| self.parse(meta))?;

let FieldOrArgumentAttribute {
name,
option,
default,
vec,
ref_: _ref_,
} = &field_or_argument_attr;
} = &self;

if let (Some((name, _)), Some(vec)) = (name, vec) {
macro_rules! err {
Expand Down Expand Up @@ -185,33 +180,20 @@ impl TryFrom<&Attribute> for FieldOrArgumentAttribute {
_ => {}
}

Ok(field_or_argument_attr)
Ok(())
}
}

impl FieldOrArgumentAttribute {
pub(crate) fn from_attrs(
attrs: &mut Vec<Attribute>,
) -> syn::Result<Option<FieldOrArgumentAttribute>> {
let mut field_or_argument_attr = None;
pub(crate) fn from_attrs(attrs: &mut Vec<Attribute>) -> syn::Result<FieldOrArgumentAttribute> {
let mut field_or_argument_attr = FieldOrArgumentAttribute::default();
let mut errors = Vec::new();
let mut di_already_appeared = false;

attrs.retain(|attr| {
if !attr.path().is_ident("di") {
return true;
}

if di_already_appeared {
let err = syn::Error::new(attr.span(), "duplicate `#[di(...)]` attribute");
errors.push(err);
} else {
di_already_appeared = true;

match FieldOrArgumentAttribute::try_from(attr) {
Ok(o) => field_or_argument_attr = Some(o),
Err(e) => errors.push(e),
}
if let Err(e) = field_or_argument_attr.parse_attr(attr) {
errors.push(e)
}

false
Expand Down

0 comments on commit b4c9620

Please sign in to comment.