Skip to main content

Crate typwire

Crate typwire 

Source
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

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: the Json wire format, backed by serde_json.
  • decimal: the Decimal type, backed by rust_decimal.
  • regex: the Regex type, backed by regex-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::BLACK and color::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, true and false.
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.
ConicGradientBuilder
A builder for creating conic gradients.
DashPattern
A stroke’s dash pattern: alternating dash and gap lengths plus a phase offset.
DateTime
Represents a date, a time, or a combination of both.
DateTimeBuilder
A builder for DateTime, created with DateTime::builder.
Dictionary
A map from string keys to values.
Duration
Represents a positive or negative span of time.
DurationBuilder
A builder for Duration, created with Duration::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.
LinearGradientBuilder
A builder for creating linear gradients.
LinearRgb
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.
RadialGradientBuilder
A builder for creating radial gradients.
Radius
The corner radii of a rectangle, where None leaves 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.
SymbolVariant
A single named modifier variant of a Symbol.
Type
Describes a kind of value, such as int or str.
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.
ColorGradient
Represents either a single color or a gradient.
ColorSpace
The color space a gradient interpolates its stops in.
DashLength
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, right or end.
LineCap
How the ends of a stroke are rendered: butt, round or square.
LineJoin
How sharp turns in a stroke are rendered: miter, round or bevel.
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, horizon or bottom.

Traits§

Format
A wire format typwire can encode/decode a Typwire value 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.