106 releases

0.17.10 Nov 13, 2024
0.17.9 May 18, 2024
0.17.8 Nov 7, 2023
0.17.5 Mar 22, 2023
0.1.11 Nov 22, 2017

#22 in Template engine

Download history 8325/week @ 2024-09-20 6495/week @ 2024-09-27 12631/week @ 2024-10-04 14225/week @ 2024-10-11 6829/week @ 2024-10-18 7299/week @ 2024-10-25 7758/week @ 2024-11-01 7209/week @ 2024-11-08 8915/week @ 2024-11-15 9243/week @ 2024-11-22 16008/week @ 2024-11-29 17592/week @ 2024-12-06 14443/week @ 2024-12-13 10229/week @ 2024-12-20 9009/week @ 2024-12-27 8206/week @ 2025-01-03

44,147 downloads per month
Used in 79 crates (27 directly)

MIT/Apache

235KB
4.5K SLoC

genco

github crates.io docs.rs build status

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 support line changes during whitespace detection, we depend on the nightly proc_macro_span feature. On stable we can only detect column changes.

Until this is stabilized and you want fully functional whitespace detection you must build and run projects using genco with a nightly compiler. This is important for whitespace-sensitive languages like python.

You can try the difference between:

cargo run --example rust

And:

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.4–0.8MB
~19K SLoC