chur_build/cfg/
builder.rs1use std::fmt::Display;
2
3#[cfg(feature = "codegen")]
4use std::path::PathBuf;
5
6use crate::{defined_constants::ROOT_MANIFEST_DIR, dependency::Dependency};
7
8use super::Config;
9use crate::error::ChurResult;
10
11#[derive(Debug, Default)]
12pub struct ConfigBuilder {
13 root_dir: String,
14
15 dependencies: Vec<Dependency>,
16
17 protos: Vec<String>,
18 file_descriptors: bool,
19
20 #[cfg(feature = "codegen")]
21 codegen: Option<String>,
22}
23
24impl ConfigBuilder {
25 pub fn new() -> Self {
26 Self::default()
27 }
28
29 pub fn root_dir(mut self, root_dir: impl Display) -> Self {
33 self.root_dir = root_dir.to_string();
34 self
35 }
36
37 pub fn dependency(mut self, dependency: Dependency) -> Self {
42 self.dependencies.push(dependency);
43 self
44 }
45
46 pub fn protos(mut self, protos: impl IntoIterator<Item = impl Display>) -> Self {
51 let mut protos_as_strings = protos
52 .into_iter()
53 .map(|item| item.to_string())
54 .collect::<Vec<String>>();
55 self.protos.append(&mut protos_as_strings);
56
57 self
58 }
59
60 pub fn file_descriptors(mut self, file_descriptors: bool) -> Self {
62 self.file_descriptors = file_descriptors;
63 self
64 }
65
66 #[cfg(feature = "codegen")]
67 pub fn codegen(mut self, codegen_path: impl ToString) -> Self {
76 self.codegen = Some(codegen_path.to_string());
77 self
78 }
79
80 pub fn build(self) -> ChurResult<Config> {
84 let root_dir = ROOT_MANIFEST_DIR.join(self.root_dir);
85
86 let protos = self
87 .protos
88 .into_iter()
89 .map(|dir| root_dir.join(dir))
90 .collect::<Vec<_>>();
91
92 Ok(Config {
93 root_dir,
94 protos,
95 dependencies: self.dependencies,
96 file_descriptors: self.file_descriptors,
97
98 #[cfg(feature = "codegen")]
99 codegen: self.codegen.map(PathBuf::from),
100 })
101 }
102}