From a9293f62a8fd9565a67e5b01ada5d120bf5ff37e Mon Sep 17 00:00:00 2001 From: Thomas Hartmann Date: Mon, 14 Oct 2019 09:43:54 +0200 Subject: [PATCH 1/4] Adds some initial ideas for refactoring. --- src/commands/from_ssv.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/commands/from_ssv.rs b/src/commands/from_ssv.rs index 41a611f8cf..354d2cb2d1 100644 --- a/src/commands/from_ssv.rs +++ b/src/commands/from_ssv.rs @@ -33,6 +33,27 @@ impl WholeStreamCommand for FromSSV { } } +fn string_to_table(s: &str, headerless: bool) -> std::iter::Map> { + let mut lines = s.lines().filter(|l| !l.trim().is_empty()); + + let headers = lines + .next() + .unwrap() + .split_whitespace() + .map(|s| s.to_owned()) + .collect::>(); + + let header_row = if headerless { + (0..headers.len()) + .map(|i| format!("Column{}", i + 1)) + .collect::>() + } else { + headers + }; + + lines.map(|l| header_row.iter().zip(l.split_whitespace())) +} + fn from_ssv_string_to_value( s: &str, headerless: bool, @@ -118,3 +139,22 @@ fn from_ssv( Ok(stream.to_output_stream()) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_trims_empty_and_whitespace_only_lines() { + let input = r#" + + a b + + 1 2 + + 3 4 + "#; + + let +} +} \ No newline at end of file From 104b7824f58631d8dcfef96493827d9375ea7937 Mon Sep 17 00:00:00 2001 From: Thomas Hartmann Date: Mon, 14 Oct 2019 16:34:06 +0200 Subject: [PATCH 2/4] Updates return types. --- src/commands/from_ssv.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/commands/from_ssv.rs b/src/commands/from_ssv.rs index 354d2cb2d1..5f88147afe 100644 --- a/src/commands/from_ssv.rs +++ b/src/commands/from_ssv.rs @@ -33,7 +33,7 @@ impl WholeStreamCommand for FromSSV { } } -fn string_to_table(s: &str, headerless: bool) -> std::iter::Map> { +fn string_to_table(s: &str, headerless: bool) -> Vec> { let mut lines = s.lines().filter(|l| !l.trim().is_empty()); let headers = lines @@ -51,7 +51,7 @@ fn string_to_table(s: &str, headerless: bool) -> std::iter::Map Date: Mon, 14 Oct 2019 22:00:25 +0200 Subject: [PATCH 3/4] Adds conversion test for leading whitespace. Refactors string parsing into a separate function. --- src/commands/from_ssv.rs | 81 +++++++++++++++++++++++++--------------- 1 file changed, 50 insertions(+), 31 deletions(-) diff --git a/src/commands/from_ssv.rs b/src/commands/from_ssv.rs index 5f88147afe..56a3f10868 100644 --- a/src/commands/from_ssv.rs +++ b/src/commands/from_ssv.rs @@ -44,14 +44,22 @@ fn string_to_table(s: &str, headerless: bool) -> Vec> { .collect::>(); let header_row = if headerless { - (0..headers.len()) - .map(|i| format!("Column{}", i + 1)) + (1..=headers.len()) + .map(|i| format!("Column{}", i)) .collect::>() } else { headers }; - lines.map(|l| header_row.iter().zip(l.split_whitespace())).collect() + lines + .map(|l| { + header_row + .iter() + .zip(l.split_whitespace()) + .map(|(a, b)| (String::from(a), String::from(b))) + .collect() + }) + .collect() } fn from_ssv_string_to_value( @@ -59,33 +67,18 @@ fn from_ssv_string_to_value( headerless: bool, tag: impl Into, ) -> Option> { - let mut lines = s.lines().filter(|l| !l.is_empty()); - - let headers = lines - .next()? - .split_whitespace() - .map(|s| s.to_owned()) - .collect::>(); - - let header_row = if headerless { - (0..headers.len()) - .map(|i| format!("Column{}", i + 1)) - .collect::>() - } else { - headers - }; - let tag = tag.into(); - let rows = lines - .map(|l| { - let mut row = TaggedDictBuilder::new(tag); - for (column, value) in header_row.iter().zip(l.split_whitespace()) { - row.insert_tagged( - column.to_owned(), - Value::Primitive(Primitive::String(String::from(value))).tagged(tag), + let rows = string_to_table(s, headerless) + .iter() + .map(|row| { + let mut tagged_dict = TaggedDictBuilder::new(tag); + for (col, entry) in row { + tagged_dict.insert_tagged( + col, + Value::Primitive(Primitive::String(String::from(entry))).tagged(tag), ) } - row.into_tagged_value() + tagged_dict.into_tagged_value() }) .collect(); @@ -143,18 +136,44 @@ fn from_ssv( #[cfg(test)] mod tests { use super::*; + fn owned(x: &str, y: &str) -> (String, String) { + (String::from(x), String::from(y)) + } #[test] fn it_trims_empty_and_whitespace_only_lines() { let input = r#" - a b + a b - 1 2 + 1 2 3 4 "#; + let result = string_to_table(input, false); + assert_eq!( + result, + vec![ + vec![owned("a", "1"), owned("b", "2")], + vec![owned("a", "3"), owned("b", "4")] + ] + ); + } - let + #[test] + fn it_ignores_headers_when_headerless() { + let input = r#" + a b + 1 2 + 3 4 + "#; + let result = string_to_table(input, true); + assert_eq!( + result, + vec![ + vec![owned("Column1", "1"), owned("Column2", "2")], + vec![owned("Column1", "3"), owned("Column2", "4")] + ] + ); + } } -} \ No newline at end of file From 43ead45db6755cfdcc2e65dc367c7de2ba6f611c Mon Sep 17 00:00:00 2001 From: Thomas Hartmann Date: Mon, 14 Oct 2019 22:03:17 +0200 Subject: [PATCH 4/4] Removes rust_src_path and ssl_cert_file vars. --- shell.nix | 2 -- 1 file changed, 2 deletions(-) diff --git a/shell.nix b/shell.nix index f5c61ac0a8..d528cf8491 100644 --- a/shell.nix +++ b/shell.nix @@ -27,6 +27,4 @@ let in stdenv.mkDerivation { name = "nushell-rust"; buildInputs = nu-deps ++ rust; - RUST_SRC_PATH = "${nightly}/lib/rustlib/src/rust/src"; - SSL_CERT_FILE = "/etc/ssl/certs/ca-certificates.crt"; }