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
extern crate proc_macro;

use proc_macro::{Ident, TokenStream, TokenTree};

#[cfg(feature = "ams")]
mod ams;
mod ams_prefix;
#[cfg(feature = "muts")]
mod muts;
#[cfg(feature = "clones")]
mod clones;
mod clones_prefix;
#[cfg(feature = "pubimpl")]
mod pubimpl;

#[proc_macro]
#[cfg(feature = "ams")]
pub fn ams(input: TokenStream) -> TokenStream {
    use crate::ams::*;
    let ids = parse_muts(input.into_iter());
    TokenStream::from_iter(get_ams_(ids, false))
}

#[proc_macro]
#[cfg(feature = "muts")]
pub fn muts(input: TokenStream) -> TokenStream {
    use crate::muts::*;
    let (ids, ids_len, vals, vals_len) = muts_get_ids_vals_(&mut input.into_iter());
    assert_eq!(ids_len, vals_len, "The number of idents and values must be the same");
    TokenStream::from_iter(get_muts_(ids, vals))
}

#[proc_macro]
#[cfg(feature = "clones")]
pub fn clones(input: TokenStream) -> TokenStream {
    use crate::clones::*;
    let ids = parse_muts(input.into_iter());
    TokenStream::from_iter(get_clones_(ids, false))
}

#[proc_macro]
#[cfg(feature = "mutclones")]
pub fn mutclones(input: TokenStream) -> TokenStream {
    use crate::clones::*;
    let ids = parse_muts(input.into_iter());
    TokenStream::from_iter(get_clones_(ids, true))
}

#[proc_macro]
#[cfg(feature = "pubimpl")]
pub fn pubimpl(input: TokenStream) -> TokenStream {
    use crate::pubimpl::*;
    let impl_ = pubimpl_parse_impl_(input.into_iter());
    TokenStream::from_iter(impl_)
}

// IM = (ident -> i, mutability -> m).to_uppercase();
pub(crate) type IM = (Ident, bool);

pub(crate) fn parse_muts<I>(iter: I) -> Vec::<IM>
where I: Iterator<Item = TokenTree>
{
    iter.fold((false, Vec::new()), |(next, mut ret), t| {
        if let TokenTree::Ident(ident) = t {
            if next {
                ret.push((ident, true));
                (false, ret)
            } else if ident.to_string().eq("mut") {
                (true, ret)
            } else {
                ret.push((ident, false));
                (false, ret)
            }
        } else {
            if next { panic!("Expected identifier after `mut`") }
            (false, ret)
        }
    }).1
}