| Crates.io | sertyp |
| lib.rs | sertyp |
| version | 0.1.1 |
| created_at | 2026-01-09 12:15:25.469677+00 |
| updated_at | 2026-01-09 12:15:25.469677+00 |
| description | Serialization and deserialization of most typst types into sertyp CBOR format: This includes markdown, math, etc. |
| homepage | |
| repository | https://github.com/Uhrendoktor/sertyp |
| max_upload_size | |
| id | 2031976 |
| size | 66,336 |
Rust data structures for Typst values with serialization and deserialization from and into the sertyp CBOR format. Allows for straighforward communication between typst and WASM plugins.
This library provides serialization and deserialization logic for the sertyp CBOR format into a handy rust data-structure and utility functions.
Primitives: bool, int, float, string, bytes, none, auto
Common: array, dict, function, type, decimal
Typst-specific: alignment, angle, color, length, relative, ratio, fraction, duration, datetime, symbol, label, regex, stroke, gradient, tiling, direction, version, module, styles, content
use wasm_minimal_protocol::*;
use sertyp::*;
#[cfg(target_arch = "wasm32")]
initiate_protocol!();
#[wasm_func]
pub fn fibonacci(
data: &[u8],
) -> Vec<u8> {
let n: i32 = match deserialize_cbor(data) {
Ok(Item::Integer(i)) => {
match i.try_into() {
Ok(n) => n,
Err(_) => error!("Invalid integer range")
}
},
Ok(other) => {
error!("Expected integer, found {}", other.type_name());
}
Err(e) => {
error!("Deserialization Error: {}", &e);
}
};
let (mut v0, mut v1) = (0, 1);
for _ in 0..n {
(v0, v1) = (v1, v0 + v1);
}
let result: Integer = v1.into();
serialize_cbor(&result.into()).unwrap()
}
#import "@preview/sertyp:0.1.1";
#let fibonacci(n) = {
let plugin = plugin("./target/wasm32-unknown-unknown/release/test_plugin.wasm");
sertyp.deserialize_cbor(test_plugin.cycle(sertyp.serialize_cbor(data)));
}
#assert(fibonacci(10) == 89)
The test suite in ../test_plugin/ validates roundtrip serialization for all types:
cd ../test_plugin
cargo build --release
# Then run via Typst
typst compile ../test_plugin/test.typ --root ..
To add a new type:
src/types/<newtype>.rs with serialization logicItem enum in src/types/mod.rsItemTagged_ enum for CBOR encodingFrom<Item_> and From<Item> blocks../typst/types/<newtype>.typ