[−][src]Crate genco
genco is a language neutral code generator and quasi quoter.
Central to genco is the quote! and quote_in! macros. While token streams can be constructed manually, these makes the process much more intuitive.
genco solves the following, language-specific concerns.
-
Imports — genco generates and groups import statements as they are used. No redundancy, no mess. Only exactly what you need. Worried about conflicts? We've got you covered.
-
String Quoting — we figure out how to correctly quote strings for your language. We even know how to generate code to interpolate values into the quoted string (like
"Hello $name"
in Dart). -
Structural Indentation — our quasi quoter performs efficient whitespace detection to structurally sort out spaces and indentation. Allowing us to generate beautiful, readable code with minimal effort.
-
Language Customization — building support for new languages is a piece of cake with the help of the batteries included impl_lang! macro.
In order to do whitespace detection, we depend on the
proc_macro_span
feature to access information on spans.
Until this is stable, you must build and run projects using genco with the
nightly
compiler.
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); }
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
impl_lang | Macro to implement support for a custom language. |
quote | Language neutral whitespace sensitive quasi-quoting. |
quote_in | Behaves the same as quote! while quoting into an existing token stream
with |
Structs
Tokens | A stream of tokens. |