1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use proc_macro2::Ident;
use quote::ToTokens;
use syn::Result as ParseResult;
use syn::{
    parse::{Parse, ParseStream},
    token::Comma,
};

#[derive(Default)]
pub struct SubclassAttrs {
    pub self_owned: bool,
    pub superclass: Option<String>,
}

impl Parse for SubclassAttrs {
    fn parse(input: ParseStream) -> ParseResult<Self> {
        let mut me = Self::default();
        let mut id = input.parse::<Option<Ident>>()?;
        while id.is_some() {
            match id {
                Some(id) if id == "self_owned" => me.self_owned = true,
                Some(id) if id == "superclass" => {
                    let args;
                    syn::parenthesized!(args in input);
                    let superclass: syn::LitStr = args.parse()?;
                    if me.superclass.is_some() {
                        return Err(syn::Error::new_spanned(
                            id.into_token_stream(),
                            "Expected single superclass specification",
                        ));
                    }
                    me.superclass = Some(superclass.value());
                }
                Some(id) => {
                    return Err(syn::Error::new_spanned(
                        id.into_token_stream(),
                        "Expected self_owned or superclass",
                    ))
                }
                None => {}
            };
            let comma = input.parse::<Option<Comma>>()?;
            if comma.is_none() {
                break;
            }
            id = input.parse::<Option<Ident>>()?;
        }
        Ok(me)
    }
}