Git Product home page Git Product logo

specta's Introduction

Specta Logo

Specta

Easily export your Rust types to other languages

Discord Crates.io crates.io docs.rs License

Features

  • Export structs and enums to Typescript
  • Get function types to use in libraries like tauri-specta
  • Supports wide range of common crates in Rust ecosystem
  • Supports type inference - can determine type of fn demo() -> impl Type.

Ecosystem

Specta can be used in your application either directly or through a library which simplifies the process of using it.

  • rspc for easily building end-to-end typesafe APIs
  • tauri-specta for typesafe Tauri commands

Usage

Add specta as a dependency to your project, enabling the languages you want to export to:

cargo add specta --features typescript # only 'typescript' is currently supported

Then you can use Specta like so:

use specta::{ts, Type};

#[derive(Type)]
pub struct TypeOne {
    pub a: String,
    pub b: GenericType<i32>,
    #[serde(rename = "cccccc")]
     pub c: MyEnum,
}

#[derive(Type)]
pub struct GenericType<A> {
    pub my_field: String,
    pub generic: A,
}

#[derive(Type)]
pub enum MyEnum {
    A,
    B,
    C,
}

fn main() {
    assert_eq!(
        ts::export::<TypeOne>(&Default::default()).unwrap(),
        "export type TypeOne = { a: string; b: GenericType<number>; cccccc: MyEnum }".to_string()
    );
}

Check out the docs for more information.

Motivation

This library was originally created to power the type exporting functionality of rspc, but after building it we realized that it could be useful for other projects as well so we decided to move it into a dedicated library.

A huge thanks to Brendonovich for doing a heap of development on this library.

specta's People

Contributors

oscartbeaumont avatar brendonovich avatar tonymushah avatar whymidnight avatar lowr avatar vlad-shcherbina avatar thy486 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.