[−][src]Crate genco
genco is a code generator and quasi quoter for Rust, written for use in reproto.
The workhorse of genco is the quote! and quote_in! macros. While tokens can be constructed manually, these make this process much easier.
genco only minimally deals with language-specific syntax, but primarily deals with solving the following:
-
Imports — genco generates and groups import statements according to conventions for the language being generated for.
-
String Quoting — Strings can be quoted using the
<stmt>.quoted()
trait function. -
Structural Indentation — genco's quasi quoting utilizes whitespace detection to structurally sort out spaces and indentation.
We depend on proc_macro_hygiene
stabilizations. Until then, you must build
and run with the nightly
branch.
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::imported("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(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); }
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 for working with genco. |
fmt | Code formatting utilities. |
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
quote | Language neutral whitespace sensitive quasi-quoting. |
quote_in | Behaves the same as quote! while quoting into an existing token stream
with |
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. |
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 stream of tokens. |
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. |