datalogic_rs/builder/
string_builder.rs1use crate::arena::DataArena;
2use crate::logic::StringOp;
3use crate::logic::{Logic, OperatorType};
4
5pub struct StringBuilder<'a> {
10 arena: &'a DataArena,
12}
13
14impl<'a> StringBuilder<'a> {
15 pub fn new(arena: &'a DataArena) -> Self {
17 Self { arena }
18 }
19
20 pub fn concat_op(&self) -> StringOperationBuilder<'a> {
22 StringOperationBuilder::new(self.arena, StringOp::Cat)
23 }
24
25 pub fn substr_op(&self) -> SubstringBuilder<'a> {
27 SubstringBuilder::new(self.arena)
28 }
29
30 pub fn starts_with_op(&self) -> StringOperationBuilder<'a> {
32 StringOperationBuilder::new(self.arena, StringOp::StartsWith)
33 }
34
35 pub fn ends_with_op(&self) -> StringOperationBuilder<'a> {
37 StringOperationBuilder::new(self.arena, StringOp::EndsWith)
38 }
39
40 pub fn upper_op(&self) -> StringOperationBuilder<'a> {
42 StringOperationBuilder::new(self.arena, StringOp::Upper)
43 }
44
45 pub fn lower_op(&self) -> StringOperationBuilder<'a> {
47 StringOperationBuilder::new(self.arena, StringOp::Lower)
48 }
49
50 pub fn trim_op(&self) -> StringOperationBuilder<'a> {
52 StringOperationBuilder::new(self.arena, StringOp::Trim)
53 }
54}
55
56pub struct StringOperationBuilder<'a> {
58 arena: &'a DataArena,
60 operation: StringOp,
62 operands: Vec<Logic<'a>>,
64}
65
66impl<'a> StringOperationBuilder<'a> {
67 pub fn new(arena: &'a DataArena, operation: StringOp) -> Self {
69 Self {
70 arena,
71 operation,
72 operands: Vec::new(),
73 }
74 }
75
76 pub fn operand(mut self, operand: Logic<'a>) -> Self {
78 self.operands.push(operand);
79 self
80 }
81
82 pub fn var(mut self, path: &str) -> Self {
84 let var = Logic::variable(path, None, self.arena);
85 self.operands.push(var);
86 self
87 }
88
89 pub fn string(mut self, value: &str) -> Self {
91 let val = Logic::literal(
92 crate::value::DataValue::string(self.arena, value),
93 self.arena,
94 );
95 self.operands.push(val);
96 self
97 }
98
99 pub fn int(mut self, value: i64) -> Self {
101 let val = Logic::literal(crate::value::DataValue::integer(value), self.arena);
102 self.operands.push(val);
103 self
104 }
105
106 pub fn float(mut self, value: f64) -> Self {
108 let val = Logic::literal(crate::value::DataValue::float(value), self.arena);
109 self.operands.push(val);
110 self
111 }
112
113 pub fn bool(mut self, value: bool) -> Self {
115 let val = Logic::literal(crate::value::DataValue::bool(value), self.arena);
116 self.operands.push(val);
117 self
118 }
119
120 pub fn build(self) -> Logic<'a> {
122 if self.operands.is_empty() {
123 return Logic::literal(crate::value::DataValue::string(self.arena, ""), self.arena);
125 }
126
127 Logic::operator(
128 OperatorType::String(self.operation),
129 self.operands,
130 self.arena,
131 )
132 }
133}
134
135pub struct SubstringBuilder<'a> {
137 arena: &'a DataArena,
139 string: Option<Logic<'a>>,
141 start: Option<Logic<'a>>,
143 length: Option<Logic<'a>>,
145}
146
147impl<'a> SubstringBuilder<'a> {
148 pub fn new(arena: &'a DataArena) -> Self {
150 Self {
151 arena,
152 string: None,
153 start: None,
154 length: None,
155 }
156 }
157
158 pub fn string(mut self, string: Logic<'a>) -> Self {
160 self.string = Some(string);
161 self
162 }
163
164 pub fn var(self, path: &str) -> Self {
166 let var = Logic::variable(path, None, self.arena);
167 self.string(var)
168 }
169
170 pub fn literal(self, value: &str) -> Self {
172 let val = Logic::literal(
173 crate::value::DataValue::string(self.arena, value),
174 self.arena,
175 );
176 self.string(val)
177 }
178
179 pub fn start(mut self, start: Logic<'a>) -> Self {
181 self.start = Some(start);
182 self
183 }
184
185 pub fn start_at(self, index: i64) -> Self {
187 let val = Logic::literal(crate::value::DataValue::integer(index), self.arena);
188 self.start(val)
189 }
190
191 pub fn length(mut self, length: Logic<'a>) -> Self {
193 self.length = Some(length);
194 self
195 }
196
197 pub fn take(self, length: i64) -> Self {
199 let val = Logic::literal(crate::value::DataValue::integer(length), self.arena);
200 self.length(val)
201 }
202
203 pub fn build(self) -> Logic<'a> {
209 let string = self.string.unwrap_or_else(|| {
210 Logic::literal(crate::value::DataValue::string(self.arena, ""), self.arena)
211 });
212
213 let start = self
214 .start
215 .unwrap_or_else(|| Logic::literal(crate::value::DataValue::integer(0), self.arena));
216
217 let mut operands = vec![string, start];
218
219 if let Some(length) = self.length {
220 operands.push(length);
221 }
222
223 Logic::operator(OperatorType::String(StringOp::Substr), operands, self.arena)
224 }
225}