epub_builder/
zip_command_or_library.rs1use crate::zip::Zip;
6use crate::Result;
7use crate::ZipCommand;
8use crate::ZipLibrary;
9
10use std::io::Read;
11use std::io::Write;
12use std::path::Path;
13
14pub enum ZipCommandOrLibrary {
18 Command(ZipCommand),
20 Library(ZipLibrary),
22}
23
24impl Zip for ZipCommandOrLibrary {
25 fn write_file<P: AsRef<Path>, R: Read>(&mut self, path: P, content: R) -> Result<()> {
26 match self {
27 ZipCommandOrLibrary::Command(command) => command.write_file(path, content),
28 ZipCommandOrLibrary::Library(library) => library.write_file(path, content),
29 }
30 }
31
32 fn generate<W: Write>(self, to: W) -> Result<()> {
33 match self {
34 ZipCommandOrLibrary::Command(command) => command.generate(to),
35 ZipCommandOrLibrary::Library(library) => library.generate(to),
36 }
37 }
38}
39
40impl ZipCommandOrLibrary {
41 pub fn new(command: &str) -> Result<ZipCommandOrLibrary> {
44 ZipCommand::new()
45 .map(|mut z| {
46 z.command(command);
47 z
48 })
49 .and_then(|z| z.test().map(|_| z))
50 .map(ZipCommandOrLibrary::Command)
51 .or_else(|_| ZipLibrary::new().map(ZipCommandOrLibrary::Library))
52 }
53}