mirror of
https://github.com/storytold/figcraft.git
synced 2026-11-15 23:02:36 +00:00
Finish FigCraft native shared format cutover
This commit is contained in:
1 parent
821f9a8631
commit
6ad04aa4d2
6 files changed
+81
-26
No files matched your search
Generated
+3
-3
@@ -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",
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
@@ -395,11 +395,11 @@ pub fn from_json(text: &str) -> Result<(Document, Vec<String>), 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<String>, 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::<Document>(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;
|
||||
|
||||
@@ -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"));
|
||||
|
||||
@@ -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<u8> {
|
||||
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))));
|
||||
}
|
||||
@@ -160,23 +160,13 @@ pub fn archive_preview<R: Read + Seek>(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<u64>,
|
||||
#[serde(default)]
|
||||
nodes: std::collections::BTreeMap<String, Node>,
|
||||
#[serde(default)]
|
||||
kind: figcraft_doc::FileKind,
|
||||
}
|
||||
#[derive(serde::Deserialize)]
|
||||
struct Node {
|
||||
#[serde(default)]
|
||||
background: Option<serde_json::Value>,
|
||||
}
|
||||
let Ok(head) = serde_json::from_slice::<Head>(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`).
|
||||
|
||||
Reference in new issue
Block a user