Skip to content

Commit

Permalink
Support parsing Field in parse_quote
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Dec 12, 2023
1 parent 2ab0f6a commit c268c67
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/parse_quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl<T: Parse> ParseQuote for T {

use crate::punctuated::Punctuated;
#[cfg(any(feature = "full", feature = "derive"))]
use crate::{attr, Attribute};
use crate::{attr, Attribute, Field, FieldMutability, Ident, Type, Visibility};
#[cfg(feature = "full")]
use crate::{Block, Pat, Stmt};

Expand All @@ -151,6 +151,36 @@ impl ParseQuote for Attribute {
}
}

#[cfg(any(feature = "full", feature = "derive"))]
impl ParseQuote for Field {
fn parse(input: ParseStream) -> Result<Self> {
let attrs = input.call(Attribute::parse_outer)?;
let vis: Visibility = input.parse()?;

let ident: Option<Ident>;
let colon_token: Option<Token![:]>;
let is_named = input.peek(Ident) && input.peek2(Token![:]) && !input.peek2(Token![::]);
if is_named {
ident = Some(input.parse()?);
colon_token = Some(input.parse()?);
} else {
ident = None;
colon_token = None;
}

let ty: Type = input.parse()?;

Ok(Field {
attrs,
vis,
mutability: FieldMutability::None,
ident,
colon_token,
ty,
})
}
}

#[cfg(feature = "full")]
impl ParseQuote for Pat {
fn parse(input: ParseStream) -> Result<Self> {
Expand Down

0 comments on commit c268c67

Please sign in to comment.