datalogic_rs/builder/
type_builder.rs

1use crate::arena::DataArena;
2use crate::logic::{Logic, OperatorType};
3use crate::value::DataValue;
4
5/// Builder for the type operator.
6///
7/// This builder provides a fluent interface for creating type operators
8/// which return the type of a value as a string.
9pub struct TypeBuilder<'a> {
10    /// The arena in which all allocations will be made.
11    arena: &'a DataArena,
12}
13
14impl<'a> TypeBuilder<'a> {
15    /// Creates a new type builder.
16    pub fn new(arena: &'a DataArena) -> Self {
17        Self { arena }
18    }
19
20    /// Sets the argument for the type operator to a variable reference.
21    pub fn var(&self, path: &str) -> TypeOperationBuilder<'a> {
22        TypeOperationBuilder::new(self.arena).var(path)
23    }
24
25    /// Sets the argument for the type operator to a literal integer.
26    pub fn int(&self, value: i64) -> TypeOperationBuilder<'a> {
27        TypeOperationBuilder::new(self.arena).int(value)
28    }
29
30    /// Sets the argument for the type operator to a literal float.
31    pub fn float(&self, value: f64) -> TypeOperationBuilder<'a> {
32        TypeOperationBuilder::new(self.arena).float(value)
33    }
34
35    /// Sets the argument for the type operator to a literal string.
36    pub fn string(&self, value: &str) -> TypeOperationBuilder<'a> {
37        TypeOperationBuilder::new(self.arena).string(value)
38    }
39
40    /// Sets the argument for the type operator to a literal boolean.
41    pub fn bool(&self, value: bool) -> TypeOperationBuilder<'a> {
42        TypeOperationBuilder::new(self.arena).bool(value)
43    }
44
45    /// Sets the argument for the type operator to a literal null.
46    pub fn null(&self) -> TypeOperationBuilder<'a> {
47        TypeOperationBuilder::new(self.arena).null()
48    }
49
50    /// Sets the argument for the type operator to a literal array.
51    pub fn array(&self) -> TypeOperationBuilder<'a> {
52        TypeOperationBuilder::new(self.arena).array()
53    }
54
55    /// Sets the argument for the type operator to a literal object.
56    pub fn object(&self) -> TypeOperationBuilder<'a> {
57        TypeOperationBuilder::new(self.arena).object()
58    }
59}
60
61/// Builder for a type operation with its argument.
62pub struct TypeOperationBuilder<'a> {
63    /// The arena in which all allocations will be made.
64    arena: &'a DataArena,
65    /// The argument for the type operator.
66    arg: Option<Logic<'a>>,
67}
68
69impl<'a> TypeOperationBuilder<'a> {
70    /// Creates a new type operation builder.
71    pub fn new(arena: &'a DataArena) -> Self {
72        Self { arena, arg: None }
73    }
74
75    /// Sets the argument for the type operator to a variable reference.
76    pub fn var(mut self, path: &str) -> Self {
77        self.arg = Some(Logic::variable(path, None, self.arena));
78        self
79    }
80
81    /// Sets the argument for the type operator to a literal integer.
82    pub fn int(mut self, value: i64) -> Self {
83        self.arg = Some(Logic::literal(DataValue::integer(value), self.arena));
84        self
85    }
86
87    /// Sets the argument for the type operator to a literal float.
88    pub fn float(mut self, value: f64) -> Self {
89        self.arg = Some(Logic::literal(DataValue::float(value), self.arena));
90        self
91    }
92
93    /// Sets the argument for the type operator to a literal string.
94    pub fn string(mut self, value: &str) -> Self {
95        self.arg = Some(Logic::literal(
96            DataValue::string(self.arena, value),
97            self.arena,
98        ));
99        self
100    }
101
102    /// Sets the argument for the type operator to a literal boolean.
103    pub fn bool(mut self, value: bool) -> Self {
104        self.arg = Some(Logic::literal(DataValue::bool(value), self.arena));
105        self
106    }
107
108    /// Sets the argument for the type operator to a literal null.
109    pub fn null(mut self) -> Self {
110        self.arg = Some(Logic::literal(DataValue::null(), self.arena));
111        self
112    }
113
114    /// Sets the argument for the type operator to a literal empty array.
115    pub fn array(mut self) -> Self {
116        let empty_array = DataValue::Array(self.arena.vec_into_slice(Vec::new()));
117        self.arg = Some(Logic::literal(empty_array, self.arena));
118        self
119    }
120
121    /// Sets the argument for the type operator to a literal empty object.
122    pub fn object(mut self) -> Self {
123        let empty_object = DataValue::Object(self.arena.vec_into_slice(Vec::new()));
124        self.arg = Some(Logic::literal(empty_object, self.arena));
125        self
126    }
127
128    /// Sets the argument for the type operator to a provided logic expression.
129    pub fn logic(mut self, logic: Logic<'a>) -> Self {
130        self.arg = Some(logic);
131        self
132    }
133
134    /// Builds the type operation logic.
135    pub fn build(self) -> Logic<'a> {
136        let arg = self.arg.expect("Type operator requires an argument");
137        Logic::operator(OperatorType::Type, vec![arg], self.arena)
138    }
139}