Skip to content

dcSpark/codegen

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

116 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Codegen

Provides a builder API to assist in generating Rust code.

This is a fork of the upstream codegen crate (0.3.0). On top of upstream it emits deterministic, sorted output (see below) and adds a few builder extensions — import aliases, field/tuple visibility, cfg_attr on items, and scope merging — with no syn/quote dependency.

Discord

Installation

To use codegen, first add it as a dependency

cargo add codegen

Usage

  1. Create a Scope instance.
  2. Use the builder API to add elements to the scope.
  3. Call Scope::to_string() to get the generated code.

Example

use codegen::Scope;

let mut scope = Scope::new();

scope.new_struct("Foo")
    .derive("Debug")
    .field("one", "usize")
    .field("two", "String");

println!("{}", scope.to_string());

Deterministic output

Top-level items are emitted in a canonical order — sorted by name, then by kind (a struct sorts ahead of other items sharing its name) — so the order in which you add them does not change the output. A type and its impl blocks always group together, and generated source stays stable across unrelated input changes, which makes it well-suited to snapshot testing. Insertion order is preserved within an item (fields, variants, function bodies).

Non-goals

codegen will not attempt to perform anything beyond basic formatting. For improved formatting, the generated code can be passed to rustfmt.

License

This project is licensed under the MIT license.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in codegen by you, shall be licensed as MIT, without any additional terms or conditions.

About

A Rust library providing a builder API to generate Rust code.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Rust 98.4%
  • RenderScript 1.6%