Giter Site home page Giter Site logo

codegen-rs's Introduction

codegen-rs

This is a work-in-progress code generation library for Rust.

The exact way to create code is not set in stone yet and there are still several things missing.

Feel free to open an issue if you have any ideas for the library.

Generation currently looks like this:

let mut module = Module::new(); // Create a new module

let s_vec2 = module.add_struct("Vec2").set_public(true); // Create a struct
s_vec2.add_field("x", "f32").set_public(true);
s_vec2.add_field("y", "f32").set_public(true);

let i_vec2 = module.add_impl("Vec2"); // Create an impl block
let vec2_new = i_vec2 // Create a function in the impl block
    .add_function("new")
    .set_public(true)
    .set_const(true)
    .add_param("x", "f32")
    .add_param("y", "f32")
    .set_return_type("Self");

vec2_new
    .add_body()
    .add_statement(Statement::Return(Box::new(ReturnStatement::new(
        expr::init_struct("Self")
            .field_auto("x")
            .field_auto("y")
            .into(),
    )))); // Add a body to the function

let code = module.to_string();

The above will generate this code:

pub struct Vec2 {
    pub x: f32,
    pub y: f32,
}

impl Vec2 {
    pub const fn new(x: f32, y: f32) -> Self {
        return Self { x, y };
    }
}

codegen-rs's People

Contributors

sqyyy-jar avatar

Watchers

 avatar

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.