From 9f796071d573792529b654514029c7a35ffe47d2 Mon Sep 17 00:00:00 2001 From: Alexander Date: Wed, 4 May 2022 01:19:28 +0300 Subject: initial commit:parser --- src/parser.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/parser.rs (limited to 'src/parser.rs') diff --git a/src/parser.rs b/src/parser.rs new file mode 100644 index 0000000..55fae87 --- /dev/null +++ b/src/parser.rs @@ -0,0 +1,53 @@ + +fn parse_rows (x_vals:&[f64],y_vals:&[&str]) -> (Vec,Vec){ + //pointer + + let mut parsed_x_vals= Vec::new(); + + let mut parsed_y_vals = Vec::new(); + + for (index,x_val) in x_vals.into_iter().enumerate(){ + + if let Some(val) = y_vals.get(index){ + + match val.trim().parse::(){ + Ok(float_type) => { + parsed_x_vals.push(*x_val); + parsed_y_vals.push(float_type); + + } + + Err(_not_float) => { + continue + } + } + + } + } + + (parsed_x_vals,parsed_y_vals) +} + + +#[cfg(test)] + +mod tests{ + + use super::*; + #[test] + + fn test_parsing_rows(){ + + //use a large test case + let x_vals = [12.2,12.1,16.5,11.1]; + let y_vals = ["12.1","11.1","11.6","11.7"]; + + let h = ["","1.2","11.1","","4.5","nan"]; + + assert_eq!(parse_rows(&x_vals,&y_vals), + (vec![12.2,12.1,16.5,11.1],vec![12.1,11.1,11.6,11.7])); + + assert_eq!(parse_rows(&x_vals, &h),(vec![12.1,16.5],vec![1.2,11.1])); + } +} + -- cgit 1.4.1