stringqb/src/lib.rs

56 lines
1.2 KiB
Rust
Raw Normal View History

2019-04-02 16:35:52 -04:00
//! # StringQB
//!
//! A query builder using mostly strings, with methods following common SQL syntax
2019-04-10 14:03:28 -04:00
#![warn(missing_docs)]
// Temporarily silence unused variables and uncalled code warnings
// @TODO remove when most of the code is implemented
2019-04-10 14:03:28 -04:00
#![allow(dead_code)]
#![allow(unused_variables)]
2019-04-02 16:35:52 -04:00
2019-04-11 17:16:35 -04:00
#[macro_use]
extern crate lazy_static;
2019-04-02 16:35:52 -04:00
pub mod drivers;
pub mod enums;
2019-04-02 16:35:52 -04:00
pub mod query_builder;
2019-04-10 20:11:26 -04:00
pub mod types;
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);
/// ```
pub fn split_map_join<'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_map_join() {
let start = "a\t,b ,c\n,d";
let expected = "a,b,c,d";
assert_eq!(
split_map_join(start, ",", |s| s.trim().to_string()),
expected
);
}
}