Organize how the caller determines which attrs to parse

In preparation for parsing even more attributes, such as `repr`.
This commit is contained in:
David Tolnay 2020-05-10 14:29:30 -07:00
parent e86b9cf225
commit b129ea7163
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
2 changed files with 28 additions and 13 deletions

View File

@ -1,27 +1,36 @@
use crate::syntax::{Derive, Doc};
use proc_macro2::Ident;
use syn::parse::{ParseStream, Parser};
use syn::parse::{ParseStream, Parser as _};
use syn::{Attribute, Error, LitStr, Path, Result, Token};
#[derive(Default)]
pub struct Parser<'a> {
pub doc: Option<&'a mut Doc>,
pub derives: Option<&'a mut Vec<Derive>>,
}
pub(super) fn parse_doc(attrs: &[Attribute]) -> Result<Doc> {
let mut doc = Doc::new();
let derives = None;
parse(attrs, &mut doc, derives)?;
parse(
attrs,
Parser {
doc: Some(&mut doc),
..Parser::default()
},
)?;
Ok(doc)
}
pub(super) fn parse(
attrs: &[Attribute],
doc: &mut Doc,
mut derives: Option<&mut Vec<Derive>>,
) -> Result<()> {
pub(super) fn parse(attrs: &[Attribute], mut parser: Parser) -> Result<()> {
for attr in attrs {
if attr.path.is_ident("doc") {
let lit = parse_doc_attribute.parse2(attr.tokens.clone())?;
doc.push(lit);
continue;
if let Some(doc) = &mut parser.doc {
let lit = parse_doc_attribute.parse2(attr.tokens.clone())?;
doc.push(lit);
continue;
}
} else if attr.path.is_ident("derive") {
if let Some(derives) = &mut derives {
if let Some(derives) = &mut parser.derives {
derives.extend(attr.parse_args_with(parse_derive_attribute)?);
continue;
}

View File

@ -56,7 +56,13 @@ fn parse_struct(item: ItemStruct) -> Result<Api> {
let mut doc = Doc::new();
let mut derives = Vec::new();
attrs::parse(&item.attrs, &mut doc, Some(&mut derives))?;
attrs::parse(
&item.attrs,
attrs::Parser {
doc: Some(&mut doc),
derives: Some(&mut derives),
},
)?;
let fields = match item.fields {
Fields::Named(fields) => fields,