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
use crate::header::Header;
use crate::toc::Toc;
use crate::*;
use failure::Error;
use libflate::zlib::Decoder;
use std::fmt;
use std::io::SeekFrom;
use std::io::{Read, Seek};

#[derive(Debug, Clone)]
pub struct Archive {
    header: Header,
    toc: Toc,
}

impl Archive {
    pub fn from_read<T: Read>(reader: &mut T) -> Result<Archive, Error> {
        let header = Header::from_read(reader)?;

        // TODO: verify that only header.toc_length_compressed bytes were read.
        let toc = Toc::from_read(reader, header.toc_length_uncompressed as usize)?;

        Ok(Archive { header, toc })
    }

    pub fn header(&self) -> &Header {
        &self.header
    }

    pub fn toc(&self) -> &Toc {
        &self.toc
    }
}

impl std::fmt::Display for Archive {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}\n{}", self.header, self.toc)
    }
}