#template #code-generation

genco

A whitespace-aware quasiquoter for beautiful code generation

93 releases (14 breaking)

new 0.15.1 Oct 13, 2020
0.15.0 Jun 27, 2020
0.14.2 Jun 21, 2020
0.3.30 Jun 1, 2019
0.1.11 Nov 22, 2017

#29 in Template engine

Download history 182/week @ 2020-06-27 105/week @ 2020-07-04 205/week @ 2020-07-11 103/week @ 2020-07-18 48/week @ 2020-07-25 2/week @ 2020-08-01 206/week @ 2020-08-08 198/week @ 2020-08-15 14/week @ 2020-08-22 200/week @ 2020-08-29 287/week @ 2020-09-05 12/week @ 2020-09-12 11/week @ 2020-09-19 104/week @ 2020-09-26 13/week @ 2020-10-03 31/week @ 2020-10-10

435 downloads per month
Used in 17 crates (14 directly)

MIT/Apache

190KB
3.5K SLoC

Build Status crates.io docs.rs

genco

A whitespace-aware quasiquoter for beautiful code generation.

Central to genco are the quote! and quote_in! procedural macros which ease the construction of token streams.

This project solves the following language-specific concerns:

  • Imports — Generates and groups import statements as they are used. So you only import what you use, with no redundancy. We also do our best to solve namespace conflicts.

  • String Quoting — genco knows how to quote strings. And can even interpolate values into the quoted string if it's supported by the language.

  • Structural Indentation — The quoter relies on intuitive whitespace detection to structurally sort out spacings and indentation. Allowing genco to generate beautiful readable code with minimal effort. This is also a requirement for generating correctly behaving code in languages like Python where indentation is meaningful.

  • Language Customization — Building support for new languages is a piece of cake with the help of the impl_lang! macro.


To do whitespace detection, we depend on the nightly proc_macro_span feature.

Until this is stabilized, you must build and run projects using genco with a nightly compiler.

cargo +nightly run --example rust

Supported Languages

The following are languages which have built-in support in genco.

Is your favorite language missing? Open an issue!

You can run one of the examples by:

cargo +nightly run --example rust

Rust Example

The following is a simple program producing Rust code to stdout with custom configuration:

use genco::prelude::*;

let hash_map = rust::import("std::collections", "HashMap");

let tokens: rust::Tokens = quote! {
    fn main() {
        let mut m = #hash_map::new();
        m.insert(1u32, 2u32);
    }
};

println!("{}", tokens.to_file_string()?);

This would produce:

use std::collections::HashMap;

fn main() {
    let mut m = HashMap::new();
    m.insert(1u32, 2u32);
}

Dependencies

~0.6–1MB
~24K SLoC