mirror of
https://gitee.com/openharmony/third_party_rust_cxx
synced 2024-11-24 07:40:19 +00:00
Organize how the caller determines which attrs to parse
In preparation for parsing even more attributes, such as `repr`.
This commit is contained in:
parent
e86b9cf225
commit
b129ea7163
@ -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;
|
||||
}
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user