Merge branch 'main' of https://github.com/storytold/craft-libs into feat/score-writers

This commit is contained in:
Dylan M. Taylor
2026-11-16 23:28:26 -05:00
4 changed files with 67 additions and 12 deletions
+34 -7
View File
@@ -382,16 +382,43 @@ pub fn manifest(generated_by: &str, files: &[(&str, &str, Value)]) -> Value {
serde_json::json!({"type": MANIFEST_TYPE, "version": 1, "generatedBy": generated_by, "refer": "penpot", "files": files, "relations": []})
}
/// Incremental in-memory Penpot container writer. Members are compressed as they are added;
/// the writer retains their names and archive output, not a second collection of payloads.
/// Paths must be safe and distinct. Entries keep insertion order and use deflate level 3.
/// If [`Writer::add`] fails, discard the writer: additions are not transactional, and a ZIP
/// failure may leave a reserved member name or partially written output. Do not finish or reuse
/// a writer after an error.
#[derive(Default)]
pub struct Writer {
zip: craft_zip::Writer,
seen: BTreeSet<String>,
}
impl Writer {
pub fn new() -> Self {
Self::default()
}
/// Write one member immediately; the caller may release its serialized payload afterwards.
/// On any error, discard this writer rather than retrying or calling [`Writer::finish`].
pub fn add(&mut self, path: &str, bytes: &[u8]) -> Result<(), Error> {
if path.is_empty() || !safe_name(path) || !self.seen.insert(path.to_owned()) {
return Err(Error::Io(format!("unsafe or repeated member path: {}", path.chars().take(200).collect::<String>())));
}
self.zip.add(path, bytes, craft_zip::Compression::Level(3)).map_err(|e| Error::Io(e.to_string()))
}
pub fn finish(self) -> Result<Vec<u8>, Error> {
self.zip.finish().map_err(|e| Error::Io(e.to_string()))
}
}
/// A Penpot archive of `members` (path, bytes), deflated, in the order given. Paths must be safe
/// and distinct, as [`Archive::open`] requires of what it reads.
pub fn write_archive<'a>(members: impl IntoIterator<Item = (&'a str, &'a [u8])>) -> Result<Vec<u8>, Error> {
let mut z = craft_zip::Writer::new();
let mut seen = BTreeSet::new();
let mut writer = Writer::new();
for (path, bytes) in members {
if path.is_empty() || !safe_name(path) || !seen.insert(path) {
return Err(Error::Io(format!("unsafe or repeated member path: {}", path.chars().take(200).collect::<String>())));
}
z.add(path, bytes, craft_zip::Compression::Level(3)).map_err(|e| Error::Io(e.to_string()))?;
writer.add(path, bytes)?;
}
z.finish().map_err(|e| Error::Io(e.to_string()))
writer.finish()
}
+20
View File
@@ -438,3 +438,23 @@ fn an_archive_declaring_too_many_members_is_refused_before_it_is_indexed() {
let bomb = craft_zip::zip64_entry_count_sample(MAX_ENTRIES as u64 + 1);
assert_eq!(Archive::open(&bomb).err(), Some(Error::Corrupt("too many ZIP members".into())));
}
#[test]
fn incremental_writer_keeps_members_and_rejects_ambiguous_paths() {
let all = members();
let mut writer = Writer::new();
for (name, bytes) in &all {
writer.add(name, bytes).unwrap();
}
let output = writer.finish().unwrap();
assert_eq!(output, write_archive(all.iter().map(|(n, b)| (n.as_str(), b.as_slice()))).unwrap());
let mut archive = Archive::open(&output).unwrap();
assert_eq!(archive.names().first().map(String::as_str), Some("manifest.json"));
assert_eq!(archive.page_shapes(FILE, PAGE).unwrap().len(), 2);
for bad in ["../x", "/abs", "a/./b", "a\\b", "", "a\u{1}b"] {
assert!(Writer::new().add(bad, b"x").is_err());
}
let mut duplicate = Writer::new();
duplicate.add("a", b"first").unwrap();
assert!(duplicate.add("a", b"second").is_err());
}
+9 -5
View File
@@ -1010,7 +1010,7 @@ const NAMES_XML: &str = r#"<workbook xmlns="http://schemas.openxmlformats.org/sp
fn xml_names(xml: &str) -> Vec<craft_xlsx::DefinedName> {
let types = r#"<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/></Types>"#;
let root = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="r" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/></Relationships>"#;
let bytes = zip(&[("[Content_Types].xml", types.as_bytes()), ("_rels/.rels", root.as_bytes()), ("xl/workbook.xml", xml.as_bytes())]);
let bytes = zip(&[("[Content_Types].xml", types.as_bytes()), ("_rels/.rels", root.as_bytes()), ("xl/workbook.xml", xml.as_bytes())]).unwrap();
Package::open(&bytes, &Limits::default()).unwrap().workbook().unwrap().defined_names
}
@@ -1146,7 +1146,8 @@ fn xml_sheet(xml: &str) -> (Vec<Row>, SheetPart) {
("xl/workbook.xml", book.as_bytes()),
("xl/_rels/workbook.xml.rels", rels.as_bytes()),
("xl/sheet.xml", xml.as_bytes()),
]);
])
.unwrap();
let (rows, part, _) = sizes_of(&bytes).unwrap();
(rows, part)
}
@@ -1256,6 +1257,7 @@ fn styled_file(styles: &[u8], types: &str) -> Vec<u8> {
("data/two.bin", &worksheet(&[], &[])),
("look/styles.bin", styles),
])
.unwrap()
}
/// The styles part of a package, and the warnings reading it gave.
@@ -1440,7 +1442,8 @@ fn xml_styles(xml: &str) -> Styles {
("xl/workbook.xml", br#"<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"/>"#),
("xl/_rels/workbook.xml.rels", rels.as_bytes()),
("xl/styles.xml", xml.as_bytes()),
]);
])
.unwrap();
styles_of(&bytes).0.unwrap()
}
@@ -1793,7 +1796,7 @@ fn book_with_tables(data: &[u8], tables: &[Vec<u8>]) -> Vec<u8> {
("text/strings.bin", &strings),
];
entries.extend(names.iter().map(String::as_str).zip(tables.iter().map(Vec::as_slice)));
zip(&entries)
zip(&entries).unwrap()
}
/// Sheet one's tables through `Package`, and its warnings.
@@ -1834,7 +1837,8 @@ fn a_name_using_a_structured_reference_is_decoded_after_tables_are_indexed() {
("data/two.bin", two.as_slice()),
("text/strings.bin", strings.as_slice()),
("tables/t1.bin", table.as_slice()),
]);
])
.unwrap();
let names = Package::open(&bytes, &Limits::default()).unwrap().workbook().unwrap().defined_names;
assert_eq!(names.iter().find(|n| n.name == "Vals").map(|n| n.formula.as_str()), Some("Sales[Val]"));
let wb = read(&bytes).unwrap();
+4
View File
@@ -90,6 +90,10 @@ mod tests {
fn set_permissions(&self, file: &File, perms: Permissions) -> io::Result<()> {
RealFs.set_permissions(file, perms)
}
#[cfg(unix)]
fn set_group(&self, file: &File, gid: u32) -> io::Result<()> {
RealFs.set_group(file, gid)
}
}
fn keys(v: &Value) -> Vec<&str> {