An open API service indexing awesome lists of open source software.

https://github.com/deno-prototyping/rusty-types

Easily create Rust-flavored datatypes in TypeScript
https://github.com/deno-prototyping/rusty-types

deno enum prototyping rust standard-schema struct typescript

Last synced: about 2 months ago
JSON representation

Easily create Rust-flavored datatypes in TypeScript

Awesome Lists containing this project

README

          

# Rusty Types

> [!NOTE]
> This project is still a work in progress.

This package help you create Rust flavored datatypes in TypeScript.

## Usage

> [!NOTE]
> Currently rusty-types is not published to JSR.
> You can import it with JSDelivr.

Two foundamental exports from `rusty-types` is `Struct` and `Enum`.

`rusty-types` use [`standard-schema`](https://github.com/standard-schema/standard-schema) in its interface,
so you can use [Zod](https://zod.dev/), [Valibot](https://valibot.dev/), [Arktype](https://arktype.io/)
or any other validator to your best fits.

Case
Rust
rusty-types

Named struct

```rust
struct Person {
name: String,
age: usize,
}

let person = Person {
name: "Alice".into(),
age: 20
};
```

```typescript
const Person = Struct({
name: z.string(),
age: z.number(),
});

const alice = new Person({
name: "Alice",
age: 20
});
const bob = Person({
name: "Bob",
age: 22
});
```

Tuple struct

```rust
struct Vec3(f32, f32, f32);

let vec = Vec3(1., 2., 3.);
```

```typescript
const Vec3 = Struct([
z.number(),
z.number(),
z.number(),
]);

const vec_1 = new Vec3(1, 2, 3);
const vec_2 = Vec3(4, 5, 6);
```

Enum

```rust
enum Event {
Key { code: usize },
Mouse {
state: usize,
button: usize,
},
Paste(String),
}

let event_1 = Event::Key {
code: 123
};
let event_2 = Event::Mouse {
button: 123,
state: 0,
};
let event_3 = Event::Paste(
"some text".into()
);
```

```typescript
const Event = Enum({
Key: { code: z.number() },
Mouse: {
state: z.number(),
button: z.number()
},
Paste: [z.string()] as const,
});

const event_1 = Event.Key({
code: 123
});
const event_2 = new Event.Mouse({
button: 123,
state: 0
});
const event_3 = new Event.Paste(
"some text"
);
```

## Roadmap

- [ ] custom transform
- [ ] methods
- [ ] extends
- [ ] auto JS-lize(e.g. `get_name()` -> `get name()`)