diff --git a/Cargo.lock b/Cargo.lock index b0f05a3b0..761575903 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2691,6 +2691,8 @@ name = "craft-tcraft" version = "0.1.0" dependencies = [ "craft-zip", + "serde", + "serde_json", "thiserror 2.0.21", ] diff --git a/crates/tcraft/Cargo.toml b/crates/tcraft/Cargo.toml index 76eeeafb0..3ce35abfb 100644 --- a/crates/tcraft/Cargo.toml +++ b/crates/tcraft/Cargo.toml @@ -12,4 +12,6 @@ workspace = true [dependencies] craft-zip = { workspace = true } +serde = { workspace = true } +serde_json = { workspace = true } thiserror = { workspace = true } diff --git a/crates/tcraft/src/lib.rs b/crates/tcraft/src/lib.rs index 4646d8717..730ef3b0c 100644 --- a/crates/tcraft/src/lib.rs +++ b/crates/tcraft/src/lib.rs @@ -6,10 +6,12 @@ //! envelope. Reads use a caller-owned budget shared with other archive entries. No reader opens //! a file or reserves memory based on a claimed payload before checking it. -use std::collections::HashSet; +use std::collections::{BTreeMap, HashSet}; use std::io::{self, Read, Write}; use craft_zip::Budget; +use serde::{Deserialize, Serialize}; +use serde_json::Value; pub const TILE_SIDE: u32 = 64; pub const TILE_AREA: usize = 64 * 64; @@ -32,6 +34,34 @@ pub enum Error { Io(#[from] io::Error), } +/// Neutral top-level project schema. Nested app-domain values and unknown fields remain JSON +/// so they round-trip without requiring TextureCraft's in-memory model. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct ProjectDocument { + pub format_version: u32, + pub name: String, + pub mesh: String, + pub normal_format: Value, + pub texture_sets: Vec, + pub layers: BTreeMap, + pub next_id: u64, + pub display: Value, + #[serde(default)] + pub cameras: Vec, + #[serde(flatten)] + pub extra: BTreeMap, +} + +/// Read only the version before an application applies its own recovery policy. Serde's +/// structured parse rejects a duplicate or invalid version field, like the project reader. +pub fn project_version(bytes: &[u8]) -> Result { + #[derive(Deserialize)] + struct Version { + format_version: u32, + } + Ok(serde_json::from_slice::(bytes)?.format_version) +} + /// Width, height and number of components expected by the enclosing project schema. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Layout { @@ -243,6 +273,24 @@ mod tests { bytes } + #[test] + fn project_schema_keeps_nested_and_future_fields() { + let json = serde_json::json!({ + "format_version": 2, "name": "Stone", "mesh": "mesh.obj", "normal_format": "openGl", + "texture_sets": [{"name": "main", "futureSet": {"keep": true}}], + "layers": {"1": {"kind": "paint", "futureLayer": [1, 2]}}, "next_id": 2, + "display": {"futureDisplay": 4}, "mesh_file": "source.obj", "futureProject": [3] + }); + let bytes = serde_json::to_vec(&json).unwrap(); + assert_eq!(project_version(&bytes).unwrap(), 2); + let document: ProjectDocument = serde_json::from_slice(&bytes).unwrap(); + let mut expected = json; + expected["cameras"] = serde_json::json!([]); + assert_eq!(serde_json::to_value(document).unwrap(), expected); + assert!(project_version(br#"{"format_version":1,"format_version":2}"#).is_err()); + assert!(project_version(br#"{"format_version":-1}"#).is_err()); + } + #[test] fn baked_map_matches_the_native_little_endian_wire_layout() { let layout = Layout { width: 2, height: 1, components: 1 };