Git Product home page Git Product logo

gents's Introduction

gents

MIT/Apache 2.0

gents is a tool for generating Typescript interfaces from Rust code. We can easily use serde-json to communicate between Rust and Typescript, without writing Typescript stubs trivially. It is helpful when your API changes frequently.

It is designed for LogiSheets and is inspired by ts-rs. Many thanks to them!

Your issues and PRs are welcome!

Why do you need gents?

  • Writing a easy web server in Rust and you hate things like grpc-web
  • Writing a wasm project

Why not ts-rs?

  • ts-rs generates the files when running cargo test and in this way we must commit those generated files into our repo. It is not necessary and is even an obstacle when we use other build tools like bazel. gents acts as a binary to generate Typescript files.

  • gents introduces a concept Group that from all the members in this group files generated will be placed in the same directory. Group is seperate from the other group even though they can share some dependecies. Therefore, gents requires you to specify the file_name on structs or enums and to specify the dir on group, while ts-rs requires specifing the path on every item.

  • gents helps you manage the export files. And it gathers all the dependencies automatically.

  • gents is well support for referencing other crates.

  • Code generated by ts-rs is not match our coding style.

How to use gents?

In your Rust code:

You should import gents in your Cargo.toml.

[dev-dependencies]
gents = "0.4"
gents_derives = "0.4"
use gents_derives::TS;

#[derive(TS)]
#[ts(file_name = "person.ts", rename_all = "camelCase")]
pub struct Person {
    pub age: u16,
    pub en_name: String,
}

#[derive(TS)]
#[ts(file_name = "group.ts", rename_all = "camelCase")]
pub struct Group {
    pub name: String,
    pub capacity: u16,
    pub members: Vec<Person>,
    pub leader: Option<Person>,
}

derive(TS) generates code under #[cfg(any(test, feature="gents"))], which means gents does not make any difference until you run it.

If all the structs or enums derived from TS are in the same crate, we recommend that you can write a simple unit test to generate the files like below:

#[ignore]
#[test]
fn gents() {
    use gents::FileGroup;
    let mut group = FileGroup::new();
    // You don't need to add Person because it is a dependency of Group and it will be added automatically
    group.add::<Group>();
    // If you need to generate the index.ts file, set true.
    group.gen_files("outdir", false);
}

After running this test, there are 2 files generated in outdir:

  • person.ts
  • group.ts

Check more cases and usage in the tests folder.

If your derive(TS)s are from different crates, then you should need to define a feature called gents. Please check the detailed usage in LogiSheets.

gents's People

Contributors

imjeremyhe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  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.