Expand description
Easily export your Rust types to other languages
Specta provides a system for type introspection and a set of language exporters which allow you to export your Rust types to other languages!
Currently we only support exporting to TypeScript but work has begun on other languages.
§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
§Example
use specta::{*, ts::*};
#[derive(Type)]
pub struct MyCustomType {
pub my_field: String,
}
fn main() {
assert_eq!(
ts::export::<MyCustomType>(&ExportConfig::default()).unwrap(),
"export type MyCustomType = { my_field: string }".to_string()
);
}
§Supported Libraries
If you are using Prisma Client Rust you can enable the rspc
feature on it to allow for Specta support on types coming directly from your database. This includes support for the types created via a selection.
§Feature flags
function
- Support for exporting the types of Rust functions.export
- Support for collecting up a global type map
Languages
typescript
- Support for TypeScript language exportingjs_doc
- Support for JSDoc exporting helpers. Also requirestypescript
feature to be enabled.
Compatability
serde
- Support for serdeserde_json
- Support for serde-jsonserde_yaml
- Support for serde_yamltoml
- Support for tomltauri
- Support for Tauri. This is required when usingspecta::function
with Tauri Commands.
External types
ulid
- ulid crateuuid
- uuid cratechrono
- chrono cratetime
- time cratebigdecimal
- bigdecimal craterust_decimal
- rust_decimal crateindexmap
- indexmap crateipnetwork
- ipnetwork cratemac_address
- mac_address cratebit-vec
- bit-vec cratebson
- bson crateuhlc
- uhlc cratebytesize
- bytesize crateglam
- glam cratetokio
- tokio crateurl
- url crateeither
- either cratebevy_ecs
- bevy_ecs crate
§Alternatives
§Why not ts-rs?
ts-rs is a great library, but it has a few limitations which became a problem when I was building rspc. Namely it deals with types individually which means it is not possible to export a type and all of the other types it depends on.
§Why not Typeshare?
Typeshare is also great, but its approach is fundamentally different. While Specta uses traits and runtime information, Typeshare statically analyzes your Rust files. This results in a loss of information and lack of compatability with types from other crates.
Modules§
- Types related to working with
DataType
. Exposed for advanced users. - function
function
Support for exporting Rust function.
Structs§
- The location of the impl block for a given type. This is used for error reporting. The content of it is transparent and is generated by the macros.
- The unique Specta ID for the type.
- A map used to store the types “discovered” while exporting a type.
Enums§
- TODO
Traits§
- A marker trait for compile-time validation of which types can be flattened.
- TODO
- represents a type that can be converted into NamedDataType. This will be implemented for all types with the Type derive macro.
- Provides runtime type information that can be fed into a language exporter to generate a type definition in another language. Avoid implementing this trait yourself where possible and use the
Type
macro instead.
Attribute Macros§
- specta
derive
andfunction
Prepares a function to have its types extracted usingfunctions::fn_datatype