this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor: simplify gzip compression and extraction

+31 -68
+31 -68
src/gzip.rs
··· 38 38 in_path.file_stem().unwrap().to_str().unwrap().to_string() 39 39 } 40 40 41 + /// Compress an input file or pipe to a gzip archive 41 42 fn compress(&self, input: CmprssInput, output: CmprssOutput) -> Result<(), io::Error> { 42 43 if let CmprssOutput::Path(out_path) = &output { 43 44 if out_path.is_dir() { ··· 53 54 } 54 55 } 55 56 } 56 - match (input, output) { 57 - (CmprssInput::Path(in_path), CmprssOutput::Path(out_path)) => { 58 - let mut encoder = GzEncoder::new( 59 - File::create(out_path)?, 60 - Compression::new(self.compression_level), 61 - ); 62 - for x in in_path { 63 - std::io::copy(&mut File::open(x)?, &mut encoder)?; 57 + let mut input_stream = match input { 58 + CmprssInput::Path(paths) => { 59 + if paths.len() > 1 { 60 + return cmprss_error("only 1 file can be compressed at a time"); 64 61 } 65 - encoder.finish()?; 66 - Ok(()) 67 - } 68 - (CmprssInput::Path(in_path), CmprssOutput::Pipe(out_pipe)) => { 69 - let mut encoder = 70 - GzEncoder::new(out_pipe, Compression::new(self.compression_level)); 71 - for x in in_path { 72 - std::io::copy(&mut File::open(x)?, &mut encoder)?; 73 - } 74 - encoder.finish()?; 75 - Ok(()) 76 - } 77 - (CmprssInput::Pipe(in_pipe), CmprssOutput::Path(out_path)) => { 78 - self.compress_internal(in_pipe, File::create(out_path)?) 62 + Box::new(File::open(paths[0].as_path())?) 79 63 } 80 - (CmprssInput::Pipe(in_pipe), CmprssOutput::Pipe(out_pipe)) => { 81 - self.compress_internal(in_pipe, out_pipe) 82 - } 83 - } 64 + CmprssInput::Pipe(pipe) => Box::new(pipe) as Box<dyn Read + Send>, 65 + }; 66 + let output_stream = match output { 67 + CmprssOutput::Path(path) => Box::new(File::create(path)?), 68 + CmprssOutput::Pipe(pipe) => Box::new(pipe) as Box<dyn Write + Send>, 69 + }; 70 + 71 + let mut encoder = GzEncoder::new(output_stream, Compression::new(self.compression_level)); 72 + std::io::copy(&mut input_stream, &mut encoder)?; 73 + encoder.finish()?; 74 + Ok(()) 84 75 } 85 76 77 + /// Extract a gzip archive 86 78 fn extract(&self, input: CmprssInput, output: CmprssOutput) -> Result<(), io::Error> { 87 - match (input, output) { 88 - (CmprssInput::Path(in_path), CmprssOutput::Path(out_path)) => { 89 - if in_path.len() > 1 { 90 - return cmprss_error("only 1 archive can be extracted at a time"); 79 + let input_stream = match input { 80 + CmprssInput::Path(paths) => { 81 + if paths.len() > 1 { 82 + return cmprss_error("only 1 file can be extracted at a time"); 91 83 } 92 - self.extract_internal(File::open(in_path[0].as_path())?, File::create(out_path)?) 84 + Box::new(File::open(paths[0].as_path())?) 93 85 } 94 - (CmprssInput::Path(in_path), CmprssOutput::Pipe(out_pipe)) => { 95 - if in_path.len() > 1 { 96 - return cmprss_error("only 1 archive can be extracted at a time"); 97 - } 98 - self.extract_internal(File::open(in_path[0].as_path())?, out_pipe) 99 - } 100 - (CmprssInput::Pipe(in_pipe), CmprssOutput::Path(out_path)) => { 101 - self.extract_internal(in_pipe, File::create(out_path)?) 102 - } 103 - (CmprssInput::Pipe(in_pipe), CmprssOutput::Pipe(out_pipe)) => { 104 - self.extract_internal(in_pipe, out_pipe) 105 - } 106 - } 107 - } 108 - } 86 + CmprssInput::Pipe(pipe) => Box::new(pipe) as Box<dyn Read + Send>, 87 + }; 88 + let mut output_stream = match output { 89 + CmprssOutput::Path(path) => Box::new(File::create(path)?), 90 + CmprssOutput::Pipe(pipe) => Box::new(pipe) as Box<dyn Write + Send>, 91 + }; 109 92 110 - impl Gzip { 111 - /// Compress an input stream into a gzip archive. 112 - fn compress_internal<I: Read, O: Write>( 113 - &self, 114 - mut input: I, 115 - output: O, 116 - ) -> Result<(), io::Error> { 117 - let mut encoder = GzEncoder::new(output, Compression::new(self.compression_level)); 118 - 119 - std::io::copy(&mut input, &mut encoder)?; 120 - encoder.finish()?; 121 - Ok(()) 122 - } 123 - 124 - /// Extract the gzip compressed data 125 - fn extract_internal<I: Read, O: Write>( 126 - &self, 127 - input: I, 128 - mut output: O, 129 - ) -> Result<(), io::Error> { 130 - let mut decoder = GzDecoder::new(input); 131 - std::io::copy(&mut decoder, &mut output)?; 93 + let mut decoder = GzDecoder::new(input_stream); 94 + std::io::copy(&mut decoder, &mut output_stream)?; 132 95 Ok(()) 133 96 } 134 97 }