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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use crate::errors::Result;
use crate::zip::Zip;
use crate::ZipCommand;
use crate::ZipLibrary;
use std::io::Read;
use std::io::Write;
use std::path::Path;
pub enum ZipCommandOrLibrary {
Command(ZipCommand),
Library(ZipLibrary),
}
impl Zip for ZipCommandOrLibrary {
fn write_file<P: AsRef<Path>, R: Read>(&mut self, path: P, content: R) -> Result<()> {
match self {
ZipCommandOrLibrary::Command(ref mut command) => command.write_file(path, content),
ZipCommandOrLibrary::Library(ref mut library) => library.write_file(path, content),
}
}
fn generate<W: Write>(&mut self, to: W) -> Result<()> {
match self {
ZipCommandOrLibrary::Command(ref mut command) => command.generate(to),
ZipCommandOrLibrary::Library(ref mut library) => library.generate(to),
}
}
}
impl ZipCommandOrLibrary {
pub fn new(command: &str) -> Result<ZipCommandOrLibrary> {
ZipCommand::new()
.map(|mut z| {
z.command(command);
z
})
.and_then(|z| z.test().map(|_| z))
.map(|z| ZipCommandOrLibrary::Command(z))
.or_else(|_| ZipLibrary::new().map(|l| ZipCommandOrLibrary::Library(l)))
}
}