[][src]Crate genco

GenCo

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

The workhorse of GenCo is the quote! macro. While tokens can be constructed, manually, quote! makes this process much easier.

GenCo does not deal with language-specific syntax, instead it limits itself to do the following basic necessities through specialization:

  • Handle and collapse import statements.
  • Quote strings according to language convention.
  • Indents and spaces your code according to generic indentation rules.

Examples

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

The following is a simple example showcasing code generation for Rust.

#![feature(proc_macro_hygiene)]

use genco::rust::imported;
use genco::{quote, Rust, Tokens};

// Import the LittleEndian item, without referencing it through the last
// module component it is part of.
let little_endian = imported("byteorder", "LittleEndian").qualified();
let big_endian = imported("byteorder", "BigEndian");

// This is a trait, so only import it into the scope (unless we intent to
// implement it).
let write_bytes_ext = imported("byteorder", "WriteBytesExt").alias("_");

let tokens: Tokens<Rust> = quote! {
    @write_bytes_ext

    let mut wtr = vec![];
    wtr.write_u16::<#little_endian>(517).unwrap();
    wtr.write_u16::<#big_endian>(768).unwrap();
    assert_eq!(wtr, vec![5, 2, 3, 0]);
};

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:

#![feature(proc_macro_hygiene)]

let _: genco::Tokens<genco::Rust> = genco::quote!(fn   test() {});

Becomes:

fn test() {}

More that two line breaks are collapsed.

So:

#![feature(proc_macro_hygiene)]

let _: genco::Tokens<genco::Rust> = genco::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:

#![feature(proc_macro_hygiene)]

let _: genco::Tokens<genco::Rust> = genco::quote! {
  fn test() {
      println!("Hello...");
      println!("... World!");
    }
};

Becomes:

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

Modules

csharp

Specialization for Csharp code generation.

dart

Specialization for Dart code generation.

go

Specialization for Go code generation.

java

Specialization for Java code generation.

js

Specialization for JavaScript code generation.

prelude

Prelude to import.

python

Specialization for Python code generation.

rust

Specialization for Rust code generation.

swift

Specialization for Swift code generation.

Macros

nestedDeprecated

Helper macro to reduce boilerplate needed with nested token expressions.

pushDeprecated

Helper macro to reduce boilerplate needed with pushed token expressions.

quote

Quotes the specified expression as a stream of tokens for use with genco.

toksDeprecated

Helper macro to reduce boilerplate needed with nested token expressions.

Structs

Csharp

Language specialization for C#.

Dart

Language specialization for Dart.

Formatter

Formatter implementation for write types.

FormatterConfig

Configuration to use for formatting output.

Go

Language specialization for Go.

Java

Language specialization for Java.

JavaScript

JavaScript language specialization.

Python

Language specialization for Python.

Rust

Language specialization for Rust.

Swift

Swift token specialization.

Tokens

A set of tokens.

VecWriter

Helper struct to write to an underlying writer.

Enums

Cons

A managed string that permits immutable borrowing.

Element

A single element in a set of tokens.

ErasedElement

A type-erased variant of element, useful for constructing elements which are not associated with any specific customization.

LangBox

A box containing a lang item.

Traits

FormatTokens

Helper trait to convert something into tokens.

Lang

Trait to implement for language specialization.

LangItem

A type-erased holder for language-specific items.

Quoted

Trait to convert types to quoted elements.