from_attr_core/
lib.rs

1use syn::{
2    braced, bracketed,
3    parse::{Parse, ParseStream},
4    punctuated::Punctuated,
5    Token,
6};
7
8pub struct Array<T> {
9    pub elems: Punctuated<T, Token![,]>,
10}
11
12impl<T: Parse> Parse for Array<T> {
13    fn parse(input: ParseStream) -> syn::Result<Self> {
14        let content;
15        bracketed!(content in input);
16
17        Ok(Self {
18            elems: Punctuated::<T, Token![,]>::parse_terminated(&content)?,
19        })
20    }
21}
22
23impl<T> Array<T> {
24    pub fn is_empty(&self) -> bool {
25        self.elems.is_empty()
26    }
27}
28
29pub struct Pair<K, V> {
30    pub key: K,
31    pub colon_token: Token![:],
32    pub value: V,
33}
34
35impl<K: Parse, V: Parse> Parse for Pair<K, V> {
36    fn parse(input: ParseStream) -> syn::Result<Self> {
37        Ok(Self {
38            key: input.parse()?,
39            colon_token: input.parse()?,
40            value: input.parse()?,
41        })
42    }
43}
44
45pub struct Map<K, V> {
46    pub pairs: Punctuated<Pair<K, V>, Token![,]>,
47}
48
49impl<K: Parse, V: Parse> Parse for Map<K, V> {
50    fn parse(input: ParseStream) -> syn::Result<Self> {
51        let content;
52        braced!(content in input);
53
54        Ok(Self {
55            pairs: Punctuated::<Pair<K, V>, Token![,]>::parse_terminated(&content)?,
56        })
57    }
58}