1use std::io::{self, Seek, Write};
2
3use zip::write::SimpleFileOptions;
4use zip::ZipWriter;
5
6use crate::record::Record;
7
8pub struct Writer<W> {
9 wr: W,
10}
11
12impl<W> Writer<W>
13where
14 W: Write,
15{
16 pub fn new(mut wr: W) -> Result<Self, io::Error> {
17 writeln!(wr, "FileType=text/acmi/tacview")?;
18 writeln!(wr, "FileVersion=2.2")?;
19 Ok(Self { wr })
20 }
21
22 pub fn new_compressed(wr: W) -> Result<Writer<impl Write>, io::Error>
23 where
24 W: Seek,
25 {
26 let mut zip = ZipWriter::new(wr);
27 zip.start_file("track.txt.acmi", SimpleFileOptions::default())?;
28 Writer::new(zip)
29 }
30
31 pub fn write(&mut self, record: impl Into<Record>) -> Result<(), io::Error> {
32 writeln!(self.wr, "{}", record.into())?;
33 Ok(())
34 }
35
36 pub fn into_inner(self) -> W {
37 self.wr
38 }
39}