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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use core::panic;
use std::path;

extern crate proc_macro;

struct ModuleConfig {
    pub pub_keyword: bool,
    pub mod_keyword: bool,

    pub directory: String,
    pub entry: String,
}

fn parse_modules(config: ModuleConfig, modules: Vec<String>) -> Vec<String> {
    let mut parsed_modules = Vec::new();

    for module in modules {
        let mut parsed_module = String::new();

        let module = module.replace(&config.directory, "");
        let module = module.replace(".rs", "");

        let module = module.replace(path::MAIN_SEPARATOR_STR, "::");

        if module.len() == 0 {
            continue;
        }

        if config.pub_keyword == true {
            parsed_module.push_str("pub ");
        }

        if config.mod_keyword == true {
            parsed_module.push_str("mod ");
        }

        parsed_module.push_str(&module);

        if config.entry.len() > 0 {
            parsed_module.push_str("::");
            parsed_module.push_str(&config.entry);
        }

        parsed_module.push_str(";");

        parsed_modules.push(parsed_module);
    }

    parsed_modules
}

fn find_modules(
    directory: &String,
    pattern: &fancy_regex::Regex,
) -> Result<Vec<String>, fancy_regex::Error> {
    let mut modules = Vec::new();

    for path in std::fs::read_dir(directory).unwrap() {
        let path = path.unwrap().path();

        let path = path.to_str().unwrap();

        if pattern.is_match(path).unwrap() {
            modules.push(path.to_string());
        }
    }

    Ok(modules)
}

fn parse_parameters(input: String) -> Vec<String> {
    let mut parameters = Vec::new();

    let mut parameter = String::new();

    let mut is_escape = false;
    let mut is_string = false;

    for c in input.chars() {
        if c == ',' && is_string == false {
            parameters.push(parameter.clone());
            parameter.clear();
        } else if c == ' ' && is_string == false {
            continue;
        } else if c == '"' && is_escape == false {
            is_string = !is_string;
        } else if c == '\\' && is_escape == false && is_string == true {
            is_escape = true;
        } else if is_string {
            parameter.push(c);
            is_escape = false;
        } else {
            panic!("Invalid character: {}", c);
        }
    }

    parameters.push(parameter);
    parameters
}

fn module_handler(
    pub_keyword: bool, mod_keyword: bool, mut directory: String,
    pattern: String, entry: String,
)
    -> proc_macro::TokenStream
{
    if directory.ends_with("/") == false {
        directory.push_str(path::MAIN_SEPARATOR_STR);
    }

    let pattern = fancy_regex::Regex::new(&pattern).unwrap();

    let modules = find_modules(&directory, &pattern).unwrap();

    let module_config = ModuleConfig {
        pub_keyword, mod_keyword,
        directory, entry,
    };

    let modules = parse_modules(module_config, modules);
    let output = modules.join("\n");

    output.parse().unwrap()
}

/// #### Description
/// 
/// Import public modules from a directory.
/// 
/// #### Example
/// 
/// ```rust, ignore
/// use import_modules::import_pub_modules;
/// 
/// import_pub_modules!("src", "^((?!mod.rs).)*$");
/// ```
/// 
/// #### Returns
/// 
/// ```rust, ignore
/// pub mod module1;
/// pub mod moduleN;
/// ```
/// 
#[proc_macro]
pub fn import_pub_modules(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let input = input.to_string();

    let parameters = parse_parameters(input);

    if parameters.len() != 2 {
        panic!("Invalid number of parameters: {}", parameters.len());
    }

    module_handler(
        true, true, parameters[0].clone(), parameters[1].clone(), "".to_string(),
    )
}

/// #### Description
/// 
/// Import modules from a directory.
///
/// #### Example
/// 
/// ```rust, ignore
/// use import_modules::import_modules;
/// 
/// import_modules!("src", "^((?!mod.rs).)*$");
/// ```
/// 
/// #### Returns
/// 
/// ```rust, ignore
/// mod module1;
/// mod moduleN;
/// ```
/// 
#[proc_macro]
pub fn import_modules(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let input = input.to_string();

    let parameters = parse_parameters(input);

    if parameters.len() != 2 {
        panic!("Invalid number of parameters: {}", parameters.len());
    }

    module_handler(
        false, true, parameters[0].clone(), parameters[1].clone(), "".to_string(),
    )
}

/// #### Description
/// 
/// Import modules from a directory on function scope.
/// 
/// #### Example
/// 
/// ```rust, ignore
/// use import_modules::{import_modules, import_scope_modules};
/// 
/// import_modules!("src", "^((?!mod.rs).)*$");
/// 
/// fn main() {
///    import_scope_modules!("src", "^((?!mod.rs).)*$", "function()");
/// }
/// ```
/// 
/// #### Returns
/// 
/// ```rust, ignore
/// mod module1;
/// mod moduleN;
/// 
/// fn main() {
///    module1::function();
///    moduleN::function();
/// }
/// ```
/// 
#[proc_macro]
pub fn import_scope_modules(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let input = input.to_string();

    let parameters = parse_parameters(input);

    if parameters.len() != 3 {
        panic!("Invalid number of parameters: {}", parameters.len());
    }

    module_handler(
        false, false, parameters[0].clone(), parameters[1].clone(), parameters[2].clone(),
    )
}