genco 0.10.0

Genco, the even simpler code generator for Rust.
Documentation

Build Status crates.io docs.rs

genco

genco is a code generator and quasi quoter for Rust, written for use in reproto.

The workhorse of genco is the quote! and quote_in! macros. While tokens can be constructed manually, these make this process much easier.

genco only minimally deals with language-specific syntax, but primarily deals with solving the following:

  • Imports — genco generates and groups import statements according to conventions for the language being generated for.

  • String Quoting — Strings can be quoted in a language specific way either by including them literally in the token stream using quote!("hello"), or by through the quoted() function.

  • Structural Indentation — genco's quasi quoting utilizes whitespace detection to structurally sort out spaces and indentation.

  • Language Customization — Building support for a custom language is straight forward with the impl_lang! macro.

We depend on proc_macro_hygiene stabilizations. Until then, you must build and run with the nightly branch.

cargo +nightly run --example rust

Examples

The following are language specific examples for genco using the quote! macro.

You can run one of the examples above using:

cargo run --example go

Rust Example

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

use genco::prelude::*;
use genco::fmt;

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

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

let stdout = std::io::stdout();
let mut w = fmt::IoWriter::new(stdout.lock());

let fmt = fmt::Config::from_lang::<Rust>()
    .with_indentation(fmt::Indentation::Space(2));
let config = rust::Config::default();

tokens.format_file(&mut w.as_formatter(fmt), &config)?;

This would produce:

use std::collections::HashMap;

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