Code Monkey home page Code Monkey logo

codegen's Introduction

Codegen

Provides an builder API to assist in generating Rust code.

Build Status

More information about this crate can be found in the crate documentation

Installation

To use codegen, first add this to your Cargo.toml:

[dependencies]
codegen = "0.1.3"

Next, add this to your crate:

extern crate 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.

For 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());

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.

codegen's People

Contributors

andrewleverette avatar briansmith avatar carllerche avatar hawkw avatar luro02 avatar marcelbuesing avatar nbigaouette avatar neysofu avatar piranna avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

codegen's Issues

Code generation?

So far docs and tests show only high level components can be generated, like structs, modules and functions, and code needs yo be created with raw code strings at Function.line(). Not sure if it's a limitation of the builder pattern used un the API, but how much would cost to add support for generation of variables, asignements, blocks, loops, conditionals and function calls?

Const + Static

It would be nice if we could add const or static global items to codegen scopes. To ensure correctness, these could only be allowed for a small set of primitives, like ints and floats and &str, as well as arrays / slices / references of those. I have a password generator which uses build.rs to compile a list of words directly into the source; codegen would be a great addition to this functionality.

Annotations for structs, enums and variants

When dealing with some serialization libraries, it's required to add annotations to structs, enums and variants. There are currently three PR fixing this issue, could the maintainers review them and merge at least one of them?

#32 #33 #34
(the last one was submitted by me)

Crate maintenance has moved to Gitlab!

Edit: May 2 2023

Maintenance of this crate has been picked up by lovoslavlovchev (with some help from me) @ https://gitlab.com/IovoslavIovchev/codegen

Please file new issues there. New versions of this crate on crates.io will be released from there.

---- Original Text:
This crate is useful, but missing some minor features. Several pull requests have been open for years without merging?

Would love not to fork, but actually to breath life into this crate?

Split in múltiple files

Lib.rs file is almost 2000 lines of code, wouldn't It be better to split un a file for each Item type? Also, some methods are duplicarse, and by being traits its code could be reused between several Items...

How do I add a Function to a Module?

The Module has a Scope with a Vec of Item, but there is no Function in the Item, so the only way to push a Function to Module is to turn it into a String, right?

Perhaps Item should include Function, which would make it much more easier.

Generated empty structs are missing semicolomn

Interesting crate, exactly what I was attempting to do! :D

When generating an empty struct (using cd34e60), a semicolon is missing from the generated string.

For example:

extern crate codegen;

use codegen::Scope;

// Generated from the code underneath. Note the missing `;`
// #[derive(Debug)]
// struct Foo

fn main() {
    let mut scope = Scope::new();

    scope.new_struct("Foo").derive("Debug");

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

Field documentation

It'd be interesting to be able to add (structs' and fields') fields documentation.

For example:

let mut scope = Scope::new();

scope.new_struct("Foo").field("bar", "i64", "This is a `bar`");

could generate:

struct Foo {
    /// This is a `bar`
    bar: i64,
}

Or maybe a Field type would be useful which could be push_field()ed to a Struct, similarly to Scope::push_struct()?

Push to crates.io?

Hi, I quite like this library, but it isn't on crates.io currently. Do you think you can push it, even if it might be lacking features in some areas?

Multiple calls to allow should emit multiple attributes for an enum

use codegen::Enum;

let enumerator = Scope::new_enum("ExampleEnum")
    .vis("pub")
    .derive("Debug")
    .derive("Clone")
    .derive("PartialEq")
    .allow("clippy::all")
    .allow("dead_code")
    .to_string();

println!("{}", enumerator);

Something like this would be generated:

#[derive(Debug, Clone, PartialEq)]
#[allow(dead_code)]
pub enum ExampleEnum {}

The clippy::all attribute would be overwritten by the second call to it, but I expected it to work like the derive function.

Release new version

I would be grateful if you could release a new version, including the two latest pull requests (#12, #13). Both changes should be non breaking.

I am planning to release a crate based on the changes.

Fields annotations?

It would be interesting to be able to add a field annotations (to structs and enums?).

For example I often need to rename my fields for serde using #[serde(rename = "mixedCaseRequired")].

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.