typwire 0.1.0

Rust types for the Typst plugin system.
Documentation
  • Coverage
  • 50.68%
    74 out of 146 items documented17 out of 28 items with examples
  • Size
  • Source code size: 78.23 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 12.99 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 34s Average build duration of successful builds.
  • all releases: 34s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • T1mVo/typwire
    2 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • T1mVo

typwire

An extended CBOR encoder for the Typst plugin system.

This library provides a CBOR encoder with extended support for Typst types, along with corresponding Rust types and deserialization, enabling seamless data exchange between Typst and Rust WASM plugins.

Usage

Note: The versions used in Typst and Rust MUST match exactly.

Rust:

Add typwire to your Rust project like this:

cargo add typwire
use serde::Deserialize;
use typwire::{Angle, Color, DateTime, FromBytes as _};
use wasm_minimal_protocol::*;

initiate_protocol!();

#[derive(Deserialize)]
struct Custom {
    angle: Angle,
    color: Color,
    datetime: DateTime,
}

#[wasm_func]
fn custom_fn(arg: &[u8]) -> Result<Vec<u8>, String> {
    let custom = Custom::from_bytes(arg)?;

    // ...

    Ok(vec![])
}

Typst:

#import "@preview/typwire:0.1.0"

#let custom-plugin = plugin("custom_plugin.wasm")

#let custom = (
    "angle": 90deg,
    "color": red,
    "datetime": datetime.today(),
)

#let encoded = typwire.cbor.encode(custom)

#custom-plugin.custom_fn(encoded)

Supported types

  • int
  • float
  • bytes
  • str
  • bool
  • content
  • none
  • array
  • dictionary
  • angle
  • length
  • ratio
  • color
  • gradient
  • datetime
  • duration
  • version
  • type
  • fraction
  • direction
  • relative
  • decimal

Missing features

  • Color conversion in Rust
  • Dynamic Value type that includes all Typst types
  • Decoding from plugin
  • Static Gradient defaults
  • Support for all types