Skip to content

Commit

Permalink
apply, applydp, explode, geocode & pseudo: use replace_colu…
Browse files Browse the repository at this point in the history
…mn_value in util.rs

and remove redundant code in each command module
  • Loading branch information
jqnatividad committed Dec 6, 2023
1 parent 4e074c9 commit cd8b4a5
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 68 deletions.
17 changes: 3 additions & 14 deletions src/cmd/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,9 @@ use crate::{
config::{Config, Delimiter},
regex_oncelock,
select::SelectColumns,
util, CliResult,
util,
util::replace_column_value,
CliResult,
};

#[derive(Clone, EnumString)]
Expand Down Expand Up @@ -483,19 +485,6 @@ enum ApplySubCmd {
CalcConv,
}

#[inline]
fn replace_column_value(
record: &csv::StringRecord,
column_index: usize,
new_value: &str,
) -> csv::StringRecord {
record
.into_iter()
.enumerate()
.map(|(i, v)| if i == column_index { new_value } else { v })
.collect()
}

pub fn run(argv: &[&str]) -> CliResult<()> {
let args: Args = util::get_args(USAGE, argv)?;
let rconfig = Config::new(&args.arg_input)
Expand Down
17 changes: 3 additions & 14 deletions src/cmd/applydp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,9 @@ use crate::{
config::{Config, Delimiter},
regex_oncelock,
select::SelectColumns,
util, CliResult,
util,
util::replace_column_value,
CliResult,
};

#[derive(Clone, EnumString)]
Expand Down Expand Up @@ -316,19 +318,6 @@ static ROUND_PLACES: OnceLock<u32> = OnceLock::new();
// default number of decimal places to round to
const DEFAULT_ROUND_PLACES: u32 = 3;

#[inline]
fn replace_column_value(
record: &csv::StringRecord,
column_index: usize,
new_value: &str,
) -> csv::StringRecord {
record
.into_iter()
.enumerate()
.map(|(i, v)| if i == column_index { new_value } else { v })
.collect()
}

pub fn run(argv: &[&str]) -> CliResult<()> {
let args: Args = util::get_args(USAGE, argv)?;
let rconfig = Config::new(&args.arg_input)
Expand Down
16 changes: 3 additions & 13 deletions src/cmd/explode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ use serde::Deserialize;
use crate::{
config::{Config, Delimiter},
select::SelectColumns,
util, CliResult,
util,
util::replace_column_value,
CliResult,
};
#[derive(Deserialize)]
struct Args {
Expand All @@ -49,18 +51,6 @@ struct Args {
flag_delimiter: Option<Delimiter>,
}

pub fn replace_column_value(
record: &csv::StringRecord,
column_index: usize,
new_value: &str,
) -> csv::StringRecord {
record
.into_iter()
.enumerate()
.map(|(i, v)| if i == column_index { new_value } else { v })
.collect()
}

pub fn run(argv: &[&str]) -> CliResult<()> {
let args: Args = util::get_args(USAGE, argv)?;
let rconfig = Config::new(&args.arg_input)
Expand Down
17 changes: 3 additions & 14 deletions src/cmd/geocode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,9 @@ use crate::{
config::{Config, Delimiter},
regex_oncelock,
select::SelectColumns,
util, CliResult,
util,
util::replace_column_value,
CliResult,
};

#[derive(Deserialize)]
Expand Down Expand Up @@ -594,19 +596,6 @@ impl From<anyhow::Error> for CliError {
}
}

#[inline]
fn replace_column_value(
record: &csv::StringRecord,
column_index: usize,
new_value: &str,
) -> csv::StringRecord {
record
.into_iter()
.enumerate()
.map(|(i, v)| if i == column_index { new_value } else { v })
.collect()
}

pub fn run(argv: &[&str]) -> CliResult<()> {
let mut args: Args = util::get_args(USAGE, argv)?;

Expand Down
16 changes: 3 additions & 13 deletions src/cmd/pseudo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ use serde::Deserialize;
use crate::{
config::{Config, Delimiter},
select::SelectColumns,
util, CliResult,
util,
util::replace_column_value,
CliResult,
};

#[derive(Deserialize)]
Expand All @@ -35,18 +37,6 @@ struct Args {
flag_delimiter: Option<Delimiter>,
}

pub fn replace_column_value(
record: &csv::StringRecord,
column_index: usize,
new_value: &str,
) -> csv::StringRecord {
record
.into_iter()
.enumerate()
.map(|(i, v)| if i == column_index { new_value } else { v })
.collect()
}

type Values = AHashMap<String, u64>;

pub fn run(argv: &[&str]) -> CliResult<()> {
Expand Down

0 comments on commit cd8b4a5

Please sign in to comment.