Function timrs_macro_utils::parse::accumulate_while
source · pub fn accumulate_while<F>(
cursor: Cursor<'_>,
predicate: F
) -> Result<(TokenStream, Cursor<'_>)>
Expand description
Accumulate proc_macro2::TokenTree
into a proc_macro2::TokenStream
while the given predicate returns true
returning a tuple containing the resulting proc_macro2::TokenStream
and a syn::buffer::Cursor
positioned at the point where accumulation ended.
§Examples
use core::str::FromStr;
use proc_macro2::TokenStream;
use syn::{
parse::{Parse, ParseStream},
parse2, Result as SynResult,
};
use timrs_macro_utils::parse::accumulate_while;
#[derive(Debug, PartialEq)]
struct Test { value: String }
impl Parse for Test {
fn parse(input: ParseStream) -> SynResult<Self> {
SynResult::Ok(Self {
value: input
.step(|cursor| accumulate_while(*cursor, |current| !current.eof()))?
.to_string(),
})
}
}
assert_eq!(
parse2::<Test>(
TokenStream::from_str("ARBITRARY_TOKEN").map_err(|error| error.to_string()).unwrap()
).map_err(|error| error.to_string()).unwrap(),
Test { value: "ARBITRARY_TOKEN".to_owned() }
);
§Errors
Returns the syn::Error
if the cursor hits eof
before the predicate returns false
.