Browse Source

Simplify some logic with a helper function

master
Timothy Warren 7 months ago
parent
commit
66d158d9c1
3 changed files with 40 additions and 9 deletions
  1. 3
    0
      .cargo/config
  2. 9
    9
      src/drivers.rs
  3. 28
    0
      src/lib.rs

+ 3
- 0
.cargo/config View File

@@ -0,0 +1,3 @@

[target]
rustflags = ["-C target-cpu=native"]

+ 9
- 9
src/drivers.rs View File

@@ -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<String> = 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
}

+ 28
- 0
src/lib.rs View File

@@ -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::<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
);
}
}

Loading…
Cancel
Save