genco 0.5.0-alpha.1

Even simpler code generation for Rust.
Documentation

GenCo

Build Status crates.io

GenCo is an even simpler code generator for Rust, specifically written for use in reproto.

It does not deal with language-specific syntax, instead it can do some of the basic necessities through specialization.

  • Handle imports, if needed.
  • Quote strings according to language convention.
  • Indents and spaces your code according to simple rules.

Examples

Language Support

This section contains example code for some of the supported languages.

For more information, see docs.rs/genco.

Dart

Simple support for importing names.

#[macro_use]
extern crate genco;

fn main() {
    use genco::dart::imported;

    let m = imported("dart:math").alias("m");
    let sqrt = m.name("sqrt");

    let mut t = toks!();
    t.push("void main() {");
    t.nested({
        let mut body = toks!();
        body.push(toks!("print(", "The Square Root Is:".quoted(), " + ", sqrt, "(42));"));
        body
    });
    t.push("}");
}

Indentation Rules

The quote! macro has the following rules for dealing with indentation and spacing.

Two tokens that are separated, are spaced. Regardless of how many spaces there are between them.

So:

quote!(fn   test());

Becomes:

fn test()

More that two line breaks are collapsed.

So:

quote! {
    fn test() {
        println!("Hello...");


        println!("... World!");
    }
}

Becomes:

fn test() {
    println!("Hello...");

    println!("... World!");
}

Indentation is determined on a row-by-row basis. If a column is further in than the one on the preceeding row, it is indented one level deeper.

Like wise if a column starts before the previous rows column, it is indended one level shallower.

So:

quote! {
  fn test() {
      println!("Hello...");
      println!("... World!");
    }
}

Becomes:

fn test() {
    println!("Hello...");
    println!("... World!");
}