Skip to main content

haloumi_picus/
params.rs

1use super::vars::NamingConvention;
2
3/// Configuration for the Picus backend.
4#[derive(Clone, Debug)]
5pub struct PicusParams {
6    expr_cutoff: Option<usize>,
7    entrypoint: Option<String>,
8    naming_convention: NamingConvention,
9    optimize: bool,
10    inline: bool,
11}
12
13impl PicusParams {
14    /// Returns the naming convention of the variables.
15    pub fn naming_convention(&self) -> NamingConvention {
16        self.naming_convention
17    }
18
19    /// Returns true if optimization is enabled.
20    pub fn optimize(&self) -> bool {
21        self.optimize
22    }
23
24    /// Returns the maximum size of expressions, if configured.
25    pub fn expr_cutoff(&self) -> Option<usize> {
26        self.expr_cutoff
27    }
28
29    /// Returns the name of the top-level module.
30    pub fn entrypoint(&self) -> &str {
31        self.entrypoint.as_deref().unwrap_or("Main")
32    }
33
34    fn new() -> Self {
35        Self {
36            expr_cutoff: None,
37            entrypoint: None,
38            naming_convention: NamingConvention::Short,
39            optimize: true,
40            inline: false,
41        }
42    }
43
44    /// Returns wether inlining is enabled or not.
45    pub fn inline(&self) -> bool {
46        self.inline
47    }
48}
49
50impl Default for PicusParams {
51    fn default() -> Self {
52        Self::new()
53    }
54}
55
56/// Builder for configuring the parameters of the Picus backend.
57#[derive(Debug, Default)]
58pub struct PicusParamsBuilder(PicusParams);
59
60impl PicusParamsBuilder {
61    /// Creates a new builder using the parameter F as the prime.
62    pub fn new() -> Self {
63        Self(PicusParams::new())
64    }
65
66    /// Sets the maximum size for the expressions.
67    pub fn expr_cutoff(&mut self, expr_cutoff: usize) -> &mut Self {
68        self.0.expr_cutoff = Some(expr_cutoff);
69        self
70    }
71
72    /// Removes the configured value for the maximum size for expressions.
73    pub fn no_expr_cutoff(&mut self) -> &mut Self {
74        self.0.expr_cutoff = None;
75        self
76    }
77
78    /// Sets the name of the top-level module.
79    pub fn entrypoint(&mut self, name: &str) -> &mut Self {
80        self.0.entrypoint = Some(name.to_owned());
81        self
82    }
83
84    /// Sets the naming convention to 'short'.
85    pub fn short_names(&mut self) -> &mut Self {
86        self.0.naming_convention = NamingConvention::Short;
87        self
88    }
89
90    /// Enables optimizations.
91    pub fn optimize(&mut self) -> &mut Self {
92        self.0.optimize = true;
93        self
94    }
95
96    /// Disables optimizations.
97    pub fn no_optimize(&mut self) -> &mut Self {
98        self.0.optimize = false;
99        self
100    }
101
102    /// Sets lowering to inlining everything into one module.
103    pub fn inline(&mut self) -> &mut Self {
104        self.0.inline = true;
105        self
106    }
107
108    /// Sets lowering to creating separate modules for each group.
109    pub fn no_inline(&mut self) -> &mut Self {
110        self.0.inline = false;
111        self
112    }
113
114    /// Finishes the build process and returns the parameters.
115    pub fn build(&mut self) -> PicusParams {
116        std::mem::take(&mut self.0)
117    }
118}
119
120impl From<PicusParamsBuilder> for PicusParams {
121    fn from(builder: PicusParamsBuilder) -> PicusParams {
122        builder.0
123    }
124}