From 6ad04aa4d2696767ec9c026055612722ea259105 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 11:54:50 +0000 Subject: [PATCH] Finish FigCraft native shared format cutover --- Cargo.lock | 6 +-- Cargo.toml | 2 +- crates/format/src/lib.rs | 10 +++-- crates/format/src/library.rs | 3 +- crates/format/src/streaming_tests.rs | 64 ++++++++++++++++++++++++++++ crates/format/src/thumbnail.rs | 22 +++------- 6 files changed, 81 insertions(+), 26 deletions(-) create mode 100644 crates/format/src/streaming_tests.rs diff --git a/Cargo.lock b/Cargo.lock index 4e56ccd07..bf72b5ac4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1029,9 +1029,9 @@ dependencies = [ [[package]] name = "craft-figcraft-format" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=c1d198925#c1d198925f10a640c4623cf9c1c0f775ff2d972e" +source = "git+https://github.com/storytold/craft-libs?rev=9aac9301a#9aac9301a92ec7f8db16760b6de0f0576f4bd52c" dependencies = [ - "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c1d198925)", + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=9aac9301a)", "serde", "serde_json", "thiserror 2.0.21", @@ -1231,7 +1231,7 @@ dependencies = [ [[package]] name = "craft-zip" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=c1d198925#c1d198925f10a640c4623cf9c1c0f775ff2d972e" +source = "git+https://github.com/storytold/craft-libs?rev=9aac9301a#9aac9301a92ec7f8db16760b6de0f0576f4bd52c" dependencies = [ "flate2", "thiserror 2.0.21", diff --git a/Cargo.toml b/Cargo.toml index 1e51bcecd..de9e5c937 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,7 +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 = "c1d198925" } +craft-figcraft-format = { git = "https://github.com/storytold/craft-libs", rev = "9aac9301a" } craft-figma-rest = { git = "https://github.com/storytold/craft-libs", rev = "d13ac58a15c45d618f25910772502e38c4ce1d4c" } # foundation diff --git a/crates/format/src/lib.rs b/crates/format/src/lib.rs index 011ffff74..167444c62 100644 --- a/crates/format/src/lib.rs +++ b/crates/format/src/lib.rs @@ -395,11 +395,11 @@ pub fn from_json(text: &str) -> Result<(Document, Vec), FormatError> { /// (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 = craft_figcraft_format::read_header(text).map_err(corrupt)?; - craft_figcraft_format::check_version(&header).map_err(|e| FormatError::Newer(e.0))?; + let (mut doc, header) = craft_figcraft_format::read_document_json::(text).map_err(|e| match e { + craft_figcraft_format::ReadDocumentError::Json(error) => FormatError::Corrupt(error.to_string()), + craft_figcraft_format::ReadDocumentError::NewerVersion(error) => FormatError::Newer(error.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(); @@ -939,6 +939,8 @@ mod large_tests; #[cfg(test)] mod staging_tests; #[cfg(test)] +mod streaming_tests; +#[cfg(test)] mod thumbnail_tests; #[cfg(test)] mod unpacked_total_tests; diff --git a/crates/format/src/library.rs b/crates/format/src/library.rs index 5c09e19a3..8ed11fedb 100644 --- a/crates/format/src/library.rs +++ b/crates/format/src/library.rs @@ -129,8 +129,7 @@ pub(crate) fn read( if !z.contains(native::MANIFEST) { return Ok(None); } - let wire_manifest: native::Manifest = - serde_json::from_slice(&unpacked.read(z, native::MANIFEST, native::MAX_MANIFEST_BYTES)?).map_err(corrupt)?; + let wire_manifest = native::read_manifest(&unpacked.read(z, native::MANIFEST, native::MAX_MANIFEST_BYTES)?).map_err(corrupt)?; let mut manifest = from_native_manifest(wire_manifest); if !is_valid_library_id(&manifest.id) { return Err(corrupt("the library has an invalid id")); diff --git a/crates/format/src/streaming_tests.rs b/crates/format/src/streaming_tests.rs new file mode 100644 index 000000000..daf7ecc18 --- /dev/null +++ b/crates/format/src/streaming_tests.rs @@ -0,0 +1,64 @@ +//! The seekable on-disk reader uses the same format and recovery behavior as the byte reader. + +#![cfg(not(target_arch = "wasm32"))] + +use std::sync::Arc; + +use figcraft_doc::{Document, NodeKind}; + +use super::*; + +fn png(red: u8) -> Vec { + let image = image::RgbaImage::from_pixel(2, 2, image::Rgba([red, 0, 0, 255])); + let mut bytes = Vec::new(); + image.write_to(&mut std::io::Cursor::new(&mut bytes), image::ImageFormat::Png).unwrap(); + bytes +} + +#[test] +fn seekable_reader_matches_byte_reader_for_history_and_branch_archives() { + let mut document = Document::new("Current"); + let current_image = add_image(&mut document, png(10)).unwrap().0; + + let mut older = Document::new("Older"); + let old_image = add_image(&mut older, png(20)).unwrap().0; + let version = Version::new("v1", "Before", "", 1, Arc::new(older.clone())); + + let mut head = document.clone(); + let page = head.first_page().unwrap(); + head.create(page, NodeKind::Rectangle, "Branch shape", 1.0, 2.0, 3.0, 4.0).unwrap(); + add_image(&mut head, png(30)).unwrap(); + let branch = Branch { + id: "b1".into(), + name: "Explore".into(), + time: 2, + client: 4, + state: BranchState::Active, + base: Arc::new(document.clone()), + versions: vec![Version::new("v2", "Branch base", "", 3, Arc::new(document.clone()))], + head: Arc::new(head), + }; + + let bytes = save_bytes_with_history(&document, None, &[version], &[branch]).unwrap(); + let from_bytes = load_bytes_with_versions(&bytes).unwrap(); + assert_eq!(from_bytes.versions.len(), 1); + assert_eq!(from_bytes.branches.len(), 1); + assert!(from_bytes.doc.images.contains_key(¤t_image)); + assert!(from_bytes.versions[0].images().contains_key(&old_image)); + + let path = std::env::temp_dir().join(format!("figcraft-stream-reader-{}.figcraft", std::process::id())); + std::fs::write(&path, &bytes).unwrap(); + let from_file = load_file(&path).unwrap(); + let _ = std::fs::remove_file(&path); + assert_eq!(from_file, (from_bytes.doc, from_bytes.issues)); +} + +#[test] +fn seekable_reader_keeps_newer_document_error_precedence() { + let bytes = crate::zip_files([("document.json", br#"{"version":999,"document":["malformed"]}"#.as_slice())]).unwrap(); + let path = std::env::temp_dir().join(format!("figcraft-stream-newer-{}.figcraft", std::process::id())); + std::fs::write(&path, bytes).unwrap(); + let result = load_file(&path); + let _ = std::fs::remove_file(&path); + assert!(matches!(result, Err(FormatError::Newer(999)))); +} diff --git a/crates/format/src/thumbnail.rs b/crates/format/src/thumbnail.rs index 530038095..dc1f4eaff 100644 --- a/crates/format/src/thumbnail.rs +++ b/crates/format/src/thumbnail.rs @@ -160,23 +160,13 @@ pub fn archive_preview(archive: R, fit: u32) -> Preview { /// The first page's background in a `.figcraft` `document.json`, and the file's kind. fn figcraft_page(json: &[u8]) -> (Option<[f32; 4]>, figcraft_doc::FileKind) { - #[derive(serde::Deserialize)] - struct Head { - #[serde(default)] - pages: Vec, - #[serde(default)] - nodes: std::collections::BTreeMap, - #[serde(default)] - kind: figcraft_doc::FileKind, - } - #[derive(serde::Deserialize)] - struct Node { - #[serde(default)] - background: Option, - } - let Ok(head) = serde_json::from_slice::(json) else { return (None, figcraft_doc::FileKind::Design) }; + let Ok(head) = craft_figcraft_format::read_preview_document(json) else { return (None, figcraft_doc::FileKind::Design) }; let page = head.pages.first().and_then(|first| head.nodes.get(&first.to_string())).and_then(|n| n.background.as_ref()).and_then(rgba); - (page, head.kind) + let kind = match head.kind { + craft_figcraft_format::PreviewKind::Design => figcraft_doc::FileKind::Design, + craft_figcraft_format::PreviewKind::Board => figcraft_doc::FileKind::Board, + }; + (page, kind) } /// The canvas colour a `.fig` file's `meta.json` gives (`client_meta.background_color`).