Expand description
Typed data exchange between a host and Rust WASM plugins.
typwire turns an ordinary Rust function into a plugin export whose
arguments and return value are decoded and encoded for you. Every value
crosses the boundary as a typed, Serde-backed
message, so a plugin never handles raw bytes itself.
§Installation
cargo add typwire§Quick Start
Call initiate_protocol! once at the crate root, then mark a function
with #[typwire::export]. It takes decodable arguments and
returns a Result, whose error type only needs to implement Display:
use typwire::{Length, initiate_protocol};
initiate_protocol!();
#[typwire::export]
fn measure(label: String, scale: f64) -> Result<(String, Length), String> {
Ok((format!("{label}{scale}"), Length::new(12.0 * scale)))
}Arguments must be owned types: references are rejected at compile time.
§Supported types
- Primitives and collections:
bool,i64,f64,String,Option<T>,Vec<T>, tuples and string-keyed maps, plus the named wrappersInt,Float,Str,Bool,ArrayandDictionary. - Binary data:
Bytes. A plainVec<u8>is an array of integers instead. - Built-in value types, such as
Length,Angle,Ratio,Color,Gradient,Stroke,Alignment,DateTime,DurationandVersion.DecimalandRegexare available behind their own features. Anyfor dynamic values, andOneOf2toOneOf8for a value that is one of several shapes, tried in declaration order.
Deriving Serialize and Deserialize is not enough for a type of your
own to cross the boundary: it must also opt in with the Typwire
derive, so an unrelated Serde type never silently becomes part of a
plugin’s interface.
use serde::{Deserialize, Serialize};
use typwire::{Length, Typwire, initiate_protocol};
initiate_protocol!();
#[derive(Serialize, Deserialize, Typwire)]
struct Size {
width: Length,
height: Length,
}
#[typwire::export]
fn area(size: Size) -> Result<f64, String> {
Ok(size.width.pt() * size.height.pt())
}§Wire formats
CBOR is the default format. Pick another per export with
#[typwire::export(format = "json")], and give an export a different
name with #[typwire::export(rename = "wire-name")]; both options can be
combined. encode and decode always use CBOR, and each Format
can also be used directly, for example Json::encode.
§Features
json: theJsonwire format, backed byserde_json.decimal: theDecimaltype, backed byrust_decimal.regex: theRegextype, backed byregex-lite.
None are enabled by default. Naming a type or format whose feature is off is a compile error.
Modules§
- color
- Typst’s predefined colors, such as
color::BLACKandcolor::RED.
Macros§
- initiate_
protocol - Macro that sets up the correct imports and traits to be used by
wasm_func.
Structs§
- Alignment
- Where to align something along an axis: horizontally, vertically, or both,
like
center + horizon. - Angle
- An angle describing a rotation.
- Arguments
- Captured arguments to a function, either positional or named.
- Array
- A sequence of values.
- Auto
- A value that indicates a smart default. The type has exactly one value,
auto. - Bool
- A type with two states,
trueandfalse. - Bytes
- A sequence of bytes, conceptually an array of integers between 0 and 255 but represented far more efficiently.
- Cbor
- The default wire format: CBOR, via
cbor4ii. - Center
- A structure representing the center of a gradient, defined by x and y ratios.
- Cmyk
- A color in the device CMYK color space.
- Conic
Gradient Builder - A builder for creating conic gradients.
- Dash
Pattern - A stroke’s dash pattern: alternating dash and gap lengths plus a phase offset.
- Date
Time - Represents a date, a time, or a combination of both.
- Date
Time Builder - A builder for
DateTime, created withDateTime::builder. - Dictionary
- A map from string keys to values.
- Duration
- Represents a positive or negative span of time.
- Duration
Builder - A builder for
Duration, created withDuration::builder. - Float
- A floating-point number, stored in 64 bits.
- Hsl
- A color in the HSL (hue, saturation, lightness) color space.
- Hsv
- A color in the HSV (hue, saturation, value) color space.
- Int
- An integer: a positive whole number, a negative whole number, or zero, stored in 64 bits.
- Label
- A label for an element.
- Length
- A size or distance.
- Linear
Gradient Builder - A builder for creating linear gradients.
- Linear
Rgb - A color in the linear RGB color space.
- Luma
- A color in the grayscale (D65 gray) color space.
- Oklab
- A color in the Oklab color space.
- Oklch
- A color in the Oklch color space.
- Path
- A file system path.
- Radial
Gradient Builder - A builder for creating radial gradients.
- Radius
- The corner radii of a rectangle, where
Noneleaves a corner at Typst’s default. - Ratio
- A ratio of a whole, written in Typst as a number followed by a percent sign.
- Rgb
- A color in the sRGB color space.
- Stop
- A structure representing a color stop in a gradient with a specified color and offset.
- Str
- A sequence of Unicode codepoints.
- Stroke
- Defines how to draw a line: a paint, thickness, line cap, line join, miter limit and dash pattern.
- Symbol
- A Unicode symbol, optionally with named modifier variants.
- Symbol
Variant - A single named modifier variant of a
Symbol. - Type
- Describes a kind of value, such as
intorstr. - Version
- A version with an arbitrary number of components.
Enums§
- Any
- A dynamic value supported by the Typwire protocol.
- Color
- A color in a specific color space.
- Color
Gradient - Represents either a single color or a gradient.
- Color
Space - The color space a gradient interpolates its stops in.
- Dash
Length - One entry of a stroke’s dash array: a length, or
"dot"(a dash as long as the stroke is thick). - Error
- An error raised while encoding or decoding a supported wire format.
- Gradient
- A color gradient: linear, radial or conic.
- HAlignment
- A horizontal alignment:
start,left,center,rightorend. - LineCap
- How the ends of a stroke are rendered:
butt,roundorsquare. - Line
Join - How sharp turns in a stroke are rendered:
miter,roundorbevel. - OneOf2
- An ordered set of alternative wire representations.
- OneOf3
- An ordered set of alternative wire representations.
- OneOf4
- An ordered set of alternative wire representations.
- OneOf5
- An ordered set of alternative wire representations.
- OneOf6
- An ordered set of alternative wire representations.
- OneOf7
- An ordered set of alternative wire representations.
- OneOf8
- An ordered set of alternative wire representations.
- VAlignment
- A vertical alignment:
top,horizonorbottom.
Traits§
- Format
- A wire format
typwirecan encode/decode aTypwirevalue through. - Typwire
- A type that is explicitly allowed to cross the Typwire boundary.
Functions§
- decode
- Decode CBOR bytes into an explicitly supported owned value.
- encode
- Encode an explicitly supported value into CBOR bytes.
Type Aliases§
- Paint
- The paint used by a stroke: a solid color or a gradient.
Attribute Macros§
- export
- Turn a function into a Typst plugin function.
Derive Macros§
- Typwire
- Mark a type as allowed to cross the Typwire boundary.