Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/specta-rs/specta
Easily export your Rust types to other languages
https://github.com/specta-rs/specta
reflection rspc rust rust-macro specta tauri-specta typesafe typescript
Last synced: 6 days ago
JSON representation
Easily export your Rust types to other languages
- Host: GitHub
- URL: https://github.com/specta-rs/specta
- Owner: specta-rs
- License: mit
- Created: 2022-12-16T17:18:52.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-12-11T02:54:48.000Z (13 days ago)
- Last Synced: 2024-12-13T21:54:03.326Z (10 days ago)
- Topics: reflection, rspc, rust, rust-macro, specta, tauri-specta, typesafe, typescript
- Language: Rust
- Homepage:
- Size: 1.07 MB
- Stars: 294
- Watchers: 5
- Forks: 41
- Open Issues: 113
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
## Features
- Export structs and enums to [Typescript](https://www.typescriptlang.org)
- Get function types to use in libraries like [tauri-specta](https://github.com/oscartbeaumont/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](https://github.com/oscartbeaumont/rspc) - Easily building end-to-end typesafe APIs
- [tauri-specta](https://github.com/oscartbeaumont/tauri-specta) - Typesafe Tauri commands and events
- [TauRPC](https://github.com/MatsDK/TauRPC) - Tauri extension to give you a fully-typed IPC layer.## Usage
Add `specta` as a dependency to your project,
enabling the languages you want to export to:```bash
cargo add specta --features typescript # only 'typescript' is currently supported
```Then you can use Specta like so:
```rust
use specta::{ts, Type};#[derive(Type)]
pub struct TypeOne {
pub a: String,
pub b: GenericType,
#[serde(rename = "cccccc")]
pub c: MyEnum,
}#[derive(Type)]
pub struct GenericType {
pub my_field: String,
pub generic: A,
}#[derive(Type)]
pub enum MyEnum {
A,
B,
C,
}fn main() {
assert_eq!(
ts::export::(&Default::default()).unwrap(),
"export type TypeOne = { a: string; b: GenericType; cccccc: MyEnum }".to_string()
);
}
```Check out the [docs](https://docs.rs/specta) for more information.
## Motivation
This library was originally created to power the type exporting functionality of [rspc](https://rspc.dev),
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](https://github.com/brendonovich) for doing a heap of development on this library.