Skip to content

Commit

Permalink
use fail macros consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
jqnatividad committed Oct 13, 2022
1 parent f6959f0 commit e3d516d
Show file tree
Hide file tree
Showing 23 changed files with 89 additions and 89 deletions.
4 changes: 2 additions & 2 deletions src/cmd/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
if args.cmd_operations {
for op in &operations {
if !OPERATIONS.contains(op) {
return fail_format!("Unknown '{op}' operation");
return fail_clierror!("Unknown '{op}' operation");
}
#[allow(clippy::useless_asref)]
match op.as_ref() {
Expand Down Expand Up @@ -561,7 +561,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
}
}
Err(e) => {
return fail_format!("Error reading file: {e}");
return fail_clierror!("Error reading file: {e}");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/dedup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
record.clone_from(&next_record);
}
cmp::Ordering::Greater => {
return fail_format!(
return fail_clierror!(
"Aborting! Input not sorted! {record:?} is greater than {next_record:?}"
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/enumerate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
} else if copy_operation {
let current_header = match String::from_utf8(headers[copy_index].to_vec()) {
Ok(s) => s,
Err(e) => return fail_format!("Could not parse cell as utf-8! - {e}"),
Err(e) => return fail_clierror!("Could not parse cell as utf-8! - {e}"),
};
headers.push_field(format!("{current_header}_copy").as_bytes());
} else {
Expand Down
10 changes: 5 additions & 5 deletions src/cmd/excel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {

let mut workbook = match open_workbook_auto(path) {
Ok(workbook) => workbook,
Err(e) => return fail_format!("Cannot open workbook: {e}."),
Err(e) => return fail_clierror!("Cannot open workbook: {e}."),
};

let sheet_names = workbook.sheet_names();
Expand Down Expand Up @@ -125,7 +125,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
if let Ok(result) = result {
result
} else {
return fail_format!("Cannot retrieve range from {}", sheet_name);
return fail_clierror!("Cannot retrieve range from {}", sheet_name);
}
}
None => Range::empty(),
Expand Down Expand Up @@ -172,7 +172,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
if sheet_index as usize <= sheet_names.len() {
sheet_names[sheet_index as usize].to_string()
} else {
return fail_format!(
return fail_clierror!(
"sheet index {sheet_index} is greater than number of sheets {}",
sheet_names.len()
);
Expand Down Expand Up @@ -206,15 +206,15 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
sheet = sheet_names[idx].clone();
idx
} else {
return fail_format!("Cannot get sheet index for {sheet}");
return fail_clierror!("Cannot get sheet index for {sheet}");
};

let range = match workbook.worksheet_range_at(sheet_index) {
Some(result) => {
if let Ok(result) = result {
result
} else {
return fail_format!("Cannot retrieve range from {sheet}");
return fail_clierror!("Cannot retrieve range from {sheet}");
}
}
None => Range::empty(),
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/exclude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl Args {
let select1 = rconf1.selection(headers1)?;
let select2 = rconf2.selection(headers2)?;
if select1.len() != select2.len() {
return fail_format!(
return fail_clierror!(
"Column selections must have the same number of columns, \
but found column selections with {} and {} columns.",
select1.len(),
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/extsort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ pub fn run(argv: &[&str]) -> CliResult<()> {

let mut input_reader = io::BufReader::new(match fs::File::open(&args.arg_input) {
Ok(f) => f,
Err(e) => return fail_format!("Cannot read input file {e}"),
Err(e) => return fail_clierror!("Cannot read input file {e}"),
});

let mut output_writer = io::BufWriter::new(match fs::File::create(&args.arg_output) {
Ok(f) => f,
Err(e) => return fail_format!("Cannot create output file: {e}"),
Err(e) => return fail_clierror!("Cannot create output file: {e}"),
});

let sorter: ExternalSorter<String, io::Error, MemoryLimitedBufferBuilder> =
Expand All @@ -83,7 +83,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
{
Ok(sorter) => sorter,
Err(e) => {
return fail_format!("cannot create external sorter: {e}");
return fail_clierror!("cannot create external sorter: {e}");
}
};

Expand Down
8 changes: 4 additions & 4 deletions src/cmd/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {

let mut redis_conn;
match redis_client.get_connection() {
Err(e) => return fail_format!(r#"Cannot connect to Redis using "{conn_str}": {e:?}"#),
Err(e) => return fail_clierror!(r#"Cannot connect to Redis using "{conn_str}": {e:?}"#),
Ok(x) => redis_conn = x,
}

Expand Down Expand Up @@ -392,7 +392,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
let vals: Vec<&str> = header.split(':').collect();

if vals.len() != 2 {
return fail_format!("{vals:?} is not a valid key-value pair. Expecting a key and a value separated by a colon.");
return fail_clierror!("{vals:?} is not a valid key-value pair. Expecting a key and a value separated by a colon.");
}

// allocate new String for header key to put into map
Expand Down Expand Up @@ -622,7 +622,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
}
final_response = match serde_json::from_str(&intermediate_redis_value) {
Ok(r) => r,
Err(e)=> return fail_format!("Cannot deserialize Redis cache value. Try flushing the Redis cache with --flushdb - {e}"),
Err(e)=> return fail_clierror!("Cannot deserialize Redis cache value. Try flushing the Redis cache with --flushdb - {e}"),
};
if !args.flag_cache_error && final_response.status_code != 200 {
let key = format!(
Expand Down Expand Up @@ -1144,7 +1144,7 @@ use serde_json::{Deserializer, Value};
pub fn apply_jql(json: &str, groups: &[jql::Group]) -> Result<String, String> {
// check if api returned valid JSON before applying JQL selector
if let Err(error) = serde_json::from_str::<Value>(json) {
return Err(format!("Invalid json: {error:?}"));
return fail_format!("Invalid json: {error:?}");
}

let mut result: Result<String, _> = Ok(String::default());
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/fetchpost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {

let mut redis_conn;
match redis_client.get_connection() {
Err(e) => return fail_format!(r#"Cannot connect to Redis using "{conn_str}": {e:?}"#),
Err(e) => return fail_clierror!(r#"Cannot connect to Redis using "{conn_str}": {e:?}"#),
Ok(x) => redis_conn = x,
}

Expand Down Expand Up @@ -359,7 +359,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
let vals: Vec<&str> = header.split(':').collect();

if vals.len() != 2 {
return fail_format!("{vals:?} is not a valid key-value pair. Expecting a key and a value separated by a colon.");
return fail_clierror!("{vals:?} is not a valid key-value pair. Expecting a key and a value separated by a colon.");
}

// allocate new String for header key to put into map
Expand Down Expand Up @@ -591,7 +591,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
}
final_response = match serde_json::from_str(&intermediate_redis_value) {
Ok(r) => r,
Err(e)=> return fail_format!("Cannot deserialize Redis cache value. Try flushing the Redis cache with --flushdb - {e}"),
Err(e)=> return fail_clierror!("Cannot deserialize Redis cache value. Try flushing the Redis cache with --flushdb - {e}"),
};
if !args.flag_cache_error && final_response.status_code != 200 {
let key = format!(
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
if let Some(skip_llines) = args.flag_skip_lastlines {
let row_count = util::count_rows(&rconfig)?;
if skip_llines > row_count {
return fail_format!(
return fail_clierror!(
"--skip-lastlines: {skip_llines} is greater than row_count: {row_count}."
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ impl Args {
let select1 = rconf1.selection(headers1)?;
let select2 = rconf2.selection(headers2)?;
if select1.len() != select2.len() {
return fail_format!(
return fail_clierror!(
"Column selections must have the same number of columns, \
but found column selections with {} and {} columns.",
select1.len(),
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/jsonl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
continue;
}
let human_idx = rowidx + 1; // not zero based, for readability
return fail_format!(
return fail_clierror!(
r#"Could not parse line {human_idx} as JSON! - {e}
Use `--ignore-errors` option to skip malformed input lines.
Use `tojsonl` command to convert _to_ jsonl instead of _from_ jsonl."#,
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
let lua_script = if args.flag_script_file {
match fs::read_to_string(&args.arg_script) {
Ok(script_file) => script_file,
Err(e) => return fail_format!("Cannot load Lua file: {e}"),
Err(e) => return fail_clierror!("Cannot load Lua file: {e}"),
}
} else {
args.arg_script
Expand Down Expand Up @@ -227,7 +227,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
record.push_field("");
}
_ => {
return fail_format!("Unexpected value type returned by provided Lua expression. {computed_value:?}");
return fail_clierror!("Unexpected value type returned by provided Lua expression. {computed_value:?}");
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/cmd/luajit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
let lua_script = if args.flag_script_file {
match fs::read_to_string(&args.arg_script) {
Ok(script_file) => script_file,
Err(e) => return fail_format!("Cannot load LuaJIT file: {e}"),
Err(e) => return fail_clierror!("Cannot load LuaJIT file: {e}"),
}
} else {
args.arg_script
Expand Down Expand Up @@ -230,7 +230,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
record.push_field("");
}
_ => {
return fail_format!("Unexpected value type returned by provided LuaJIT expression. {computed_value:?}");
return fail_clierror!("Unexpected value type returned by provided LuaJIT expression. {computed_value:?}");
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/cmd/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
if let Some(helper_file) = args.flag_helper {
helper_text = match fs::read_to_string(helper_file) {
Ok(helper_file) => helper_file,
Err(e) => return fail_format!("Cannot load python file: {e}"),
Err(e) => return fail_clierror!("Cannot load python file: {e}"),
}
}

Expand Down Expand Up @@ -250,7 +250,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
}
}
Err(e) => {
return fail_format!("Error reading file: {e}");
return fail_clierror!("Error reading file: {e}");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
let new_headers = new_rdr.byte_headers()?;

if headers.len() != new_headers.len() {
return fail_format!(
return fail_clierror!(
"The length of the CSV headers ({}) is different from the provided one ({}).",
headers.len(),
new_headers.len()
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
match infer_schema_from_stats(&args, &input_filename) {
Ok(map) => map,
Err(e) => {
return fail_format!("Failed to infer schema via stats and frequency: {e}");
return fail_clierror!("Failed to infer schema via stats and frequency: {e}");
}
};

Expand Down Expand Up @@ -144,7 +144,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {

let schema_pretty = match serde_json::to_string_pretty(&schema) {
Ok(s) => s,
Err(e) => return fail_format!("Cannot prettify schema json - {e}"),
Err(e) => return fail_clierror!("Cannot prettify schema json - {e}"),
};

if args.flag_stdout {
Expand Down Expand Up @@ -514,7 +514,7 @@ fn convert_to_string(byte_slice: &[u8]) -> CliResult<String> {
Ok(s) => s.to_string(),
Err(e) => {
let lossy_string = String::from_utf8_lossy(byte_slice);
return fail_format!("Can't convert byte slice to utf8 string. slice={byte_slice:?}, error={e}: {lossy_string}");
return fail_clierror!("Can't convert byte slice to utf8 string. slice={byte_slice:?}, error={e}: {lossy_string}");
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/cmd/sniff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
"detail": e.to_string()
}]
});
return fail_format!("{json_result}");
return fail_clierror!("{json_result}");
}
}
} else {
Expand All @@ -182,7 +182,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
println!("{disp}");
}
Err(e) => {
return fail_format!("sniff error: {e}");
return fail_clierror!("sniff error: {e}");
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/cmd/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,9 @@ fn init_date_inference(
match INFER_DATE_FLAGS.set(vec![true; headers.len()]) {
Ok(_) => (),
Err(e) => {
return Err(format!(
return fail_format!(
"Cannot init date inference flags for ALL fields - {e:?}"
))
)
}
};
} else {
Expand All @@ -394,13 +394,13 @@ fn init_date_inference(
}
match INFER_DATE_FLAGS.set(infer_date_flags) {
Ok(_) => (),
Err(e) => return Err(format!("Cannot init date inference flags - {e:?}")),
Err(e) => return fail_format!("Cannot init date inference flags - {e:?}"),
};
}
} else {
match INFER_DATE_FLAGS.set(vec![false; headers.len()]) {
Ok(_) => (),
Err(e) => return Err(format!("Cannot init empty date inference flags - {e:?}")),
Err(e) => return fail_format!("Cannot init empty date inference flags - {e:?}"),
};
}
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/tojsonl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
match infer_schema_from_stats(&schema_args, &input_filename) {
Ok(map) => map,
Err(e) => {
return fail_format!("Failed to infer field types via stats and frequency: {e}");
return fail_clierror!("Failed to infer field types via stats and frequency: {e}");
}
};

Expand Down
Loading

0 comments on commit e3d516d

Please sign in to comment.