Own native JSON schema in streaming writer

This commit is contained in:
Dylan M. Taylor committed 2026-11-15 14:46:50 +00:00
1 parent 0cdda6e15d
commit d1ee17aca4
2 files changed
+78 -1

No files matched your search

+53 -1
View File
@@ -4,9 +4,61 @@
//! Extracted from VectorCraft, licensed under MIT OR Apache-2.0.
use serde::Serialize;
use std::collections::HashSet;
use std::collections::{BTreeMap, HashSet};
use std::io::{self, Write};
/// Native file metadata for streaming a JSON envelope. Asset entries should be prepared using
/// the same [`crate::Tail`] whose parts will be written after the JSON.
pub struct FileHeader<'a> {
/// Native format version to write.
pub version: u32,
/// Application generator string.
pub generator: &'a str,
/// Optional saved PDF compatibility preference.
pub pdf_compatible: Option<bool>,
/// Optional inline preview.
pub preview: Option<&'a crate::Image>,
/// Embedded profiles keyed by profile name.
pub profiles: &'a BTreeMap<&'a str, crate::Image>,
/// Optional PDF entry.
pub pdf: Option<&'a crate::Image>,
}
/// Write the native JSON envelope, streaming the document through `document`. The callback
/// returns the image entries after serializing the document, allowing it to collect image keys
/// without keeping a copy of the document JSON. It must write one complete JSON value.
pub fn write_file_json<'a>(
mut out: impl Write,
header: &FileHeader<'a>,
pretty: bool,
document: impl FnOnce(&mut dyn Write) -> io::Result<BTreeMap<&'a str, crate::Image>>,
) -> io::Result<()> {
if !(crate::MIN_VERSION..=crate::VERSION).contains(&header.version) {
return Err(io::Error::new(io::ErrorKind::InvalidInput, "unsupported native file version"));
}
let mut env = Envelope::new(pretty);
let format = if header.version < 3 { crate::LEGACY_EXTENSION } else { crate::EXTENSION };
env.field(&mut out, "format", &format)?;
env.field(&mut out, "version", &header.version)?;
env.field(&mut out, "generator", &header.generator)?;
if let Some(compatible) = header.pdf_compatible {
env.field(&mut out, "pdfCompatible", &compatible)?;
}
if let Some(preview) = header.preview {
env.field(&mut out, "preview", preview)?;
}
if !header.profiles.is_empty() {
env.field(&mut out, "profiles", header.profiles)?;
}
if let Some(pdf) = header.pdf {
env.field(&mut out, "pdf", pdf)?;
}
env.key(&mut out, "document")?;
let images = document(&mut Indent { inner: &mut out, on: pretty })?;
env.field(&mut out, "images", &images)?;
env.finish(&mut out)
}
/// Indents JSON text written through it one level (two spaces after each newline), as a member
/// of the file's top-level object; newlines only occur between tokens, inside strings they are
/// escaped.
+25
View File
@@ -81,3 +81,28 @@ fn tail_calls_hook_in_order_and_propagates_errors() {
.unwrap();
assert!(!called);
}
#[test]
fn native_schema_order_and_versions() {
use craft_vectorcraft_format::stream::{FileHeader, write_file_json};
let profiles = Default::default();
for (version, name) in [(1, "drawcraft"), (4, "vectorcraft")] {
for pretty in [false, true] {
let header = FileHeader { version, generator: "test", pdf_compatible: Some(false), preview: None, profiles: &profiles, pdf: None };
let mut out = Vec::new();
write_file_json(&mut out, &header, pretty, |w| {
w.write_all(if pretty { b"{\n \"key\": \"image\"\n}" } else { b"{\"key\":\"image\"}" })?;
Ok(Default::default())
})
.unwrap();
let value: serde_json::Value = serde_json::from_slice(&out).unwrap();
assert_eq!(value["format"], name);
assert_eq!(value["pdfCompatible"], false);
let text = std::str::from_utf8(&out).unwrap();
assert!(text.find("\"format\"").unwrap() < text.find("\"version\"").unwrap());
assert!(text.find("\"document\"").unwrap() < text.find("\"images\"").unwrap());
}
}
let header = FileHeader { version: 0, generator: "test", pdf_compatible: None, preview: None, profiles: &profiles, pdf: None };
assert!(write_file_json(Vec::new(), &header, false, |_| Ok(Default::default())).is_err());
}