aboutsummaryrefslogtreecommitdiff
path: root/src/archive.rs
blob: a1fe34476cac1f757c74a8f06963aa0d13e6fe24 (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
38
use crate::result::ArchiveResult;
use std::{fs::File, path::Path};

use crate::io::{ArchiveRead, ArchiveWrite};

pub struct Archive<IO> {
    io: IO,
}

impl<IO: ArchiveRead> Archive<IO> {
    pub fn open(path: impl AsRef<Path>) -> ArchiveResult<Self>
    where
        IO: ArchiveRead<Reader = File>,
    {
        Self::reader(File::open(path)?)
    }

    pub fn reader(reader: IO::Reader) -> ArchiveResult<Self> {
        Ok(Self {
            io: IO::new(reader)?,
        })
    }
}

impl<IO: ArchiveWrite> Archive<IO> {
    pub fn create(path: impl AsRef<Path>) -> ArchiveResult<Self>
    where
        IO: ArchiveWrite<Writer = File>,
    {
        Self::writer(File::create(path)?)
    }

    pub fn writer(writer: IO::Writer) -> ArchiveResult<Self> {
        Ok(Self {
            io: IO::new(writer)?,
        })
    }
}