From 66d158d9c1a00437597b0e630ff9eb497d3ee26d Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 9 Apr 2019 17:14:07 -0400 Subject: [PATCH] Simplify some logic with a helper function --- .cargo/config | 3 +++ src/drivers.rs | 18 +++++++++--------- src/lib.rs | 28 ++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 .cargo/config diff --git a/.cargo/config b/.cargo/config new file mode 100644 index 0000000..449a819 --- /dev/null +++ b/.cargo/config @@ -0,0 +1,3 @@ + +[target] +rustflags = ["-C target-cpu=native"] \ No newline at end of file diff --git a/src/drivers.rs b/src/drivers.rs index e148c9c..3ea7a5a 100644 --- a/src/drivers.rs +++ b/src/drivers.rs @@ -1,6 +1,7 @@ //! Drivers //! //! Drivers represent a connection to a specific type of database engine +use crate::split_join_map; use std::fmt; #[cfg(feature = "postgres")] @@ -80,20 +81,19 @@ pub trait DatabaseDriver: fmt::Debug { let (open_char, close_char) = self._quotes(); - let mut trimmed_tiers: Vec = vec![]; - for tier in identifier.split(".") { - let mut tier = &mut tier.trim(); + let trimmed_tiers = split_join_map(identifier, ".", |tier| { + let tier = tier.trim(); // Here where the quoting actually happens. Everything // else is breaking down the identifier list for this. if tier.starts_with(open_char) && tier.ends_with(close_char) { - trimmed_tiers.push(tier.to_string()); - } else { - let tier = format!("{}{}{}", open_char, tier, close_char); - trimmed_tiers.push(tier.to_string()); + return tier.to_string(); } - } - trimmed_tiers.join(".") + + format!("{}{}{}", &open_char, tier, &close_char) + }); + + trimmed_tiers // @TODO Fix functional calls in 'select' queries } diff --git a/src/lib.rs b/src/lib.rs index d807fee..e973353 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,3 +6,31 @@ pub mod drivers; pub mod query_builder; pub mod types; + +pub fn split_join_map<'a>( + string: &'a str, + split_join_by: &str, + map_fn: impl (FnMut(&'a str) -> String) +) -> String { + string.split(split_join_by) + .into_iter() + .map(map_fn) + .collect::>() + .join(split_join_by) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_split_join_map() { + let start = "a\t,b ,c\n,d"; + let expected = "a,b,c,d"; + + assert_eq!( + split_join_map(start, ",", |s| s.trim().to_string()), + expected + ); + } +} \ No newline at end of file