From 365c8cc564347ed304d8c1876b66d321a11e85fd Mon Sep 17 00:00:00 2001 From: cognivore <66186054+cognivore@users.noreply.github.com> Date: Mon, 27 Jan 2025 00:49:41 +0000 Subject: [PATCH] Problem: Beginners struggle with recent changes Solution: - Port the old example that just exports one type. - Add an example which exports everything to a file (which seems to be the go-to way beginners use specta). --- README.md | 55 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 2fd49ad0..b13dd209 100644 --- a/README.md +++ b/README.md @@ -39,9 +39,9 @@ cargo add specta --features typescript # only 'typescript' is currently supporte Then you can use Specta like so: ```rust -use specta::{ts, Type}; +use specta::Typescript; -#[derive(Type)] +#[derive(specta::Type)] pub struct TypeOne { pub a: String, pub b: GenericType, @@ -49,13 +49,13 @@ pub struct TypeOne { pub c: MyEnum, } -#[derive(Type)] +#[derive(specta::Type)] pub struct GenericType { pub my_field: String, pub generic: A, } -#[derive(Type)] +#[derive(specta::Type)] pub enum MyEnum { A, B, @@ -64,14 +64,57 @@ pub enum MyEnum { fn main() { assert_eq!( - ts::export::(&Default::default()).unwrap(), + specta_typescript::export::(&Typescript::default()).unwrap(), "export type TypeOne = { a: string; b: GenericType; cccccc: MyEnum }".to_string() ); } ``` -Check out the [docs](https://docs.rs/specta) for more information. +A common use case is to export all types for which `specta::Type` is derived into a single file: + +```rust +use specta::Typescript; + +#[derive(specta::Type)] +pub enum MyEither { + Left(L), + Right(R), +} + +#[derive(specta::Type)] +pub struct GenericType { + pub my_field: String, + pub generic: A, +} + +#[derive(specta::Type)] +pub enum MyEnum { + A, + B, + C, +} + +fn main() { + let types_path = std::path::Path::new("priv/test/types.ts"); + Typescript::default().export_to(types_path, &specta::export()).unwrap(); +} +``` + +This code shall generate the following file and create `priv/test` if it doesn't exist: +```typescript +// This file has been generated by Specta. DO NOT EDIT. + +export type GenericType = { my_field: string; generic: A } + +export type MyEither = { Left: L } | { Right: R } + +export type MyEnum = "A" | "B" | "C" + +export type TypeOne = { a: string; b: GenericType; cccccc: MyEnum } +``` + +Check out the [docs](https://docs.rs/specta) for more information. ## Motivation