Skip to content

Commit 67d331b

Browse files
committed
refactor: transition #[sea_orm_model(..)] to nested #[sea_orm(model_attrs(..))] syntax
1 parent 99a9080 commit 67d331b

File tree

1 file changed

+40
-17
lines changed

1 file changed

+40
-17
lines changed

sea-orm-macros/src/derives/model_ex.rs

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,50 @@ pub fn expand_sea_orm_model(input: ItemStruct, compact: bool) -> syn::Result<Tok
2020
let mut model_ex_attrs: Vec<Attribute> = Vec::new();
2121

2222
for attr in input.attrs {
23-
let is_model = attr.path().is_ident("sea_orm_model");
24-
let is_model_ex = attr.path().is_ident("sea_orm_model_ex");
25-
if is_model || is_model_ex {
26-
attr.parse_nested_meta(|meta| {
23+
if !attr.path().is_ident("sea_orm") {
24+
model_attrs.push(attr.clone());
25+
model_ex_attrs.push(attr);
26+
continue;
27+
}
28+
29+
let mut other_attrs = Punctuated::<Meta, Comma>::new();
30+
31+
attr.parse_nested_meta(|meta| {
32+
let is_model = meta.path.is_ident("model_attrs");
33+
let is_model_ex = meta.path.is_ident("model_ex_attrs");
34+
35+
if is_model || is_model_ex {
36+
let content;
37+
syn::parenthesized!(content in meta.input);
38+
use syn::parse::Parse;
39+
let nested_metas = content.parse_terminated(Meta::parse, Comma)?;
40+
for m in nested_metas {
41+
let new_attr: Attribute = parse_quote!( #[#m] );
42+
if is_model {
43+
model_attrs.push(new_attr);
44+
} else {
45+
model_ex_attrs.push(new_attr);
46+
}
47+
}
48+
} else {
2749
let path = &meta.path;
28-
let new_attr: Attribute = if meta.input.peek(syn::token::Paren) {
50+
if meta.input.peek(syn::Token![=]) {
51+
let value: Expr = meta.value()?.parse()?;
52+
other_attrs.push(parse_quote!( #path = #value ));
53+
} else if meta.input.is_empty() || meta.input.peek(Comma) {
54+
other_attrs.push(parse_quote!( #path ));
55+
} else {
2956
let content;
3057
syn::parenthesized!(content in meta.input);
31-
let inner: TokenStream = content.parse()?;
32-
parse_quote!( #[#path(#inner)] )
33-
} else {
34-
parse_quote!( #[#path] )
35-
};
36-
if is_model {
37-
model_attrs.push(new_attr);
38-
} else {
39-
model_ex_attrs.push(new_attr);
58+
let tokens: TokenStream = content.parse()?;
59+
other_attrs.push(parse_quote!( #path(#tokens) ));
4060
}
41-
Ok(())
42-
})?;
43-
} else {
61+
}
62+
Ok(())
63+
})?;
64+
65+
if !other_attrs.is_empty() {
66+
let attr: Attribute = parse_quote!( #[sea_orm(#other_attrs)] );
4467
model_attrs.push(attr.clone());
4568
model_ex_attrs.push(attr);
4669
}

0 commit comments

Comments
 (0)