Simplify some logic with a helper function

This commit is contained in:
Timothy Warren 2019-04-09 17:14:07 -04:00
parent 0e1c6755b0
commit 66d158d9c1
3 changed files with 40 additions and 9 deletions

3
.cargo/config Normal file
View File

@ -0,0 +1,3 @@
[target]
rustflags = ["-C target-cpu=native"]

View File

@ -1,6 +1,7 @@
//! Drivers //! Drivers
//! //!
//! Drivers represent a connection to a specific type of database engine //! Drivers represent a connection to a specific type of database engine
use crate::split_join_map;
use std::fmt; use std::fmt;
#[cfg(feature = "postgres")] #[cfg(feature = "postgres")]
@ -80,20 +81,19 @@ pub trait DatabaseDriver: fmt::Debug {
let (open_char, close_char) = self._quotes(); let (open_char, close_char) = self._quotes();
let mut trimmed_tiers: Vec<String> = vec![]; let trimmed_tiers = split_join_map(identifier, ".", |tier| {
for tier in identifier.split(".") { let tier = tier.trim();
let mut tier = &mut tier.trim();
// Here where the quoting actually happens. Everything // Here where the quoting actually happens. Everything
// else is breaking down the identifier list for this. // else is breaking down the identifier list for this.
if tier.starts_with(open_char) && tier.ends_with(close_char) { if tier.starts_with(open_char) && tier.ends_with(close_char) {
trimmed_tiers.push(tier.to_string()); return tier.to_string();
} else {
let tier = format!("{}{}{}", open_char, tier, close_char);
trimmed_tiers.push(tier.to_string());
} }
}
trimmed_tiers.join(".") format!("{}{}{}", &open_char, tier, &close_char)
});
trimmed_tiers
// @TODO Fix functional calls in 'select' queries // @TODO Fix functional calls in 'select' queries
} }

View File

@ -6,3 +6,31 @@
pub mod drivers; pub mod drivers;
pub mod query_builder; pub mod query_builder;
pub mod types; 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::<Vec<String>>()
.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
);
}
}