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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use crate::target::Target;
use syn::{
    parse_quote, spanned::Spanned, visit_mut::VisitMut, Attribute, Block, Error, Lit, Meta,
    MetaNameValue, NestedMeta, Result,
};

pub(crate) fn process_target_cfg(target: Option<Target>, block: &mut Block) -> Result<()> {
    let mut visitor = ReplaceTargetCfg {
        target,
        result: Ok(()),
    };
    visitor.visit_block_mut(block);
    visitor.result
}

struct ReplaceTargetCfg {
    target: Option<Target>,
    result: Result<()>,
}

impl ReplaceTargetCfg {
    fn target_cfg_value(&self, nested: &NestedMeta) -> Result<bool> {
        match nested {
            NestedMeta::Meta(meta) => match meta {
                Meta::Path(path) => Err(Error::new(path.span(), "unexpected path")),
                Meta::NameValue(MetaNameValue { path, lit, .. }) => {
                    if path.is_ident("target") {
                        if let Lit::Str(s) = lit {
                            let test_target = Some(Target::parse(s)?);
                            Ok(test_target == self.target)
                        } else {
                            Err(Error::new(lit.span(), "expected string literal"))
                        }
                    } else {
                        Err(Error::new(path.span(), "unknown key"))
                    }
                }
                Meta::List(list) => {
                    if list.path.is_ident("not") {
                        if list.nested.len() != 1 {
                            return Err(Error::new(
                                list.nested.span(),
                                "expected a single target_cfg predicate",
                            ));
                        }
                        self.target_cfg_value(list.nested.first().unwrap())
                            .map(|v| !v)
                    } else if list.path.is_ident("any") {
                        for v in list.nested.iter() {
                            if self.target_cfg_value(v)? {
                                return Ok(true);
                            }
                        }
                        Ok(false)
                    } else if list.path.is_ident("all") {
                        for v in list.nested.iter() {
                            if !self.target_cfg_value(v)? {
                                return Ok(false);
                            }
                        }
                        Ok(true)
                    } else {
                        Err(Error::new(
                            list.path.span(),
                            "expected `not`, `any`, or `all`",
                        ))
                    }
                }
            },
            NestedMeta::Lit(lit) => Err(Error::new(lit.span(), "unexpected literal")),
        }
    }
}

impl VisitMut for ReplaceTargetCfg {
    fn visit_attribute_mut(&mut self, i: &mut Attribute) {
        if let Ok(Meta::List(list)) = i.parse_meta() {
            if list.path.is_ident("target_cfg") {
                self.result = self.result.clone().and_then(|_| {
                    if list.nested.len() != 1 {
                        return Err(Error::new(
                            list.nested.span(),
                            "expected a single target_cfg predicate",
                        ));
                    }
                    *i = if self.target_cfg_value(list.nested.first().unwrap())? {
                        parse_quote! { #[cfg(not(any()))] }
                    } else {
                        parse_quote! { #[cfg(any())] }
                    };
                    Ok(())
                })
            }
        }
    }
}