Litty
Literally adds literals to Rust enum variants, struct fields, and unit structs.
Litty extends Serde and allows you to represent TypeScript-like literal types, bridging the gap between the type systems.
Installation
Add litty to your Cargo.toml:
[]
= "0.3"
Or install using cargo add:
Usage
Litty is a Serde extension and provides a set of drop-in Serialize and Deserialize derive macro replacements (SerializeLiterals and DeserializeLiterals), allowing you to add literal fields to a struct or mark certain enum variants as literals.
It also provides a literal attribute macro that can be used on unit structs and enum variants to mark them as literals.
Struct Fields
With SerializeLiterals and DeserializeLiterals derive macros, you can add literal fields to a struct:
use ;
It directly maps to a TypeScript type like this:
interface SuccessResponseV1 {
v: 1;
status: "ok";
message: string;
}
The struct will serialize to JSON like this:
Enum Variants
You can also mark enum variants as literals using SerializeLiterals and DeserializeLiterals:
use ;
It directly maps to a TypeScript type like this:
type Status = "ok" | "error";
The Status::Ok and Status::Error variants will serialize to JSON strings "ok" and "error", respectively.
Combined Serialization and Deserialization for Structs and Enums
Litty also provides the Literals derive macro that combines both SerializeLiterals and DeserializeLiterals into one:
use ;
Unit Structs
You can also use literal on unit structs to create literal types:
use ;
;
;
This directly maps to TypeScript types like this:
type StatusOk = "ok";
type StatusError = "error";
The StatusOk and StatusError unit structs will serialize to JSON strings "ok" and "error", respectively.
Granular Serialization and Deserialization for Unit Structs
If you want to add serialization or deserialization to a unit struct without the other, you can use the serialize_literal or deserialize_literal attribute macros:
use ;
;
;
Changelog
See the changelog.