From 30710f5b8ce0dc85782b77eecbe83e2c4d118f39 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 06:50:58 +0000 Subject: [PATCH] feat(format): use shared native document JSON contract --- Cargo.lock | 24 +++++++++- Cargo.toml | 1 + crates/format/Cargo.toml | 1 + crates/format/src/lib.rs | 97 ++++++++-------------------------------- 4 files changed, 43 insertions(+), 80 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2544abc87..58cc4b56f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1018,7 +1018,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=d13ac58a15c45d618f2591 dependencies = [ "craft-figma-rest", "craft-kiwi", - "craft-zip", + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=d13ac58a15c45d618f25910772502e38c4ce1d4c)", "flate2", "kurbo", "ruzstd", @@ -1026,6 +1026,17 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-figcraft-format" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=111b522d0#111b522d05bcc75bb4f15eaa91496eb5f9c6a54f" +dependencies = [ + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=111b522d0)", + "serde", + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "craft-figma-rest" version = "0.1.0" @@ -1216,6 +1227,16 @@ dependencies = [ "wayland-client", ] +[[package]] +name = "craft-zip" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=111b522d0#111b522d05bcc75bb4f15eaa91496eb5f9c6a54f" +dependencies = [ + "flate2", + "thiserror 2.0.21", + "zip 9.1.1", +] + [[package]] name = "craft-zip" version = "0.1.0" @@ -1976,6 +1997,7 @@ name = "figcraft-format" version = "0.18.1" dependencies = [ "craft-fig", + "craft-figcraft-format", "craft-figma-rest", "figcraft-doc", "figcraft-layout", diff --git a/Cargo.toml b/Cargo.toml index cea4e7c9d..ee45efdab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,6 +60,7 @@ craft-settings = { git = "https://github.com/storytold/craft-libs", rev = "e11f9 # a .fig message onto the REST file JSON, and that JSON's shared helpers. These crates share # types, so they stay on one rev; they share none with the crates above. craft-fig = { git = "https://github.com/storytold/craft-libs", rev = "d13ac58a15c45d618f25910772502e38c4ce1d4c" } +craft-figcraft-format = { git = "https://github.com/storytold/craft-libs", rev = "111b522d0" } craft-figma-rest = { git = "https://github.com/storytold/craft-libs", rev = "d13ac58a15c45d618f25910772502e38c4ce1d4c" } # foundation diff --git a/crates/format/Cargo.toml b/crates/format/Cargo.toml index a548b7051..9424b95e0 100644 --- a/crates/format/Cargo.toml +++ b/crates/format/Cargo.toml @@ -21,4 +21,5 @@ sha1 = { workspace = true } sha2 = { workspace = true } image = { workspace = true } craft-fig = { workspace = true } +craft-figcraft-format = { workspace = true } craft-figma-rest = { workspace = true } diff --git a/crates/format/src/lib.rs b/crates/format/src/lib.rs index 3fb3049ae..6abf855cb 100644 --- a/crates/format/src/lib.rs +++ b/crates/format/src/lib.rs @@ -35,14 +35,11 @@ use std::io::{Read, Seek, Write}; use std::sync::Arc; use figcraft_doc::{Document, ImageData}; -use serde::de::{Deserializer, IgnoredAny, MapAccess, Visitor}; use serde::{Deserialize, Serialize, Serializer}; use serde_json::{Value, json}; use sha2::Digest; -pub const EXTENSION: &str = "figcraft"; -pub const FORMAT: &str = "figcraft"; -pub const VERSION: u32 = 1; +pub use craft_figcraft_format::{EXTENSION, FORMAT, VERSION}; /// Largest archive member we read (guards against zip bombs). const MAX_MEMBER: u64 = 512 * 1024 * 1024; /// `document.json` may unpack to [`DOCUMENT_RATIO`] times what it takes in the archive, within @@ -243,33 +240,14 @@ pub(crate) fn image_data(data: Vec) -> ImageData { ImageData { bytes: Arc::new(data), width: w, height: h } } -/// `document.json`: the format header, the image index and the document's own fields, written -/// straight from the document (no intermediate JSON tree). -#[derive(Serialize)] -struct DocumentJson<'a> { - format: &'static str, - version: u32, - #[serde(rename = "imageIndex", serialize_with = "image_index")] - images: &'a BTreeMap, - #[serde(flatten)] - doc: &'a Document, -} +/// The image hashes a `document.json` lists in its `imageIndex` (`None` without one). +type ImageIndex = Option>; -impl<'a> DocumentJson<'a> { - fn new(doc: &'a Document) -> Self { - Self { format: FORMAT, version: VERSION, images: &doc.images, doc } - } -} - -/// The `imageIndex`: `[{hash, width, height}]` for every image the document holds. -fn image_index(images: &&BTreeMap, s: S) -> Result { - #[derive(Serialize)] - struct Entry<'a> { - hash: &'a str, - width: u32, - height: u32, - } - s.collect_seq(images.iter().map(|(hash, i)| Entry { hash, width: i.width, height: i.height })) +fn native_image_index(doc: &Document) -> Vec { + doc.images + .iter() + .map(|(hash, image)| craft_figcraft_format::ImageIndexEntry { hash: hash.clone(), width: image.width, height: image.height }) + .collect() } /// A serializer adapter that writes `0` in place of every NaN or infinite float. JSON has no such @@ -452,13 +430,17 @@ impl serde::ser::SerializeStructVariant f /// Serialize the document (without images) to the JSON value stored in `document.json`. /// NaN and infinite numbers are written as `0`. pub fn to_json(doc: &Document) -> Result { - DocumentJson::new(doc).serialize(Finite(serde_json::value::Serializer)).map_err(io_err) + { + let mut bytes = Vec::new(); + craft_figcraft_format::write_document_json(&FiniteValue(doc), &native_image_index(doc), &mut bytes).map_err(io_err)?; + serde_json::from_slice(&bytes).map_err(io_err) + } } /// Write the document (without images) as `document.json` content to `w`. /// NaN and infinite numbers are written as `0`. pub fn write_json(doc: &Document, w: impl Write) -> Result<(), FormatError> { - DocumentJson::new(doc).serialize(Finite(&mut serde_json::Serializer::new(w))).map_err(io_err) + craft_figcraft_format::write_document_json(&FiniteValue(doc), &native_image_index(doc), w).map_err(io_err) } /// Parse `document.json` content. @@ -467,64 +449,21 @@ pub fn from_json(text: &str) -> Result<(Document, Vec), FormatError> { } /// The image hashes a `document.json` lists in its `imageIndex` (`None` without one). -type ImageIndex = Option>; - -/// The header of a `document.json` (its format version and image index), read in a pass that -/// skips over everything else without building it. -#[derive(Default)] -struct Header { - version: Option, - images: ImageIndex, -} - -impl<'de> Deserialize<'de> for Header { - fn deserialize>(d: D) -> Result { - struct HeaderVisitor; - impl<'de> Visitor<'de> for HeaderVisitor { - type Value = Header; - fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - f.write_str("a FigCraft document object") - } - fn visit_map>(self, mut map: A) -> Result { - let mut h = Header::default(); - while let Some(key) = map.next_key::()? { - match key.as_str() { - "version" => h.version = map.next_value::()?.as_u64(), - "imageIndex" => { - h.images = map - .next_value::()? - .as_array() - .map(|a| a.iter().filter_map(|i| i.get("hash").and_then(Value::as_str).map(str::to_string)).collect()); - } - _ => { - map.next_value::()?; - } - } - } - Ok(h) - } - } - d.deserialize_map(HeaderVisitor) - } -} /// Parse `document.json` content; also returns its [`ImageIndex`]. The header is checked first /// (a newer file is refused before its document is read), then the document is deserialized /// straight from the text. fn parse_json(text: &str) -> Result<(Document, Vec, ImageIndex), FormatError> { let corrupt = |e: serde_json::Error| FormatError::Corrupt(e.to_string()); - let header: Header = serde_json::from_str(text).map_err(corrupt)?; - if let Some(ver) = header.version - && ver > u64::from(VERSION) - { - return Err(FormatError::Newer(u32::try_from(ver).unwrap_or(u32::MAX))); - } + let header = craft_figcraft_format::read_header(text).map_err(corrupt)?; + craft_figcraft_format::check_version(&header).map_err(|e| FormatError::Newer(e.0))?; + let images = header.images.map(|hashes| hashes.into_iter().collect()); let mut doc: Document = serde_json::from_str(text).map_err(corrupt)?; let issues = doc.validate(); if !issues.is_empty() { doc.repair(); } - Ok((doc, issues, header.images)) + Ok((doc, issues, images)) } pub(crate) fn io_err(e: impl std::fmt::Display) -> FormatError {