syn_helpers/
lists_and_arguments.rs

1use std::fmt::Debug;
2
3use proc_macro2::Ident;
4use syn::{
5    parse::{self, Parse},
6    punctuated::Punctuated,
7    Token,
8};
9
10/// A comma separated list of `T`
11pub struct CommaSeparatedList<T>(pub Punctuated<T, Token![,]>);
12
13impl<T: Parse> Parse for CommaSeparatedList<T> {
14    fn parse(input: parse::ParseStream) -> syn::Result<Self> {
15        input.parse_terminated(T::parse, Token![,]).map(Self)
16    }
17}
18
19impl<T> IntoIterator for CommaSeparatedList<T> {
20    type Item = T;
21    type IntoIter = syn::punctuated::IntoIter<T>;
22
23    fn into_iter(self) -> Self::IntoIter {
24        self.0.into_iter()
25    }
26}
27
28/// A argument with a name and a value. '=' separated, e.g. `name=AST`
29#[derive(Debug)]
30pub struct Argument<T>(pub String, pub T);
31
32impl<T: Parse> Parse for Argument<T> {
33    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
34        let ident = input.parse::<Ident>()?.to_string();
35        input.parse::<Token![=]>()?;
36        Ok(Self(ident, input.parse()?))
37    }
38}
39
40pub type CommaSeparatedArgumentList<T> = CommaSeparatedList<Argument<T>>;