Brian Silverman | 4e662aa | 2022-05-11 23:10:19 -0700 | [diff] [blame^] | 1 | // Copyright 2021 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your |
| 6 | // option. This file may not be copied, modified, or distributed |
| 7 | // except according to those terms. |
| 8 | |
| 9 | use proc_macro2::Ident; |
| 10 | use quote::ToTokens; |
| 11 | use syn::Result as ParseResult; |
| 12 | use syn::{ |
| 13 | parse::{Parse, ParseStream}, |
| 14 | token::Comma, |
| 15 | }; |
| 16 | |
| 17 | #[derive(Default)] |
| 18 | pub struct SubclassAttrs { |
| 19 | pub self_owned: bool, |
| 20 | pub superclass: Option<String>, |
| 21 | } |
| 22 | |
| 23 | impl Parse for SubclassAttrs { |
| 24 | fn parse(input: ParseStream) -> ParseResult<Self> { |
| 25 | let mut me = Self::default(); |
| 26 | let mut id = input.parse::<Option<Ident>>()?; |
| 27 | while id.is_some() { |
| 28 | match id { |
| 29 | Some(id) if id == "self_owned" => me.self_owned = true, |
| 30 | Some(id) if id == "superclass" => { |
| 31 | let args; |
| 32 | syn::parenthesized!(args in input); |
| 33 | let superclass: syn::LitStr = args.parse()?; |
| 34 | if me.superclass.is_some() { |
| 35 | return Err(syn::Error::new_spanned( |
| 36 | id.into_token_stream(), |
| 37 | "Expected single superclass specification", |
| 38 | )); |
| 39 | } |
| 40 | me.superclass = Some(superclass.value()); |
| 41 | } |
| 42 | Some(id) => { |
| 43 | return Err(syn::Error::new_spanned( |
| 44 | id.into_token_stream(), |
| 45 | "Expected self_owned or superclass", |
| 46 | )) |
| 47 | } |
| 48 | None => {} |
| 49 | }; |
| 50 | let comma = input.parse::<Option<Comma>>()?; |
| 51 | if comma.is_none() { |
| 52 | break; |
| 53 | } |
| 54 | id = input.parse::<Option<Ident>>()?; |
| 55 | } |
| 56 | Ok(me) |
| 57 | } |
| 58 | } |