diff --git a/Cargo.lock b/Cargo.lock index 083d59554..879856783 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1141,6 +1141,17 @@ dependencies = [ "objc2-app-kit 0.3.2", ] +[[package]] +name = "craft-penpot" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=064afccce82b01cbf85da5e265bcf897553bea2a#064afccce82b01cbf85da5e265bcf897553bea2a" +dependencies = [ + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=064afccce82b01cbf85da5e265bcf897553bea2a)", + "serde", + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "craft-quit" version = "0.1.0" @@ -1267,6 +1278,16 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-zip" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=064afccce82b01cbf85da5e265bcf897553bea2a#064afccce82b01cbf85da5e265bcf897553bea2a" +dependencies = [ + "flate2", + "thiserror 2.0.21", + "zip 9.1.1", +] + [[package]] name = "craft-zip" version = "0.1.0" @@ -2049,6 +2070,7 @@ dependencies = [ "craft-fig", "craft-figcraft-format", "craft-figma-rest 0.1.0 (git+https://github.com/storytold/craft-libs?rev=d13ac58a15c45d618f25910772502e38c4ce1d4c)", + "craft-penpot", "craft-sketch", "figcraft-doc", "figcraft-layout", diff --git a/Cargo.toml b/Cargo.toml index 57cc46f7a..82831e556 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -58,6 +58,9 @@ craft-menu = { git = "https://github.com/storytold/craft-libs", rev = "e11f9d9ae # Where the settings live: the suite's folder rules, portable mode included (craft-libs). craft-settings = { git = "https://github.com/storytold/craft-libs", rev = "e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545" } +# Penpot export container only; JSON values cross this boundary, not shared model types. +craft-penpot = { git = "https://github.com/storytold/craft-libs", rev = "064afccce82b01cbf85da5e265bcf897553bea2a" } + # The design-file formats (craft-libs): the .fig container and its kiwi encoding, the mapping of # 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. diff --git a/crates/format/Cargo.toml b/crates/format/Cargo.toml index b5673119d..d988a7269 100644 --- a/crates/format/Cargo.toml +++ b/crates/format/Cargo.toml @@ -20,6 +20,7 @@ ruzstd = { workspace = true } sha1 = { workspace = true } sha2 = { workspace = true } image = { workspace = true } +craft-penpot = { workspace = true } craft-sketch = { workspace = true } craft-fig = { workspace = true } craft-figcraft-format = { workspace = true } diff --git a/crates/format/src/lib.rs b/crates/format/src/lib.rs index 167444c62..1c246bb89 100644 --- a/crates/format/src/lib.rs +++ b/crates/format/src/lib.rs @@ -47,6 +47,7 @@ pub use craft_figcraft_format::{ MAX_VERSION_INDEX, MAX_VERSION_NAME, MAX_VERSIONS, ReadBudget, VERSION, VersionEntry as NativeVersionEntry, VersionIndex as NativeVersionIndex, clip, document_limit, is_valid_version_id, }; +#[cfg(test)] const JSON_LEVEL: i64 = 3; /// The export warning for colour calculations that resolve to nothing (an operand missing, or more /// than 32 variables to read), shared by the Penpot and `.fig` exports. @@ -411,7 +412,8 @@ pub(crate) fn io_err(e: impl std::fmt::Display) -> FormatError { FormatError::Io(e.to_string()) } -/// JSON compression for Penpot and external ZIP exporters. +/// JSON compression for legacy Penpot test fixtures. +#[cfg(test)] pub(crate) fn json_options() -> zip::write::SimpleFileOptions { zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated).compression_level(Some(JSON_LEVEL)) } diff --git a/crates/format/src/penpot/export.rs b/crates/format/src/penpot/export.rs index d0b9d3046..071809cab 100644 --- a/crates/format/src/penpot/export.rs +++ b/crates/format/src/penpot/export.rs @@ -1,5 +1,5 @@ use std::collections::{BTreeMap, BTreeSet}; -use std::io::{Cursor, Write}; +use std::io::Cursor; use figcraft_doc::color::{BlendMode, Color, Gradient, GradientKind, ImageScaleMode, Paint, PaintKind}; use figcraft_doc::geom::kurbo::PathEl; @@ -13,7 +13,7 @@ use figcraft_doc::{ use serde_json::{Map, Value, json}; use super::{ROOT_ID, fingerprint, layout_fingerprint, strokes_fingerprint, text_fingerprint, tokens_fingerprint, uuid}; -use crate::{FormatError, io_err, json_options}; +use crate::{FormatError, io_err}; const DEFAULT_FEATURES: &[&str] = &["fdata/path-data", "design-tokens/v1", "variants/v1", "layout/grid", "components/v2", "fdata/shape-data-type"]; @@ -565,27 +565,19 @@ impl<'a> Exporter<'a> { "files": [{"id": self.file_id, "name": name, "features": file.get("features").cloned().unwrap_or(Value::Null)}], "relations": [], }); - let mut buf = Cursor::new(Vec::new()); - { - let mut z = zip::ZipWriter::new(&mut buf); - let mut put = |path: &str, bytes: &[u8]| -> Result<(), FormatError> { - z.start_file(path, json_options()).map_err(io_err)?; - z.write_all(bytes).map_err(io_err) - }; - put("manifest.json", &serde_json::to_vec_pretty(&manifest).map_err(io_err)?)?; - put(&format!("{base}.json"), &serde_json::to_vec_pretty(&file).map_err(io_err)?)?; - if let Some(tokens) = tokens { - put(&format!("{base}/tokens.json"), tokens.as_bytes())?; - } - for (path, value) in &entries { - put(path, &serde_json::to_vec_pretty(value).map_err(io_err)?)?; - } - for (path, bytes) in &blobs { - put(path, bytes)?; - } - z.finish().map_err(io_err)?; + let mut writer = craft_penpot::Writer::new(); + writer.add("manifest.json", &serde_json::to_vec_pretty(&manifest).map_err(io_err)?).map_err(io_err)?; + writer.add(&format!("{base}.json"), &serde_json::to_vec_pretty(&file).map_err(io_err)?).map_err(io_err)?; + if let Some(tokens) = tokens { + writer.add(&format!("{base}/tokens.json"), tokens.as_bytes()).map_err(io_err)?; } - Ok((buf.into_inner(), self.warnings)) + for (path, value) in &entries { + writer.add(path, &serde_json::to_vec_pretty(value).map_err(io_err)?).map_err(io_err)?; + } + for (path, bytes) in &blobs { + writer.add(path, bytes).map_err(io_err)?; + } + Ok((writer.finish().map_err(io_err)?, self.warnings)) } fn page_json(&self, page: NodeId, page_id: &str, index: usize, background: Color) -> Value { diff --git a/crates/format/src/penpot/export_tests.rs b/crates/format/src/penpot/export_tests.rs index 1e9e95638..e3ad744ef 100644 --- a/crates/format/src/penpot/export_tests.rs +++ b/crates/format/src/penpot/export_tests.rs @@ -1027,3 +1027,19 @@ fn stored_variable_ids_keep_penpot_token_identity() { assert_eq!(width, &expected); } } + +#[test] +fn shared_writer_keeps_manifest_first_and_deflated_members() { + let doc = Document::new("Shared container"); + let (bytes, _) = export(&doc).unwrap(); + let mut zip = zip::ZipArchive::new(Cursor::new(bytes.as_slice())).unwrap(); + assert_eq!(zip.by_index(0).unwrap().name(), "manifest.json"); + for index in 0..zip.len() { + assert_eq!(zip.by_index(index).unwrap().compression(), zip::CompressionMethod::Deflated); + } + let archive = craft_penpot::Archive::open(&bytes).unwrap(); + assert_eq!(archive.files.len(), 1); + assert_eq!(archive.files[0].name.as_deref(), Some("Shared container")); + assert_eq!(archive.manifest["generatedBy"], format!("figcraft/{}", env!("CARGO_PKG_VERSION"))); + assert!(import(&bytes).is_ok()); +}