Macro parsel::try_parse_quote
source · [−]macro_rules! try_parse_quote {
($($tt:tt)*) => { ... };
}
Expand description
Similar to syn::parse_quote!
, but instead of panicking, it returns an
Err
if the inferred type fails to parse from the specified token stream.
use core::iter::FromIterator;
use parsel::{try_parse_quote, Result};
use parsel::ast::{Lit, Many};
fn try_parse_literals(bit: bool, number: u64) -> Result<Many<Lit>> {
let ast: Many<Lit> = try_parse_quote!(#bit "some text" #number);
Ok(ast)
}
let actual: Many<Lit> = try_parse_literals(true, 76192)?;
let expected: Many<Lit> = Many::from_iter([
Lit::from(true),
Lit::from("some text"),
Lit::from(76192_u128),
]);
assert_eq!(actual, expected);