Add neutral project document schema

This commit is contained in:
Dylan M. Taylor committed 2026-11-15 12:34:47 +00:00
1 parent 5f46c0ab25
commit b85e4b150b
3 files changed
+53 -1

No files matched your search

Generated
+2
View File
@@ -2691,6 +2691,8 @@ name = "craft-tcraft"
version = "0.1.0"
dependencies = [
"craft-zip",
"serde",
"serde_json",
"thiserror 2.0.21",
]
+2
View File
@@ -12,4 +12,6 @@ workspace = true
[dependencies]
craft-zip = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
thiserror = { workspace = true }
+49 -1
View File
@@ -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<Value>,
pub layers: BTreeMap<String, Value>,
pub next_id: u64,
pub display: Value,
#[serde(default)]
pub cameras: Vec<Value>,
#[serde(flatten)]
pub extra: BTreeMap<String, Value>,
}
/// 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<u32, serde_json::Error> {
#[derive(Deserialize)]
struct Version {
format_version: u32,
}
Ok(serde_json::from_slice::<Version>(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 };