Skip to content

Commit

Permalink
support for zipped file extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
pacman82 committed Apr 14, 2020
1 parent c9d01f9 commit 727a4d7
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 6 deletions.
42 changes: 41 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "di-csv2xml"
version = "1.0.14"
version = "1.0.16"
authors = ["Markus Klein <markus.klein@blue-yonder.com>"]
license = "MIT"
publish = false
Expand All @@ -18,6 +18,7 @@ strum = "0.17.1"
strum_macros = "0.17.1"
structopt = "0.3.7"
quick-xml = "0.17.2"
libflate = "0.1.27"

[dev-dependencies]
assert_cli = "0.6.3"
5 changes: 5 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

1.0.16
------

* Support for zipped file extensions

1.0.15
------

Expand Down
Binary file added input.xml.gz
Binary file not shown.
34 changes: 30 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ mod record_type;

use crate::{generate_xml::generate_xml, read_csv::CsvSource, record_type::RecordType};
use indicatif::{ProgressBar, ProgressStyle};
use libflate::gzip;
use quicli::prelude::*;
use std::{fs::File, io};
use std::{fs::File, io, path::Path};
use structopt::StructOpt;
use strum;

Expand Down Expand Up @@ -55,21 +56,46 @@ fn main() -> CliResult {
.template(fmt)
.progress_chars("#>-"),
);
Box::new(progress_bar.wrap_read(file))
let file_with_pbar = progress_bar.wrap_read(file);

if has_gz_extension(&input) {
Box::new(gzip::Decoder::new(file_with_pbar)?)
} else {
Box::new(file_with_pbar)
}
} else {
Box::new(file)
// Repeat if to avoid extra Box.
if has_gz_extension(&input) {
Box::new(gzip::Decoder::new(file)?)
} else {
Box::new(file)
}
}
} else {
// just use stdin
Box::new(io::stdin())
};

let reader = CsvSource::new(input, args.delimiter as u8)?;

let mut out: Box<dyn io::Write> = if let Some(output) = args.output {
Box::new(io::BufWriter::new(File::create(&output)?))
let writer = io::BufWriter::new(File::create(&output)?);

if has_gz_extension(&output) {
Box::new(gzip::Encoder::new(writer)?)
} else {
Box::new(writer)
}
} else {
Box::new(io::stdout())
};
generate_xml(&mut out, reader, &args.category, args.record_type)?;
Ok(())
}

fn has_gz_extension(path: &Path) -> bool {
match path.extension() {
Some(ext) if ext == "gz" => true,
_ => false,
}
}
Binary file added tests/input.csv.gz
Binary file not shown.
10 changes: 10 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ fn simple() {
.unwrap();
}

#[test]
fn input_gz() {
assert_cli::Assert::main_binary()
.with_args(&["Category", "--input", "tests/input.csv.gz"])
.succeeds()
.stdout()
.is(include_str!("output.xml").replace("\r\n", "\n").as_str())
.unwrap();
}

#[test]
fn mask_text() {
assert_cli::Assert::main_binary()
Expand Down

0 comments on commit 727a4d7

Please # to comment.