Skip to content

Commit b129ea7

Browse files
committed
Organize how the caller determines which attrs to parse
In preparation for parsing even more attributes, such as `repr`.
1 parent e86b9cf commit b129ea7

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

syntax/attrs.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
11
use crate::syntax::{Derive, Doc};
22
use proc_macro2::Ident;
3-
use syn::parse::{ParseStream, Parser};
3+
use syn::parse::{ParseStream, Parser as _};
44
use syn::{Attribute, Error, LitStr, Path, Result, Token};
55

6+
#[derive(Default)]
7+
pub struct Parser<'a> {
8+
pub doc: Option<&'a mut Doc>,
9+
pub derives: Option<&'a mut Vec<Derive>>,
10+
}
11+
612
pub(super) fn parse_doc(attrs: &[Attribute]) -> Result<Doc> {
713
let mut doc = Doc::new();
8-
let derives = None;
9-
parse(attrs, &mut doc, derives)?;
14+
parse(
15+
attrs,
16+
Parser {
17+
doc: Some(&mut doc),
18+
..Parser::default()
19+
},
20+
)?;
1021
Ok(doc)
1122
}
1223

13-
pub(super) fn parse(
14-
attrs: &[Attribute],
15-
doc: &mut Doc,
16-
mut derives: Option<&mut Vec<Derive>>,
17-
) -> Result<()> {
24+
pub(super) fn parse(attrs: &[Attribute], mut parser: Parser) -> Result<()> {
1825
for attr in attrs {
1926
if attr.path.is_ident("doc") {
20-
let lit = parse_doc_attribute.parse2(attr.tokens.clone())?;
21-
doc.push(lit);
22-
continue;
27+
if let Some(doc) = &mut parser.doc {
28+
let lit = parse_doc_attribute.parse2(attr.tokens.clone())?;
29+
doc.push(lit);
30+
continue;
31+
}
2332
} else if attr.path.is_ident("derive") {
24-
if let Some(derives) = &mut derives {
33+
if let Some(derives) = &mut parser.derives {
2534
derives.extend(attr.parse_args_with(parse_derive_attribute)?);
2635
continue;
2736
}

syntax/parse.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ fn parse_struct(item: ItemStruct) -> Result<Api> {
5656

5757
let mut doc = Doc::new();
5858
let mut derives = Vec::new();
59-
attrs::parse(&item.attrs, &mut doc, Some(&mut derives))?;
59+
attrs::parse(
60+
&item.attrs,
61+
attrs::Parser {
62+
doc: Some(&mut doc),
63+
derives: Some(&mut derives),
64+
},
65+
)?;
6066

6167
let fields = match item.fields {
6268
Fields::Named(fields) => fields,

0 commit comments

Comments
 (0)