use derive_builder::Builder;
#[derive(Default, Debug, Builder, PartialEq, Clone)]
#[builder(default, setter(into))]
pub struct ProjectConfig {
pub include: Vec<String>,
pub exclude: Vec<String>,
}
impl ProjectConfig {
pub fn new() -> Self {
Self::default()
}
pub fn add_include<S: Into<String>>(mut self, include: S) -> Self {
self.include.push(include.into());
self
}
pub fn add_includes<I, S>(mut self, includes: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.include.extend(includes.into_iter().map(Into::into));
self
}
pub fn add_exclude<S: Into<String>>(mut self, exclude: S) -> Self {
self.exclude.push(exclude.into());
self
}
pub fn add_excludes<I, S>(mut self, excludes: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.exclude.extend(excludes.into_iter().map(Into::into));
self
}
pub fn clear_include(mut self) -> Self {
self.include.clear();
self
}
pub fn clear_exclude(mut self) -> Self {
self.exclude.clear();
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_init_config() {
let config = ProjectConfig::new();
println!("{:?}", config);
assert_eq!(config.include.len(), 0);
assert_eq!(config.exclude.len(), 0);
}
#[test]
fn test_width_include() {
let config = ProjectConfig::new()
.add_includes(["./src"])
.add_excludes(["./node_modules", "./dist"]);
println!("{:?}", config);
assert_eq!(config.include.len(), 1);
assert_eq!(config.exclude.len(), 2);
let config = config.add_include("123").add_exclude("321");
println!("{:?}", config);
assert_eq!(config.include.len(), 2);
assert_eq!(config.exclude.len(), 3);
}
#[test]
fn test_width_include_string() {
let config = ProjectConfig::new()
.add_includes(vec!["./src".to_string()])
.add_excludes(vec!["./node_modules".to_string(), "./dist".to_string()]);
println!("{:?}", config);
assert_eq!(config.include.len(), 1);
assert_eq!(config.exclude.len(), 2);
let config = config
.add_include("123".to_string())
.add_exclude("321".to_string());
println!("{:?}", config);
assert_eq!(config.include.len(), 2);
assert_eq!(config.exclude.len(), 3);
}
#[test]
fn test_clear() {
let mut config = ProjectConfig::new()
.add_excludes(["123", "1231"])
.add_includes(["1111"]);
config = config.clear_include().clear_exclude();
println!("{:?}", config);
assert_eq!(config.include.len(), 0);
assert_eq!(config.exclude.len(), 0);
}
}