Files
Dylan M. Taylor 26b36b4f9e Add craft-fig: the .fig container, read and written (#905)
* Add craft-kiwi: the kiwi binary schema format, decoded and encoded

The generic kiwi decoder and encoder from FigCraft's .fig import and export
(crates/format/src/fig/kiwi.rs at 3b9bf8c9), moved as it is: binary schema
decode and encode, the compact text form of a schema, message decode into a
tree keyed by field name, and message encode checked against the schema.

The value budget that follows a .fig file's compressed size stays with the
.fig reader; here decode_message_within takes the budget and
DEFAULT_MAX_VALUES is the floor FigCraft used.

* Add craft-fig: the .fig container, read and written

The app-independent half of FigCraft's .fig import and export
(crates/format/src/fig at 3b9bf8c9): the ZIP archive with its image
budget, the chunked canvas (deflate, zlib and zstd chunks), the value
budget that follows the compressed canvas, images carried in the
message's blobs, and the vector network and geometry blob encodings.
The message stays a craft-kiwi tree read by field name; mapping it onto
a document stays in the app.

* craft-kiwi: no scan per field in schema decode; the text form keeps the limits

Schema::decode compared each message field id with every earlier one: 20
definitions of 10 000 fields, within every cap, took a billion
comparisons. A set per definition now.

Schema::parse applies the definition and field caps of the binary form,
looks type names up in an index instead of scanning the lines per field,
accepts a struct or message without fields, and refuses two fields of one
name and a message field id that is 0 (it ends a message) or repeated.
2026-11-11 18:08:28 -05:00

56 lines
2.3 KiB
Rust

//! The `.fig` container (Figma Design and FigJam local copies), read and written from public
//! format knowledge only.
//!
//! A `.fig` file is a zip archive holding `canvas.fig` (the document), `meta.json`, a thumbnail
//! and `images/<hash>` files. `canvas.fig` (also accepted on its own: the clipboard carries a bare
//! canvas) is an 8-byte `fig-…` magic, a little-endian `u32` version and length-prefixed chunks,
//! each compressed with deflate or zstd: a binary schema in the kiwi format ([`kiwi`]), then a
//! message encoded with it. Every file carries its own schema, so the message is decoded
//! generically and read by field name: a list of node changes (each with its id, parent id and
//! position among its siblings) that make up the layer tree, plus blobs: vector outlines
//! ([`path`]), editable vector networks ([`network`]) and the bytes of images that travel inside
//! the message ([`embedded_images`]).
//!
//! This crate is the wire layer: the archive ([`Archive`], [`write_archive`]), the canvas
//! ([`decode_canvas`], [`encode_canvas`]) and the blobs. What the node changes mean is read from
//! the decoded [`kiwi::Kv`] tree by the caller.
//!
//! Everything here reads untrusted bytes: sizes, counts and totals are bounded, and malformed
//! input is an error, never a panic.
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
mod archive;
mod canvas;
mod images;
pub mod network;
pub mod path;
#[cfg(test)]
mod tests;
pub use craft_kiwi as kiwi;
pub use archive::{Archive, Limits, Package, is_fig, write_archive};
pub use canvas::{
DESIGN_HEADER, JAM_HEADER, MAX_CANVAS, MAX_CHUNK, MAX_CHUNKS, MAX_SCHEMA, VERSION, chunks, decode_canvas, encode_canvas, inflate, value_budget,
};
pub use images::{MAX_IMAGE_HASH, embedded_aliases, embedded_copies, embedded_images};
/// Why a `.fig` file could not be read or written.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum Error {
/// The bytes are not a readable `.fig` file.
#[error(".fig file: {0}")]
Corrupt(String),
/// What was handed to a writer does not fit the format's limits.
#[error("the document is too large for a .fig file")]
TooLarge,
/// Writing failed.
#[error("{0}")]
Io(String),
}
pub(crate) fn corrupt(msg: impl Into<String>) -> Error {
Error::Corrupt(msg.into())
}