reactive_graph_generator_codegen/
config.rs

1use std::path::Path;
2use std::path::PathBuf;
3
4use reactive_graph_graph::Namespace;
5#[cfg(test)]
6use reactive_graph_utils_test::r_lowercase_string;
7#[cfg(test)]
8use std::env::temp_dir;
9
10#[derive(Debug)]
11pub struct CodeGenerationConfig {
12    pub id: Option<Namespace>,
13    pub base_path: PathBuf,
14    pub formatting: bool,
15    pub ignore_formatter_errors: bool,
16    pub generate_builders: bool,
17}
18
19impl CodeGenerationConfig {
20    pub fn new(base_path: PathBuf) -> CodeGenerationConfig {
21        CodeGenerationConfig {
22            id: None,
23            base_path,
24            formatting: true,
25            ignore_formatter_errors: false,
26            generate_builders: false,
27        }
28    }
29
30    #[cfg(test)]
31    pub fn with_temp_dir() -> Self {
32        CodeGenerationConfig::new(temp_dir().join(r_lowercase_string()))
33    }
34
35    pub fn id(self, id: Namespace) -> Self {
36        CodeGenerationConfig {
37            id: Some(id),
38            base_path: self.base_path,
39            formatting: self.formatting,
40            ignore_formatter_errors: self.ignore_formatter_errors,
41            generate_builders: self.generate_builders,
42        }
43    }
44
45    pub fn disable_formatting(self) -> Self {
46        CodeGenerationConfig {
47            id: self.id,
48            base_path: self.base_path,
49            formatting: false,
50            ignore_formatter_errors: self.ignore_formatter_errors,
51            generate_builders: self.generate_builders,
52        }
53    }
54
55    pub fn ignore_formatter_errors(self) -> Self {
56        CodeGenerationConfig {
57            id: self.id,
58            base_path: self.base_path,
59            formatting: self.formatting,
60            ignore_formatter_errors: true,
61            generate_builders: self.generate_builders,
62        }
63    }
64
65    pub fn enable_builders(self) -> Self {
66        CodeGenerationConfig {
67            id: self.id,
68            base_path: self.base_path,
69            formatting: self.formatting,
70            ignore_formatter_errors: self.ignore_formatter_errors,
71            generate_builders: true,
72        }
73    }
74
75    pub fn base_path(&self) -> PathBuf {
76        self.base_path.clone()
77    }
78}
79
80impl AsRef<Path> for CodeGenerationConfig {
81    fn as_ref(&self) -> &Path {
82        self.base_path.as_path()
83    }
84}
85
86impl From<&Path> for CodeGenerationConfig {
87    fn from(path: &Path) -> Self {
88        Self::new(path.to_owned())
89    }
90}
91
92impl From<PathBuf> for CodeGenerationConfig {
93    fn from(path: PathBuf) -> Self {
94        Self::new(path)
95    }
96}