script-macro

An experimental way to write simple proc-macros inline with other source code.

Did you ever end up getting frustrated at the boilerplate involved in writing proc macros, and wished you could just write a Python or Bash script to generate the code instead?

```rust pub fn add(left: usize, right: usize) -> usize { left + right }

[cfg(test)]

mod tests { use super::*;

#[script_macro::run_script_on(r##"
    let output = item;

    for x in 0..10 {
        for y in 0..10 {
            output += `
            #[test]
            fn it_works_${x}_${y}() {
                it_works(${x}, ${y}, ${x + y});
            }`;
        }
    }

    return output;
"##)]
fn it_works(x: usize, y: usize, out: usize) {
    assert_eq!(add(x, y), out);
}

} ```

Macros are not Rust source code, instead they are written in the RHAI scripting language. This comes with advantages and disadvantages:

Seriously?

I seriously do wish that proc_macros were easier to write (inline with other code) and didn't contribute as much to compile time. One area where this comes up for me particularly often is programmatic test generation (or, parametrization).

This is my best shot at making this happen today, but that doesn't mean I'm convinced that the end result is viable for production use. I hope that it inspires somebody else to build something better.

API

There are two main macros to choose from:

Script API

From within the script, the entire stdlib of RHAI + the functions in rhai-fs are available.

Additionally, the following functions are defined:

Examples

Check out the example crates to see all of the above in action.

License

Licensed under the MIT, see ./LICENSE.