19 releases (5 stable)

2.0.0-rc.9 Apr 2, 2024
2.0.0-rc.7 Dec 4, 2023
2.0.0-rc.6 Oct 22, 2023
2.0.0-rc.1 Jul 17, 2023
0.0.2 Jul 31, 2022

#95 in Web programming

Download history 572/week @ 2023-12-20 657/week @ 2023-12-27 1099/week @ 2024-01-03 956/week @ 2024-01-10 955/week @ 2024-01-17 1051/week @ 2024-01-24 679/week @ 2024-01-31 689/week @ 2024-02-07 805/week @ 2024-02-14 1336/week @ 2024-02-21 1096/week @ 2024-02-28 919/week @ 2024-03-06 800/week @ 2024-03-13 1361/week @ 2024-03-20 1463/week @ 2024-03-27 1608/week @ 2024-04-03

5,389 downloads per month
Used in 15 crates (12 directly)

MIT license

190KB
4K SLoC

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.

Dependencies

~0.5–14MB
~159K SLoC