Skip to content

Commit

Permalink
Add finalize variant that allows XML customization for E57 extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
cry-inc committed Mar 27, 2024
1 parent ac16312 commit 83581af
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/e57_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,28 @@ impl<T: Write + Read + Seek> E57Writer<T> {
/// This will generate and write the XML metadata to finalize and complete the E57 file.
/// Without calling this method before dropping the E57 file will be incomplete and invalid!
pub fn finalize(&mut self) -> Result<()> {
self.finalize_customized_xml(Ok)
}

/// Same as `finalize()` but with additional XML transformation step.
///
/// Allows customizing the XML data before its written into the E57 file.
/// This is required for adding E57 extension data to the XML.
/// The transformer receives an XML string and must return an XML string.
/// The client is responsible for parsing, modifying and serializing th XML again in a non-destructive way.
/// The E57 library will not validate the XML string before writing it into the E57 file!
/// If the transformer fails, the finalization is aborted and any error is forwarded.
pub fn finalize_customized_xml(
&mut self,
transformer: impl Fn(String) -> Result<String>,
) -> Result<()> {
let xml = serialize_root(
&self.root,
&self.pointclouds,
&self.images,
&self.extensions,
)?;
let xml = transformer(xml)?;
let xml_bytes = xml.as_bytes();
let xml_length = xml_bytes.len();
let xml_offset = self.writer.physical_position()?;
Expand Down
29 changes: 29 additions & 0 deletions tests/writer_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -916,3 +916,32 @@ fn extensions_write_read_blobs() {

remove_file(path).unwrap();
}

#[test]
fn custom_xml_test() {
let path = Path::new("custom_xml_test.e57");
let inserted_xml = "<myext:mytag type=\"Structure\"></myext:mytag>";
let extension = Extension::new("myext", "https://mycompany.com/myext");

{
let mut e57_writer = E57Writer::from_file(path, "guid_file").unwrap();
e57_writer.register_extesion(extension).unwrap();
e57_writer
.finalize_customized_xml(|xml| {
assert!(!xml.contains(inserted_xml));
let from = "</e57Root>";
let to = format!("{}\n</e57Root>", inserted_xml);
Ok(xml.replace(from, &to))
})
.unwrap();
}

{
let e57 = E57Reader::from_file(path).unwrap();
assert_eq!(e57.guid(), "guid_file");
let xml = e57.xml();
assert!(xml.contains(inserted_xml));
}

remove_file(path).unwrap();
}

0 comments on commit 83581af

Please sign in to comment.