this repo has no description
1use super::stream::{copy_stream, guard_file_output, open_input, prepare_output};
2use crate::progress::ProgressArgs;
3use crate::utils::{CmprssInput, CmprssOutput, CommonArgs, Compressor, Result};
4use clap::Args;
5use snap::read::FrameDecoder;
6use snap::write::FrameEncoder;
7use std::io::Write;
8
9#[derive(Args, Debug)]
10pub struct SnappyArgs {
11 #[clap(flatten)]
12 pub common_args: CommonArgs,
13
14 #[clap(flatten)]
15 pub progress_args: ProgressArgs,
16}
17
18#[derive(Default, Clone)]
19pub struct Snappy {
20 pub progress_args: ProgressArgs,
21}
22
23impl Snappy {
24 pub fn new(args: &SnappyArgs) -> Snappy {
25 Snappy {
26 progress_args: args.progress_args,
27 }
28 }
29}
30
31impl Compressor for Snappy {
32 /// The standard extension for framed snappy files, per Google's reference
33 /// implementation.
34 fn extension(&self) -> &str {
35 "sz"
36 }
37
38 /// Full name for snappy.
39 fn name(&self) -> &str {
40 "snappy"
41 }
42
43 /// Compress an input file or pipe to a snappy frame-format archive
44 fn compress(&self, input: CmprssInput, output: CmprssOutput) -> Result {
45 guard_file_output(&output, "Snappy")?;
46 let (input_stream, file_size, pipeline_inner) = open_input(input, "Snappy")?;
47 let (writer, target) = prepare_output(output)?;
48 let mut encoder = FrameEncoder::new(writer);
49 copy_stream(
50 input_stream,
51 &mut encoder,
52 file_size,
53 pipeline_inner,
54 &self.progress_args,
55 target,
56 )?;
57 encoder.flush()?;
58 Ok(())
59 }
60
61 /// Extract a snappy frame-format archive to an output file or pipe
62 fn extract(&self, input: CmprssInput, output: CmprssOutput) -> Result {
63 guard_file_output(&output, "Snappy")?;
64 let (input_stream, file_size, pipeline_inner) = open_input(input, "Snappy")?;
65 let decoder = FrameDecoder::new(input_stream);
66 let (writer, target) = prepare_output(output)?;
67 copy_stream(
68 decoder,
69 writer,
70 file_size,
71 pipeline_inner,
72 &self.progress_args,
73 target,
74 )?;
75 Ok(())
76 }
77}
78
79#[cfg(test)]
80mod tests {
81 use super::*;
82 use crate::test_utils::*;
83
84 /// Test the basic interface of the Snappy compressor
85 #[test]
86 fn test_snappy_interface() {
87 let compressor = Snappy::default();
88 test_compressor_interface(&compressor, "snappy", Some("sz"));
89 }
90
91 /// Test that the round-trip produces identical data
92 #[test]
93 fn test_snappy_default_compression() -> Result {
94 let compressor = Snappy::default();
95 test_compression(&compressor)
96 }
97}