1 use crate::{Error, Result}
;
2 use proc_macro2
::{Ident, Span, TokenStream, TokenTree}
;
4 macro_rules
! decl_settings
{
5 ($
($val
:expr
=> $variant
:ident
),+ $
(,)*) => {
7 pub(crate) enum Setting
{
11 fn ident_to_setting(ident
: Ident
) -> Result
<Setting
> {
12 match &*ident
.to_string() {
13 $
($val
=> Ok(Setting
::$variant
),)*
15 let possible_vals
= [$
($val
),*]
17 .map(|v
| format
!("`{}`", v
))
23 format
!("unknown setting `{}`, expected one of {}", ident
, possible_vals
)))
31 "assert_unwind_safe" => AssertUnwindSafe
,
32 "allow_not_macro" => AllowNotMacro
,
33 "proc_macro_hack" => ProcMacroHack
,
36 pub(crate) fn parse_settings(input
: TokenStream
) -> Result
<Settings
> {
37 let mut input
= input
.into_iter();
38 let mut res
= Settings(Vec
::new());
41 Some(TokenTree
::Ident(ident
)) => {
42 res
.0.push(ident_to_setting(ident
)?
);
44 None
=> return Ok(res
),
46 let span
= other
.map_or(Span
::call_site(), |tt
| tt
.span());
47 return Err(Error
::new(span
, "expected identifier".to_string()));
52 Some(TokenTree
::Punct(ref punct
)) if punct
.as_char() == '
,'
=> {}
53 None
=> return Ok(res
),
55 let span
= other
.map_or(Span
::call_site(), |tt
| tt
.span());
56 return Err(Error
::new(span
, "expected `,`".to_string()));
62 pub(crate) struct Settings(Vec
<Setting
>);
65 pub(crate) fn is_set(&self, setting
: Setting
) -> bool
{
66 self.0.iter
().any(|s
| *s
== setting
)
69 pub(crate) fn set(&mut self, setting
: Setting
) {