Skip to content

Commit

Permalink
Implement custom take function
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Oct 15, 2023
1 parent 4d126e6 commit b179798
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions typed-builder-macro/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{collections::HashSet, iter};

use proc_macro2::{Ident, Span, TokenStream};
use proc_macro2::{Ident, Span, TokenStream, TokenTree};
use quote::{format_ident, ToTokens};
use syn::{
parenthesized,
Expand Down Expand Up @@ -244,6 +244,20 @@ impl ToTokens for SubAttr {
}
}

fn take_until_comma(input: ParseStream) -> syn::Result<TokenStream> {
let mut stream = TokenStream::new();
while !input.is_empty() {
if input.peek(Token![,]) {
break;
}

let token = input.parse::<TokenTree>()?;
stream.extend(Some(token));
}

Ok(stream)
}

impl Parse for AttrArg {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
if input.peek(Token![!]) {
Expand All @@ -266,7 +280,7 @@ impl Parse for AttrArg {
Ok(Self::KeyValue(KeyValue {
name,
eq: input.parse()?,
value: input.parse()?, // This thing consumes beyond the punctuation separated boundaries?
value: take_until_comma(input)?,
}))
} else {
Err(input.error("expected !<ident>, <ident>=<value> or <ident>(…)"))
Expand Down

0 comments on commit b179798

Please sign in to comment.