test_case_core/
modifier.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::collections::HashSet;
use std::fmt::{Debug, Formatter};
use syn::parse::{Parse, ParseStream};
use syn::token::Bracket;
use syn::{bracketed, parse_quote, Attribute, LitStr};

mod kw {
    syn::custom_keyword!(inconclusive);
    syn::custom_keyword!(ignore);
}

#[derive(Clone, PartialEq, Eq, Hash)]
pub enum Modifier {
    Inconclusive,
    InconclusiveWithReason(LitStr),
}

impl Debug for Modifier {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Modifier::Inconclusive | Modifier::InconclusiveWithReason(_) => {
                write!(f, "inconclusive")
            }
        }
    }
}

impl Parse for Modifier {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        if input.peek(kw::inconclusive) {
            let _: kw::inconclusive = input.parse()?;
            Self::parse_inconclusive(input)
        } else if input.peek(kw::ignore) {
            let _: kw::ignore = input.parse()?;
            Self::parse_inconclusive(input)
        } else {
            Err(syn::Error::new(input.span(), "unknown modifier keyword"))
        }
    }
}

impl Modifier {
    pub fn parse_inconclusive(input: ParseStream) -> syn::Result<Self> {
        if input.peek(Bracket) {
            let content;
            let _: Bracket = bracketed!(content in input);
            let reason: LitStr = content.parse()?;
            Ok(Self::InconclusiveWithReason(reason))
        } else {
            Ok(Self::Inconclusive)
        }
    }

    pub fn attribute(&self) -> Attribute {
        match self {
            Modifier::Inconclusive => parse_quote! { #[ignore] },
            Modifier::InconclusiveWithReason(r) => parse_quote! { #[ignore = #r] },
        }
    }
}

pub fn parse_kws(input: ParseStream) -> HashSet<Modifier> {
    let mut kws = HashSet::new();
    while let Ok(kw) = Modifier::parse(input) {
        kws.insert(kw);
    }
    kws
}