An open API service indexing awesome lists of open source software.

https://github.com/udoprog/genco

A whitespace-aware quasiquoter for beautiful code generation.
https://github.com/udoprog/genco

code-generation proc-macro quasiquoter rust

Last synced: 9 months ago
JSON representation

A whitespace-aware quasiquoter for beautiful code generation.

Awesome Lists containing this project

README

          

# genco

[github](https://github.com/udoprog/genco)
[crates.io](https://crates.io/crates/genco)
[docs.rs](https://docs.rs/genco)
[build status](https://github.com/udoprog/genco/actions?query=branch%3Amain)

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:

```bash
cargo run --example rust
```

And:

```bash
cargo +nightly run --example rust
```

[`proc_macro_span` feature]: https://github.com/rust-lang/rust/issues/54725


## Supported Languages

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

* [🦀 Rust][rust]

[Example][rust-example]

* [☕ Java][java]

[Example][java-example]

* [🎼 C#][c#]

[Example][c#-example]

* [🐿️ Go][go]

[Example][go-example]

* [🎯 Dart][dart]

[Example][dart-example]

* [🌐 JavaScript][js]

[Example][js-example]

* [🇨 C][c]

[Example][c-example]

* [🐍 Python][python]

[Example][python-example]

**Requires a `nightly` compiler**

Is your favorite language missing? [Open an issue!]

You can run one of the examples by:

```bash
cargo +nightly run --example rust
```


## Rust Example

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

```rust
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:

```rust,no_test
use std::collections::HashMap;

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


[c-example]: https://github.com/udoprog/genco/blob/master/examples/c.rs
[c]: https://docs.rs/genco/latest/genco/lang/c/index.html
[c#-example]: https://github.com/udoprog/genco/blob/master/examples/csharp.rs
[c#]: https://docs.rs/genco/latest/genco/lang/csharp/index.html
[dart-example]: https://github.com/udoprog/genco/blob/master/examples/dart.rs
[dart]: https://docs.rs/genco/latest/genco/lang/dart/index.html
[go-example]: https://github.com/udoprog/genco/blob/master/examples/go.rs
[go]: https://docs.rs/genco/latest/genco/lang/go/index.html
[impl_lang!]: https://docs.rs/genco/latest/genco/macro.impl_lang.html
[import statements]: https://docs.rs/genco/latest/genco/macro.quote.html#imports
[indentation is meaningful]: https://docs.python.org/3/faq/design.html#why-does-python-use-indentation-for-grouping-of-statements
[interpolate]: https://docs.rs/genco/latest/genco/macro.quote.html#quoted-string-interpolation
[java-example]: https://github.com/udoprog/genco/blob/master/examples/java.rs
[java]: https://docs.rs/genco/latest/genco/lang/java/index.html
[js-example]: https://github.com/udoprog/genco/blob/master/examples/js.rs
[js]: https://docs.rs/genco/latest/genco/lang/js/index.html
[Open an issue!]: https://github.com/udoprog/genco/issues/new
[python-example]: https://github.com/udoprog/genco/blob/master/examples/python.rs
[python]: https://docs.rs/genco/latest/genco/lang/python/index.html
[quote strings]: https://docs.rs/genco/latest/genco/macro.quote.html#string-quoting
[quote_in!]: https://docs.rs/genco/latest/genco/macro.quote_in.html
[quote!]: https://docs.rs/genco/latest/genco/macro.quote.html
[quoted()]: https://docs.rs/genco/latest/genco/tokens/fn.quoted.html
[rust-example]: https://github.com/udoprog/genco/blob/master/examples/rust.rs
[rust]: https://docs.rs/genco/latest/genco/lang/rust/index.html
[solve namespace conflicts]: https://docs.rs/genco/latest/genco/lang/csharp/fn.import.html
[token streams]: https://docs.rs/genco/latest/genco/tokens/struct.Tokens.html
[whitespace detection]: https://docs.rs/genco/latest/genco/macro.quote.html#whitespace-detection