use std::fmt::Display;
#[cfg(feature = "codegen")]
use std::path::PathBuf;
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>,
file_descriptors: bool,
#[cfg(feature = "codegen")]
codegen: Option<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 file_descriptors(mut self, file_descriptors: bool) -> Self {
self.file_descriptors = file_descriptors;
self
}
#[cfg(feature = "codegen")]
pub fn codegen(mut self, codegen_path: impl ToString) -> Self {
self.codegen = Some(codegen_path.to_string());
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,
file_descriptors: self.file_descriptors,
#[cfg(feature = "codegen")]
codegen: self.codegen.map(PathBuf::from),
})
}
}