2019-04-02 16:35:52 -04:00
|
|
|
//! # StringQB
|
|
|
|
//!
|
|
|
|
//! A query builder using mostly strings, with methods following common SQL syntax
|
2019-04-04 16:39:05 -04:00
|
|
|
// #![warn(missing_docs)]
|
2019-04-02 16:35:52 -04:00
|
|
|
|
|
|
|
pub mod drivers;
|
|
|
|
pub mod query_builder;
|
|
|
|
pub mod types;
|
2019-04-09 17:14:07 -04:00
|
|
|
|
2019-04-09 18:55:53 -04:00
|
|
|
/// Split a string, apply a closure to each substring,
|
|
|
|
/// then join the string back together
|
|
|
|
///
|
|
|
|
/// For example:
|
|
|
|
/// ```
|
|
|
|
/// use stringqb::split_map_join;
|
|
|
|
///
|
|
|
|
/// let result = split_map_join("a\n,b, c\t,d", ",", |s| s.trim().to_string());
|
|
|
|
/// assert_eq!("a,b,c,d", result);
|
|
|
|
/// ```
|
2019-04-09 17:33:26 -04:00
|
|
|
pub fn split_map_join<'a>(
|
2019-04-09 17:14:07 -04:00
|
|
|
string: &'a str,
|
|
|
|
split_join_by: &str,
|
2019-04-09 17:33:26 -04:00
|
|
|
map_fn: impl (FnMut(&'a str) -> String),
|
2019-04-09 17:14:07 -04:00
|
|
|
) -> String {
|
2019-04-09 17:33:26 -04:00
|
|
|
string
|
|
|
|
.split(split_join_by)
|
2019-04-09 17:14:07 -04:00
|
|
|
.into_iter()
|
|
|
|
.map(map_fn)
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(split_join_by)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
2019-04-09 17:33:26 -04:00
|
|
|
fn test_split_map_join() {
|
2019-04-09 17:14:07 -04:00
|
|
|
let start = "a\t,b ,c\n,d";
|
|
|
|
let expected = "a,b,c,d";
|
|
|
|
|
|
|
|
assert_eq!(
|
2019-04-09 17:33:26 -04:00
|
|
|
split_map_join(start, ",", |s| s.trim().to_string()),
|
2019-04-09 17:14:07 -04:00
|
|
|
expected
|
|
|
|
);
|
|
|
|
}
|
2019-04-09 17:33:26 -04:00
|
|
|
}
|