Skip to content

Commit

Permalink
apply select clippy::pedantic recommendations
Browse files Browse the repository at this point in the history
  • Loading branch information
jqnatividad committed May 20, 2022
1 parent d26c62c commit 0862a27
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 55 deletions.
1 change: 0 additions & 1 deletion src/cmd/jsonl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ fn json_line_to_csv_record(value: &Value, headers: &[Vec<String>]) -> csv::Strin

if let Some(value) = value {
record.push_field(&match value {
Value::Null => String::new(),
Value::Bool(v) => {
if v {
String::from("true")
Expand Down
34 changes: 16 additions & 18 deletions src/cmd/sample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,30 +63,28 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
let mut sample_size = args.arg_sample_size;

let mut wtr = Config::new(&args.flag_output).writer()?;
let sampled = match rconfig.indexed()? {
Some(mut idx) => {
if sample_size < 1.0 {
sample_size *= idx.count() as f64;
}
if args.flag_seed.is_none() && do_random_access(sample_size as u64, idx.count()) {
rconfig.write_headers(&mut *idx, &mut wtr)?;
sample_random_access(&mut idx, sample_size as u64)?
} else {
let mut rdr = rconfig.reader()?;
rconfig.write_headers(&mut rdr, &mut wtr)?;
sample_reservoir(&mut rdr, sample_size as u64, args.flag_seed)?
}
let sampled = if let Some(mut idx) = rconfig.indexed()? {
if sample_size < 1.0 {
sample_size *= idx.count() as f64;
}
_ => {
debug!("no index");
if sample_size < 1.0 {
return fail!("Percentage sampling requires an index.");
}
if args.flag_seed.is_none() && do_random_access(sample_size as u64, idx.count()) {
rconfig.write_headers(&mut *idx, &mut wtr)?;
sample_random_access(&mut idx, sample_size as u64)?
} else {
let mut rdr = rconfig.reader()?;
rconfig.write_headers(&mut rdr, &mut wtr)?;
sample_reservoir(&mut rdr, sample_size as u64, args.flag_seed)?
}
} else {
debug!("no index");
if sample_size < 1.0 {
return fail!("Percentage sampling requires an index.");
}
let mut rdr = rconfig.reader()?;
rconfig.write_headers(&mut rdr, &mut wtr)?;
sample_reservoir(&mut rdr, sample_size as u64, args.flag_seed)?
};

for row in sampled {
wtr.write_byte_record(&row)?;
}
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,15 +434,15 @@ fn get_unique_values(
_ => freq_args.sequential_ftables(),
}?;

let unique_values_map = construct_map_of_unique_values(&headers, ftables)?;
let unique_values_map = construct_map_of_unique_values(&headers, &ftables)?;

Ok(unique_values_map)
}

/// construct map of unique values keyed by header
fn construct_map_of_unique_values(
freq_csv_fields: &ByteRecord,
frequency_tables: Vec<Frequencies<Vec<u8>>>,
frequency_tables: &[Frequencies<Vec<u8>>],
) -> CliResult<AHashMap<String, Vec<String>>> {
let mut unique_values_map: AHashMap<String, Vec<String>> = AHashMap::new();

Expand Down
7 changes: 4 additions & 3 deletions src/cmd/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
}
let mut record = csv::ByteRecord::new();
let mut flag_rowi: u64 = 1;
let mut _matched_rows = String::from("");
#[allow(unused_assignments)]
let mut matched_rows = String::with_capacity(20); // to save on allocs
while rdr.read_byte_record(&mut record)? {
let mut m = sel.select(&record).any(|f| pattern.is_match(f));
if args.flag_invert_match {
Expand All @@ -97,8 +98,8 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
if args.flag_flag.is_some() {
flag_rowi += 1;
record.push_field(if m {
_matched_rows = flag_rowi.to_string();
_matched_rows.as_bytes()
matched_rows = flag_rowi.to_string();
matched_rows.as_bytes()
} else {
b"0"
});
Expand Down
15 changes: 9 additions & 6 deletions src/cmd/searchset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,11 @@ pub fn run(argv: &[&str]) -> CliResult<()> {

let mut record = csv::ByteRecord::new();
let mut flag_rowi: u64 = 1;
let mut _matched_rows = String::new();
let mut _match_list_with_row = String::new();
// to save allocs
#[allow(unused_assignments)]
let mut matched_rows = String::with_capacity(20);
#[allow(unused_assignments)]
let mut match_list_with_row = String::with_capacity(20);
while rdr.read_byte_record(&mut record)? {
let mut m = sel.select(&record).any(|f| {
let matched = pattern.is_match(f);
Expand All @@ -136,12 +139,12 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
if do_match_list {
flag_rowi += 1;
record.push_field(if m {
_matched_rows = flag_rowi.to_string();
matched_rows = flag_rowi.to_string();
if args.flag_invert_match {
_matched_rows.as_bytes()
matched_rows.as_bytes()
} else {
_match_list_with_row = format!("{_matched_rows};{match_list}");
_match_list_with_row.as_bytes()
match_list_with_row = format!("{matched_rows};{match_list}");
match_list_with_row.as_bytes()
}
} else {
b"0"
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
fs::create_dir_all(&args.arg_outdir)?;

match args.rconfig().indexed()? {
Some(idx) => args.parallel_split(idx),
Some(idx) => args.parallel_split(&idx),
None => args.sequential_split(),
}
}
Expand All @@ -93,7 +93,7 @@ impl Args {
Ok(())
}

fn parallel_split(&self, idx: Indexed<fs::File, fs::File>) -> CliResult<()> {
fn parallel_split(&self, idx: &Indexed<fs::File, fs::File>) -> CliResult<()> {
let nchunks = util::num_of_chunks(idx.count() as usize, self.flag_size);
let pool = ThreadPool::new(util::njobs(self.flag_jobs));
for i in 0..nchunks {
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,9 @@ impl Stats {
pieces.push(buffer.format(1.5f64.mul_add(iqr, q3)).to_owned());
// calculate skewnewss using Pearson's median skewness
// https://en.wikipedia.org/wiki/Skewness#Pearson's_second_skewness_coefficient_(median_skewness)
let _mean = self.online.unwrap().mean();
let _stddev = self.online.unwrap().stddev();
pieces.push(buffer.format((3.0 * (_mean - q2)) / _stddev).to_owned());
let mean = self.online.unwrap().mean();
let stddev = self.online.unwrap().stddev();
pieces.push(buffer.format((3.0 * (mean - q2)) / stddev).to_owned());
}
}
match self.modes.as_mut() {
Expand Down
15 changes: 6 additions & 9 deletions src/cmd/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,8 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
}]
});
return fail!(header_error.to_string());
} else {
return fail!(format!("Cannot read header ({e})."));
}
return fail!(format!("Cannot read header ({e})."));
}
}
}
Expand All @@ -147,11 +146,10 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
}]
});
return fail!(validation_error.to_string());
} else {
return fail!(format!(
r#"Validation error: {e}. Try "qsv fixlengths" or "qsv fmt" to fix it."#
));
}
return fail!(format!(
r#"Validation error: {e}. Try "qsv fixlengths" or "qsv fmt" to fix it."#
));
}
record_count += 1;
}
Expand Down Expand Up @@ -516,12 +514,11 @@ fn to_json_instance(headers: &ByteRecord, record: &ByteRecord, schema: &Value) -

// grab the first entry that's not a "null", since it just means value is optional
for val in vec {
if *val != null_type {
return_val = val.as_str().expect("type info should be a JSON string");
} else {
if *val == null_type {
// keep looking
continue;
}
return_val = val.as_str().expect("type info should be a JSON string");
}

return_val
Expand Down
23 changes: 12 additions & 11 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,18 @@ pub fn show_env_vars() {
}

pub fn count_rows(conf: &Config) -> u64 {
if let Some(idx) = conf.indexed().unwrap() {
idx.count()
} else {
let mut rdr = conf.reader().unwrap();
let mut count = 0u64;
let mut record = csv::ByteRecord::new();
while rdr.read_byte_record(&mut record).unwrap() {
count += 1;
}
count
}
conf.indexed().unwrap().map_or_else(
|| {
let mut rdr = conf.reader().unwrap();
let mut count = 0u64;
let mut record = csv::ByteRecord::new();
while rdr.read_byte_record(&mut record).unwrap() {
count += 1;
}
count
},
|idx| idx.count(),
)
}

#[cfg(any(feature = "full", feature = "lite"))]
Expand Down

0 comments on commit 0862a27

Please sign in to comment.