From 84c824d405ca2a17d99e769d34a49cb533d7368b Mon Sep 17 00:00:00 2001 From: rfitzger Date: Fri, 20 Dec 2024 14:26:34 -0700 Subject: [PATCH] finalize bar on stderr with newline --- .../src/util/fs/read_utils.rs | 82 +++++++++++-------- 1 file changed, 46 insertions(+), 36 deletions(-) diff --git a/rust/routee-compass-core/src/util/fs/read_utils.rs b/rust/routee-compass-core/src/util/fs/read_utils.rs index edd96722..d176c542 100644 --- a/rust/routee-compass-core/src/util/fs/read_utils.rs +++ b/rust/routee-compass-core/src/util/fs/read_utils.rs @@ -16,41 +16,6 @@ type RawCallback<'a> = Option>; /// if COMPASS_PROGRESS=false, the bar is deactivated, otherwise it runs. const COMPASS_PROGRESS: &str = "COMPASS_PROGRESS"; -/// reads from a CSV into an iterator of T records. -/// building the iterator may fail with an io::Error. -/// each row hasn't yet been decoded so it is provided in a Result -/// -pub fn iterator_from_csv<'a, F, T>( - filepath: F, - has_headers: bool, - mut row_callback: CsvCallback<'a, T>, -) -> Result> + 'a>, io::Error> -where - F: AsRef, - T: serde::de::DeserializeOwned + 'a, -{ - let f = File::open(filepath.as_ref())?; - let r: Box = if fs_utils::is_gzip(filepath) { - Box::new(BufReader::new(GzDecoder::new(f))) - } else { - Box::new(f) - }; - let reader = ReaderBuilder::new() - .has_headers(has_headers) - .trim(csv::Trim::Fields) - .from_reader(r) - .into_deserialize::() - .inspect(move |r| { - if let Ok(t) = r { - if let Some(cb) = &mut row_callback { - cb(t); - } - } - }); - - Ok(Box::new(reader)) -} - /// reads a csv file into a vector. not space-optimized since size is not /// known. pub fn from_csv<'a, T>( @@ -63,6 +28,7 @@ where T: serde::de::DeserializeOwned + 'a, { let bar_opt = build_bar(progress); + let finalize_bar = bar_opt.is_some(); let row_callback: CsvCallback<'a, T> = match (callback, bar_opt) { (None, None) => None, @@ -82,6 +48,11 @@ where .into_iter() .collect::, csv::Error>>()? .into_boxed_slice(); + + if finalize_bar { + eprintln!(); + } + Ok(result) } @@ -100,6 +71,7 @@ where F: AsRef, { let bar_opt = build_bar(progress); + let finalize_bar = bar_opt.is_some(); let row_callback: RawCallback = match (callback, bar_opt) { (None, None) => None, @@ -113,11 +85,49 @@ where })), }; - if fs_utils::is_gzip(filepath.as_ref()) { + let result = if fs_utils::is_gzip(filepath.as_ref()) { Ok(read_gzip(filepath, op, row_callback)?) } else { Ok(read_regular(filepath, op, row_callback)?) + }; + if finalize_bar { + eprintln!(); } + result +} + +/// reads from a CSV into an iterator of T records. +/// building the iterator may fail with an io::Error. +/// each row hasn't yet been decoded so it is provided in a Result +pub fn iterator_from_csv<'a, F, T>( + filepath: F, + has_headers: bool, + mut row_callback: CsvCallback<'a, T>, +) -> Result> + 'a>, io::Error> +where + F: AsRef, + T: serde::de::DeserializeOwned + 'a, +{ + let f = File::open(filepath.as_ref())?; + let r: Box = if fs_utils::is_gzip(filepath) { + Box::new(BufReader::new(GzDecoder::new(f))) + } else { + Box::new(f) + }; + let reader = ReaderBuilder::new() + .has_headers(has_headers) + .trim(csv::Trim::Fields) + .from_reader(r) + .into_deserialize::() + .inspect(move |r| { + if let Ok(t) = r { + if let Some(cb) = &mut row_callback { + cb(t); + } + } + }); + + Ok(Box::new(reader)) } fn read_regular<'a, F, T>(