[−][src]Crate genco
GenCo
GenCo is an even simpler code generator for Rust, written for use in reproto.
We depend on (Yet to be released) 1.45 and some of its proc_macro_hygiene
stabilizations. Until then, you must build with the beta
branch.
For example:
cargo +beta run --example rust
The workhorse of GenCo is the quote! macro. While tokens can be constructed manually, quote! makes this process much easier.
GenCo only minimally deals with language-specific syntax, but primarily deals with solving the following:
- Generates and groups import statements.
- Quote (and escape) strings using
<stmt>.quoted()
. - Indents and spaces your code according to generic indentation rules that can be tweaked on a per-language basis.
Examples
The following are language specific examples for GenCo using the quote! macro.
- Rust Example
- Java Example
- C# Example
- Go Example
- Dart Example (TODO)
- JavaScript Example (TODO)
- Python Example (TODO)
You can run one of the examples above using:
cargo run --example go
The following is the included example Rust program.
use genco::prelude::*; use rand::Rng; use std::fmt; fn main() -> fmt::Result { // Import the LittleEndian item, without referencing it through the last // module component it is part of. let little_endian = rust::imported("byteorder", "LittleEndian").qualified(); let big_endian = rust::imported("byteorder", "BigEndian"); // This is a trait, so only import it into the scope (unless we intent to // implement it). let write_bytes_ext = rust::imported("byteorder", "WriteBytesExt").alias("_"); let read_bytes_ext = rust::imported("byteorder", "ReadBytesExt").alias("_"); // Iterators can be tokenized using `tokenize_iter`, as long as they contain // something which can be converted into a stream of tokens. let numbers = (0..10) .map(|_| quote!(#(rand::thread_rng().gen::<i16>()))); let tokens = quote! { // Markup used for imports without an immediate use. #@(write_bytes_ext) #@(read_bytes_ext) fn test() { let mut wtr = vec![]; wtr.write_u16::<#little_endian>(517).unwrap(); wtr.write_u16::<#big_endian>(768).unwrap(); assert_eq!(wtr, vec![#numbers,*]); } }; // Simpler printing with default indentation: // println!("{}", tokens.to_file_string()?); tokens.to_io_writer_with( std::io::stdout().lock(), rust::Config::default(), FormatterConfig::from_lang::<Rust>().with_indentation(2), )?; Ok(()) }
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:
let _: genco::Tokens<genco::Rust> = genco::quote!(fn test() {});
Becomes:
fn test() {}
More that two line breaks are collapsed.
So:
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:
let _: genco::Tokens<genco::Rust> = genco::quote! { fn test() { println!("Hello..."); println!("... World!"); } };
Becomes:
fn test() { println!("Hello..."); println!("... World!"); }
Re-exports
pub use self::ext::DisplayExt; |
pub use self::ext::QuotedExt; |
Modules
csharp | Specialization for Csharp code generation. |
dart | Specialization for Dart code generation. |
ext | Extension traits provided by GenCo. |
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
nested | Deprecated Helper macro to reduce boilerplate needed with nested token expressions. |
push | Deprecated Helper macro to reduce boilerplate needed with pushed token expressions. |
quote | Quotes the specified expression as a stream of tokens for use with genco. |
quote_in | Same as [quote!], except that it allows for quoting directly to a token stream. |
toks | Deprecated Helper macro to reduce boilerplate needed with nested token expressions. |
Structs
Csharp | Language specialization for C#. |
Dart | Language specialization for Dart. |
Display | Struct containing a type that implements Display and can be tokenized into a stream. |
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. |
LangBox | A box containing a lang item. |
Python | Language specialization for Python. |
Quoted | Struct containing a type that is quoted. |
Rust | Language specialization for Rust. |
Swift | Swift token specialization. |
Tokens | A set of tokens. |
VecWriter | Helper struct to write to an underlying writer. |
Enums
Item | A single element in a set of tokens. |
ItemStr | A managed string that permits immutable borrowing. |
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. |
RegisterTokens | Helper trait to convert something into a tokens registration. |