Merge pull request #326 from storytold/codex/anicraft-native-envelope

refactor(format): share AniCraft native project envelope
This commit is contained in:
Dylan M. Taylor authored and GitHub committed 2026-11-14 00:10:55 -05:00
commit f15cde3864
4 files changed
+73 -69

No files matched your search

Generated
+10
View File
@@ -298,6 +298,7 @@ dependencies = [
"anicraft-svg",
"anicraft-text",
"anicraft-xfl",
"craft-anicraft-format",
"gif",
"image-webp",
"miniz_oxide 0.8.9",
@@ -1164,6 +1165,15 @@ dependencies = [
"libc",
]
[[package]]
name = "craft-anicraft-format"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=b77af389f30bb10de10d8436ca9b39fe94e3bb67#b77af389f30bb10de10d8436ca9b39fe94e3bb67"
dependencies = [
"serde",
"serde_json",
]
[[package]]
name = "craft-update"
version = "0.1.0"
+1
View File
@@ -27,6 +27,7 @@ anicraft-plugins = { path = "crates/plugins" }
anicraft-audio = { path = "crates/audio" }
anicraft-gpu = { path = "crates/gpu" }
anicraft-guide = { path = "crates/guide" }
craft-anicraft-format = { git = "https://github.com/storytold/craft-libs", rev = "b77af389f30bb10de10d8436ca9b39fe94e3bb67" }
# The GPU stage (ADR 0019) uses the wgpu that eframe's egui-wgpu brings, with the same
# (default) features, so the tree keeps one wgpu build.
wgpu = "30"
+1
View File
@@ -15,6 +15,7 @@ anicraft-xfl.workspace = true
anicraft-svg.workspace = true
anicraft-text.workspace = true
anicraft-plugins.workspace = true
craft-anicraft-format.workspace = true
serde.workspace = true
serde_json.workspace = true
thiserror.workspace = true
+61 -69
View File
@@ -3,85 +3,36 @@
use crate::EngineError;
use anicraft_doc::{Document, path_table};
use anicraft_geom::Path;
use serde::{Deserialize, Serialize};
use craft_anicraft_format::PayloadDecoder;
pub use craft_anicraft_format::{FORMAT_NAME, FORMAT_VERSION, MAX_FILE_BYTES};
use serde::Deserialize;
use std::sync::Arc;
pub const FORMAT_NAME: &str = "anicraft";
/// v1: inline tagged paths. v2: SVG path strings, shared paths stored once in `paths`.
/// v3: `scenes` (a list of named timelines) instead of one `timeline`; v1/v2 files load as one
/// scene.
pub const FORMAT_VERSION: u32 = 3;
/// Largest file we will parse.
pub const MAX_FILE_BYTES: u64 = 1024 * 1024 * 1024;
/// Files smaller than this (compact) are pretty-printed for humans and diffs.
const PRETTY_LIMIT: usize = 1024 * 1024;
/// AniCraft document deserialization uses its own path-table scope while the shared format crate
/// controls when a payload is decoded in the envelope stream.
struct DocumentDecoder;
/// Written in this field order: the path table precedes the document, so a reader can stream
/// the file in one pass with references already resolvable.
#[derive(Serialize)]
struct EnvelopeOut<'a> {
format: &'a str,
version: u32,
generator: String,
paths: &'a [Arc<Path>],
document: &'a Document,
}
impl PayloadDecoder<Arc<Path>> for DocumentDecoder {
type Document = Document;
/// Streaming reader: when `paths` arrives it opens a path-table scope for `document`.
struct EnvelopeIn {
format: String,
version: u32,
document: Option<Document>,
}
impl<'de> Deserialize<'de> for EnvelopeIn {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
struct V;
impl<'de> serde::de::Visitor<'de> for V {
type Value = EnvelopeIn;
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str("an .anicraft envelope object")
}
fn visit_map<A: serde::de::MapAccess<'de>>(self, mut map: A) -> Result<EnvelopeIn, A::Error> {
let (mut format, mut version, mut document) = (None, None, None);
let mut _scope = None;
while let Some(key) = map.next_key::<String>()? {
match key.as_str() {
"format" => format = Some(map.next_value::<String>()?),
"version" => version = Some(map.next_value::<u32>()?),
"paths" => {
let paths: Vec<Path> = map.next_value()?;
_scope = Some(path_table::begin_read(paths.into_iter().map(Arc::new).collect()));
}
"document" => document = Some(map.next_value::<Document>()?),
_ => {
map.next_value::<serde::de::IgnoredAny>()?;
}
}
}
Ok(EnvelopeIn {
format: format.ok_or_else(|| serde::de::Error::missing_field("format"))?,
version: version.ok_or_else(|| serde::de::Error::missing_field("version"))?,
document,
})
}
}
d.deserialize_map(V)
fn decode<'de, D>(&mut self, deserializer: D, paths: Option<Vec<Arc<Path>>>) -> Result<Self::Document, D::Error>
where
D: serde::Deserializer<'de>,
{
let _scope = paths.map(path_table::begin_read);
Document::deserialize(deserializer)
}
}
pub fn to_json(doc: &Document) -> Result<String, EngineError> {
let paths = path_table::build_table(doc);
let env = EnvelopeOut { format: FORMAT_NAME, version: FORMAT_VERSION, generator: format!("AniCraft {}", env!("CARGO_PKG_VERSION")), paths: &paths, document: doc };
let compact = path_table::write_with_paths(&paths, || serde_json::to_string(&env)).map_err(|e| EngineError::Format(e.to_string()))?;
if compact.len() < PRETTY_LIMIT {
return path_table::write_with_paths(&paths, || serde_json::to_string_pretty(&env)).map_err(|e| EngineError::Format(e.to_string()));
}
Ok(compact)
let generator = format!("AniCraft {}", env!("CARGO_PKG_VERSION"));
path_table::write_with_paths(&paths, || craft_anicraft_format::write(&generator, &paths, doc)).map_err(|e| EngineError::Format(e.to_string()))
}
pub fn from_json(text: &str) -> Result<Document, EngineError> {
let env: EnvelopeIn = serde_json::from_str(text).map_err(|e| EngineError::Format(format!("not a valid .anicraft file: {e}")))?;
let env = craft_anicraft_format::read::<Arc<Path>, _>(text, &mut DocumentDecoder)
.map_err(|e| EngineError::Format(format!("not a valid .anicraft file: {e}")))?;
if env.format != FORMAT_NAME {
return Err(EngineError::Format(format!("unexpected format '{}'", env.format)));
}
@@ -132,10 +83,48 @@ mod tests {
let back = from_json(&json).unwrap();
assert_eq!(d, back);
let l = &back.timeline(0).layers[0];
let (anicraft_doc::ElementKind::Shape(a), anicraft_doc::ElementKind::Shape(b)) = (&l.keyframes[0].elements[0].kind, &l.keyframes[3].elements[0].kind) else { panic!() };
let (anicraft_doc::ElementKind::Shape(a), anicraft_doc::ElementKind::Shape(b)) =
(&l.keyframes[0].elements[0].kind, &l.keyframes[3].elements[0].kind)
else {
panic!()
};
assert!(Arc::ptr_eq(&a.path, &b.path), "sharing restored on load");
}
#[test]
fn duplicate_envelope_keys_keep_last_values_and_path_table_scope() {
let doc = sample::stress(2, 40, 48);
let wire: serde_json::Value = serde_json::from_str(&to_json(&doc).unwrap()).unwrap();
let paths = wire["paths"].as_array().unwrap();
let mut earlier_paths = paths.clone();
earlier_paths.reverse();
assert_ne!(earlier_paths.as_slice(), paths.as_slice());
let json = format!(
"{{\"format\":\"other\",\"format\":\"anicraft\",\"version\":99,\"version\":3,\"paths\":{},\"paths\":{},\"document\":{},\"ignored\":true}}",
serde_json::to_string(&earlier_paths).unwrap(),
serde_json::to_string(paths).unwrap(),
serde_json::to_string(&wire["document"]).unwrap(),
);
assert_eq!(from_json(&json).unwrap(), doc);
}
#[test]
fn malformed_earlier_duplicate_document_keeps_error_precedence_and_location() {
let doc = sample::demo();
let wire: serde_json::Value = serde_json::from_str(&to_json(&doc).unwrap()).unwrap();
let later_document = serde_json::to_string(&wire["document"]).unwrap();
let json = format!("{{\n \"format\":\"anicraft\",\n \"version\":3,\n \"document\":null,\n \"document\":{later_document}\n}}");
let error = from_json(&json).err().map(|e| e.to_string()).unwrap_or_default();
assert!(error.contains("not a valid .anicraft file: invalid type: null"), "{error}");
assert!(error.contains("line 4"), "payload diagnostics keep source locations: {error}");
}
#[test]
fn absent_document_keeps_the_native_format_error() {
let error = from_json(r#"{"format":"anicraft","version":3}"#).err().map(|e| e.to_string()).unwrap_or_default();
assert!(error.contains("the file has no document"), "{error}");
}
#[test]
fn version_1_files_still_load() {
let v1 = r#"{"format":"anicraft","version":1,"document":{"timeline":{"layers":[{"id":1,"name":"L","keyframes":[{"start":0,"duration":2,"elements":[
@@ -162,7 +151,10 @@ mod tests {
#[test]
fn missing_fields_default() {
let d = from_json(r#"{"format":"anicraft","version":1,"document":{"stage":{"width":0,"height":50,"fps":-3,"background":{"r":2,"g":0,"b":0,"a":1}}}}"#).unwrap();
let d = from_json(
r#"{"format":"anicraft","version":1,"document":{"stage":{"width":0,"height":50,"fps":-3,"background":{"r":2,"g":0,"b":0,"a":1}}}}"#,
)
.unwrap();
assert_eq!(d.stage.width, 1);
assert_eq!(d.stage.fps, 24.0);
assert_eq!(d.stage.background.r, 1.0);