use std::fmt::Display;
use crate::{defined_constants::ROOT_MANIFEST_DIR, dependency::Dependency};
use super::Config;
use crate::error::ChurResult;
#[derive(Debug, Default)]
pub struct ConfigBuilder {
root_dir: String,
dependencies: Vec<Dependency>,
protos: Vec<String>,
}
impl ConfigBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn root_dir(mut self, root_dir: impl Display) -> Self {
self.root_dir = root_dir.to_string();
self
}
pub fn dependency(mut self, dependency: Dependency) -> Self {
self.dependencies.push(dependency);
self
}
pub fn protos(mut self, protos: impl IntoIterator<Item = impl Display>) -> Self {
let mut protos_as_strings = protos
.into_iter()
.map(|item| item.to_string())
.collect::<Vec<String>>();
self.protos.append(&mut protos_as_strings);
self
}
pub fn build(self) -> ChurResult<Config> {
let root_dir = ROOT_MANIFEST_DIR.join(self.root_dir);
let protos = self
.protos
.into_iter()
.map(|dir| root_dir.join(dir))
.collect::<Vec<_>>();
Ok(Config {
root_dir,
protos,
dependencies: self.dependencies,
})
}
}