Skip to content

Commit 3e38df8

Browse files
Markus Kleinpacman82
Markus Klein
authored andcommitted
finish test
1 parent 80bbbd3 commit 3e38df8

File tree

6 files changed

+58
-46
lines changed

6 files changed

+58
-46
lines changed

Cargo.lock

+22-25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ strum = "0.18.0"
1818
strum_macros = "0.18.0"
1919
structopt = "0.3.12"
2020
quick-xml = "0.18.1"
21-
libflate = "0.1.27"
21+
flate2 = "1.0.14"
2222

2323
[dev-dependencies]
2424
assert_cli = "0.6.3"

Changelog.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Changelog
22
=========
33

4+
1.0.17
5+
------
6+
7+
* Update dependencies
8+
* Use flate2 for compression / decompression
9+
* Speedup for compressing / decompressing files, through inserting io buffers.
10+
411
1.0.16
512
------
613

src/main.rs

+16-8
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ mod read_csv;
33
mod record_type;
44

55
use crate::{generate_xml::generate_xml, read_csv::CsvSource, record_type::RecordType};
6+
use flate2::{bufread::GzDecoder, GzBuilder};
67
use indicatif::{ProgressBar, ProgressStyle};
7-
use libflate::gzip;
88
use quicli::prelude::*;
99
use std::{fs::File, io, path::Path};
1010
use structopt::StructOpt;
@@ -19,17 +19,17 @@ struct Cli {
1919
#[structopt()]
2020
category: String,
2121
/// Path to input file. If ommited STDIN is used for input.
22-
#[structopt(long = "input", short = "i", parse(from_os_str))]
22+
#[structopt(long, short = "i", parse(from_os_str))]
2323
input: Option<std::path::PathBuf>,
2424
/// Path to output file. If ommited output is written to STDOUT.
25-
#[structopt(long = "output", short = "o", parse(from_os_str))]
25+
#[structopt(long, short = "o", parse(from_os_str))]
2626
output: Option<std::path::PathBuf>,
2727
/// Record type of generated XML. Should be either Record, DeleteRecord, DeleteAllRecords.
2828
#[structopt(long = "record-type", short = "r", default_value = "Record")]
2929
record_type: RecordType,
3030
/// Character used as delimiter between csv columns. While this tool assumes utf8 encoding,
3131
/// only ASCII delimiters are supported.
32-
#[structopt(long = "delimiter", short = "d", default_value = ",")]
32+
#[structopt(long, short = "d", default_value = ",")]
3333
delimiter: char,
3434
}
3535

@@ -46,6 +46,12 @@ fn main() -> CliResult {
4646
let input: Box<dyn io::Read> = if let Some(input) = args.input {
4747
// Path argument specified. Open file and initialize progress bar.
4848
let file = File::open(&input)?;
49+
// Only show Progress bar, if both input and output are files.
50+
//
51+
// * We need the input to so we have the file metadata and therefore file length, to know
52+
// the amount of data we are going to proccess. Otherwise we can't set the length of the
53+
// progress bar.
54+
// * We don't want the Progress bar to interfere with the output, if writing to stdout.
4955
// Progress bar interferes with formatting if stdout and stderr both go to console
5056
if args.output.is_some() {
5157
let len = file.metadata()?.len();
@@ -59,20 +65,22 @@ fn main() -> CliResult {
5965
let file_with_pbar = progress_bar.wrap_read(file);
6066

6167
if has_gz_extension(&input) {
62-
Box::new(gzip::Decoder::new(file_with_pbar)?)
68+
Box::new(GzDecoder::new(io::BufReader::new(file_with_pbar)))
6369
} else {
6470
Box::new(file_with_pbar)
6571
}
6672
} else {
73+
// Input file, but writing to stdout
74+
6775
// Repeat if to avoid extra Box.
6876
if has_gz_extension(&input) {
69-
Box::new(gzip::Decoder::new(file)?)
77+
Box::new(GzDecoder::new(io::BufReader::new(file)))
7078
} else {
7179
Box::new(file)
7280
}
7381
}
7482
} else {
75-
// just use stdin
83+
// Input path not set => Just use stdin
7684
Box::new(io::stdin())
7785
};
7886

@@ -82,7 +90,7 @@ fn main() -> CliResult {
8290
let writer = io::BufWriter::new(File::create(&output)?);
8391

8492
if has_gz_extension(&output) {
85-
Box::new(gzip::Encoder::new(writer)?)
93+
Box::new(GzBuilder::new().write(writer, Default::default()))
8694
} else {
8795
Box::new(writer)
8896
}

tests/lib.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,20 @@ fn write_gz() {
114114
.succeeds()
115115
.unwrap();
116116

117-
// // Compare output file with expectation
118-
// let mut expected = Vec::new();
119-
// File::open("tests/output.xml.gz")
120-
// .unwrap()
121-
// .read_to_end(&mut expected)
122-
// .unwrap();
117+
// Compare output file with expectation
118+
let mut expected = Vec::new();
119+
File::open("tests/output.xml.gz")
120+
.unwrap()
121+
.read_to_end(&mut expected)
122+
.unwrap();
123123

124-
// let mut actual = Vec::new();
125-
// File::open(out_path)
126-
// .unwrap()
127-
// .read_to_end(&mut actual)
128-
// .unwrap();
124+
let mut actual = Vec::new();
125+
File::open(out_path)
126+
.unwrap()
127+
.read_to_end(&mut actual)
128+
.unwrap();
129129

130-
// assert_eq!(expected, actual);
130+
assert_eq!(expected, actual);
131131

132132
// By closing the `out_dir` explicitly, we can check that it has been deleted successfully. If
133133
// we don't close it explicitly, the file will still be deleted when `file` goes out of scope,

tests/output.xml.gz

123 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)