datalogic_rs/builder/
variable_builder.rs

1use crate::arena::DataArena;
2use crate::logic::Logic;
3
4/// Builder for variable references.
5///
6/// This builder provides a fluent interface for creating variable references,
7/// optionally with default values.
8pub struct VariableBuilder<'a> {
9    /// The arena in which all allocations will be made.
10    arena: &'a DataArena,
11    /// The path to the variable.
12    path: &'a str,
13    /// The default value to use if the variable is not found.
14    default: Option<Logic<'a>>,
15}
16
17impl<'a> VariableBuilder<'a> {
18    /// Creates a new variable builder.
19    pub fn new(arena: &'a DataArena, path: &str) -> Self {
20        let interned_path = arena.intern_str(path);
21        Self {
22            arena,
23            path: interned_path,
24            default: None,
25        }
26    }
27
28    /// Sets the default value to use if the variable is not found.
29    pub fn default(mut self, default: Logic<'a>) -> Self {
30        self.default = Some(default);
31        self
32    }
33
34    /// Builds the variable reference.
35    pub fn build(self) -> Logic<'a> {
36        Logic::variable(self.path, self.default, self.arena)
37    }
38}
39
40impl<'a> From<VariableBuilder<'a>> for Logic<'a> {
41    fn from(builder: VariableBuilder<'a>) -> Self {
42        builder.build()
43    }
44}