[][src]Crate 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.


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::imported("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);
}

Modules

fmt

Code formatting utilities.

lang

Language specialization for genco

prelude

Prelude containing typical things to import when using the library.

tokens

Utilities for working with token streams.

Macros

quote

Language neutral whitespace sensitive quasi-quoting.

quote_in

Behaves the same as quote! while quoting into an existing token stream with <target> => <quoted>.

Structs

Tokens

A stream of tokens.