-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
write.rs
41 lines (29 loc) · 1.17 KB
/
write.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use std::io::{self, prelude::*};
use crate::Error;
use quick_xml::{
events::{BytesDecl, Event},
se::Serializer,
Writer,
};
use serde::Serialize;
use zip::{write::SimpleFileOptions, ZipWriter};
use crate::model::Model;
/// Write a triangle mesh to a 3MF writer
pub fn write<W: Write + io::Seek, M: Into<Model>>(writer: W, model: M) -> Result<(), Error> {
let mut archive = ZipWriter::new(writer);
archive.start_file("[Content_Types].xml", SimpleFileOptions::default())?;
archive.write_all(include_bytes!("content-types.xml"))?;
archive.start_file("_rels/.rels", SimpleFileOptions::default())?;
archive.write_all(include_bytes!("rels.xml"))?;
archive.start_file("3D/model.model", SimpleFileOptions::default())?;
let mut xml = String::new();
let mut ser = Serializer::with_root(&mut xml, Some("model"))?;
ser.indent(' ', 2);
model.into().serialize(ser)?;
let mut xml_writer = Writer::new_with_indent(&mut archive, b' ', 2);
xml_writer.write_event(Event::Decl(BytesDecl::new("1.0", Some("utf-8"), None)))?;
xml_writer.write_indent()?;
xml_writer.into_inner().write_all(xml.as_bytes())?;
archive.finish()?;
Ok(())
}