#[derive(Debug, Clone)]
pub struct Config {
pub(crate) parallel_threshold: usize,
pub(crate) min_records: usize,
pub(crate) record_path: Vec<Box<str>>,
}
impl Config {
pub fn new() -> Self {
Self::default()
}
pub fn with_parallel_threshold(mut self, bytes: usize) -> Self {
self.parallel_threshold = bytes;
self
}
pub fn with_min_records(mut self, n: usize) -> Self {
self.min_records = n;
self
}
pub fn with_record_path<I, S>(mut self, path: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<Box<str>>,
{
self.record_path = path.into_iter().map(Into::into).collect();
self
}
pub fn parallel_threshold(&self) -> usize {
self.parallel_threshold
}
pub fn min_records(&self) -> usize {
self.min_records
}
pub fn record_path(&self) -> &[Box<str>] {
&self.record_path
}
}
impl Default for Config {
fn default() -> Self {
Self {
parallel_threshold: 4 * 1024 * 1024, min_records: 64,
record_path: Vec::new(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn defaults_are_the_documented_values() {
let c = Config::default();
assert_eq!(c.parallel_threshold(), 4 * 1024 * 1024);
assert_eq!(c.min_records(), 64);
assert!(c.record_path().is_empty());
}
#[test]
fn new_matches_default() {
let (a, b) = (Config::new(), Config::default());
assert_eq!(a.parallel_threshold(), b.parallel_threshold());
assert_eq!(a.min_records(), b.min_records());
assert_eq!(a.record_path(), b.record_path());
}
#[test]
fn builders_are_independent_and_chainable() {
let c = Config::new()
.with_min_records(7)
.with_record_path(["a", "b"])
.with_parallel_threshold(123);
assert_eq!(c.parallel_threshold(), 123);
assert_eq!(c.min_records(), 7);
assert_eq!(c.record_path(), [Box::from("a"), Box::from("b")]);
}
#[test]
fn builders_overwrite_on_repeat() {
let c = Config::new()
.with_record_path(["first"])
.with_record_path(["second"])
.with_min_records(1)
.with_min_records(2);
assert_eq!(c.record_path(), [Box::from("second")]);
assert_eq!(c.min_records(), 2);
}
#[test]
fn record_path_accepts_common_string_types() {
let from_str = Config::new().with_record_path(["objects"]);
let from_string = Config::new().with_record_path(vec![String::from("objects")]);
let from_boxed = Config::new().with_record_path(vec![Box::<str>::from("objects")]);
assert_eq!(from_str.record_path(), from_string.record_path());
assert_eq!(from_str.record_path(), from_boxed.record_path());
}
}