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
use std::io::{self, Seek, Write};

use zip::{write::FileOptions, ZipWriter};

use crate::record::Record;

pub struct Writer<W> {
    wr: W,
}

impl<W> Writer<W>
where
    W: Write,
{
    pub fn new(mut wr: W) -> Result<Self, io::Error> {
        writeln!(wr, "FileType=text/acmi/tacview")?;
        writeln!(wr, "FileVersion=2.2")?;
        Ok(Self { wr })
    }

    pub fn new_compressed(wr: W) -> Result<Writer<impl Write>, io::Error>
    where
        W: Seek,
    {
        let mut zip = ZipWriter::new(wr);
        zip.start_file("track.txt.acmi", FileOptions::default())?;
        Writer::new(zip)
    }

    pub fn write(&mut self, record: impl Into<Record>) -> Result<(), io::Error> {
        writeln!(self.wr, "{}", record.into())?;
        Ok(())
    }

    pub fn into_inner(self) -> W {
        self.wr
    }
}