Skip to content

Commit

Permalink
Feat/issue 25 handle gzipped files (#26)
Browse files Browse the repository at this point in the history
* handle gzipped files

* tests

* refine signature to be more informative

* cargo fmt

* cargo clippy

* add test data

* support GZ files

* remove test configuration from files in tests/ as they don't need it

* Delete flamegraph.svg

* cargo fmt

* cargo clippy
  • Loading branch information
hderms authored May 4, 2021
1 parent 29c72b2 commit 451c7aa
Show file tree
Hide file tree
Showing 7 changed files with 254 additions and 37 deletions.
157 changes: 150 additions & 7 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ simple_logger="1.11.0"
clap = "3.0.0-beta.2"
md5 = "0.7.0"
statistical = "1.0.0"
tree_magic = "0.2.3"
flate2 = "1.0.20"

[dev-dependencies]
assert_cmd="0.10"
Expand Down
13 changes: 12 additions & 1 deletion src/csv/csv_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use csv::{StringRecord, Trim};
use log::debug;
use std::error::Error;
use std::fmt::{Display, Formatter};
use std::fs::File;

#[derive(Eq, PartialEq, Debug)]
pub enum CsvWrapper {
Expand Down Expand Up @@ -44,13 +45,23 @@ impl CsvData {
trim: bool,
) -> Result<CsvData, Box<dyn Error>> {
debug!("Trying to load CSV from filename {}", filename);
let file = File::open(filename)?;
CsvData::from_reader(file, filename, delimiter, trim)
}

pub fn from_reader<R: std::io::Read>(
reader: R,
filename: &str,
delimiter: char,
trim: bool,
) -> Result<CsvData, Box<dyn Error>> {
let mut records = Vec::with_capacity(10000);
let trim = if trim { Trim::All } else { Trim::None };
let mut rdr = csv::ReaderBuilder::new()
.buffer_capacity(16 * (1 << 10))
.delimiter(delimiter as u8)
.trim(trim)
.from_path(filename)?;
.from_reader(reader);

for result in rdr.records() {
let record = result?;
Expand Down
12 changes: 12 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::qsv::{execute_analysis, execute_query, write_to_stdout};
use clap::{AppSettings, Clap};
use simple_logger::SimpleLogger;
use std::error::Error;
use std::path::Path;

#[derive(Clap)]
#[clap(
Expand All @@ -23,6 +24,7 @@ struct Opts {
enum SubCommand {
Query(Query),
Analyze(Analyze),
FileType(FileType),
}

#[derive(Clap)]
Expand All @@ -44,6 +46,10 @@ struct Analyze {
#[clap(long)]
trim: bool,
}
#[derive(Clap)]
struct FileType {
filename: String,
}
fn main() -> Result<(), Box<dyn Error>> {
SimpleLogger::from_env().init()?;
let opts: Opts = Opts::parse();
Expand Down Expand Up @@ -71,6 +77,12 @@ fn main() -> Result<(), Box<dyn Error>> {
let results = execute_analysis(subcmd.query.as_str(), &options)?;
println!("{}", results);
}

SubCommand::FileType(ft) => {
let path = Path::new(ft.filename.as_str());
let t = tree_magic::from_filepath(path);
println!("{}", t);
}
}
Ok(())
}
Loading

0 comments on commit 451c7aa

Please sign in to comment.