rust-codegen 0.1.1

A simple builder API for generating Rust code
Documentation
  • Coverage
  • 100%
    25 out of 25 items documented1 out of 1 items with examples
  • Size
  • Source code size: 106.28 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 7.64 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 14s Average build duration of successful builds.
  • all releases: 14s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • robertcorponoi/rust-codegen
    1 1 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • robertcorponoi

Rust Codegen

Rust Codegen aims to help you generate Rust code programmatically with a simple builder API.

Installation

To use rust-codegen, add the following to your Cargo.toml file:

[dependencies]
rust-codegen

Usage

While usage can vary based on what you need, a basic flow is creating a Scope and then adding what you need onto it. Below is a simple example of creating a struct with a couple of fields.

use rust_codegen::Scope;

// A `Scope` is the root of the builder. Everything should be added on to it.
let mut scope = Scope::new();

// Creates a new struct named Foo that derives from `Debug` and have two fields.
scope.new_struct("Foo")
    .derive("Debug")
    .field("one", "usize")
    .field("two", "String");

// Once turned into a string, the above looks like:
// #[derive(Debug)]
// struct Foo {
//    one: usize,
//    two: String,
// }
println!("{}", scope.to_string());

Make sure to check out the documentation for all of the available features with examples.

Acknowledgements

This was originally a fork of carllerche's codegen repo with some updates. However, due to the amount of updates and the fact that I needed to publish it on crates.io for other projects, I made it its own thing.

License

MIT

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.

For example:

Note: You should not rely on the formatted output as it's very basic. You should instead run the generated code through rustfmt.