aboutsummaryrefslogtreecommitdiff
path: root/src/archive.rs
blob: e635007ae446e215c2836681bd8c48224c6c601c (plain)
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
use crate::driver::{ArchiveRead, ArchiveWrite, Driver};
use crate::ArchiveResult;
use std::fs::File;
use std::io::{Read, Write};
use std::path::Path;

pub struct Archive<D: Driver> {
    pub(crate) driver: D,
}

impl<D: ArchiveRead> Archive<D>
where
    D::IO: std::io::Read,
{
    pub fn read(io: D::IO) -> ArchiveResult<Self, D::Error> {
        Ok(Self {
            driver: D::read(io)?,
        })
    }

    pub fn read_from_file(path: impl AsRef<Path>) -> ArchiveResult<Self, D::Error>
    where
        D: ArchiveRead<IO = File>,
    {
        Self::read(File::open(path)?)
    }

    pub fn files(&self) -> Vec<&D::File> {
        self.driver.files()
    }

    pub fn get_file(&self, name: &str) -> Option<&D::File> {
        self.driver.get_file(name)
    }
}

impl<D: ArchiveWrite> Archive<D> where D::IO: Read + Write {}