vrl/value/kind/
builder.rs

1use super::{Collection, Field, Index, Kind};
2
3// Initializer functions.
4impl Kind {
5    /// The "any" type state.
6    ///
7    /// This state implies all states for the type are valid. There is no known information that
8    /// can be gleaned from the type.
9    #[must_use]
10    pub fn any() -> Self {
11        Self {
12            bytes: Some(()),
13            integer: Some(()),
14            float: Some(()),
15            boolean: Some(()),
16            timestamp: Some(()),
17            regex: Some(()),
18            null: Some(()),
19            undefined: Some(()),
20            array: Some(Collection::any()),
21            object: Some(Collection::any()),
22        }
23    }
24
25    /// The "json" type state.
26    ///
27    /// This state is similar to `any`, except that it excludes any types that can't be represented
28    /// in a native JSON-type (such as `timestamp` and `regex`).
29    #[must_use]
30    pub fn json() -> Self {
31        Self {
32            bytes: Some(()),
33            integer: Some(()),
34            float: Some(()),
35            boolean: Some(()),
36            timestamp: None,
37            regex: None,
38            null: Some(()),
39            undefined: None,
40            array: Some(Collection::json()),
41            object: Some(Collection::json()),
42        }
43    }
44
45    /// The "bytes" type state.
46    #[must_use]
47    pub const fn bytes() -> Self {
48        Self {
49            bytes: Some(()),
50            integer: None,
51            float: None,
52            boolean: None,
53            timestamp: None,
54            regex: None,
55            null: None,
56            undefined: None,
57            array: None,
58            object: None,
59        }
60    }
61
62    /// The "integer" type state.
63    #[must_use]
64    pub const fn integer() -> Self {
65        Self {
66            bytes: None,
67            integer: Some(()),
68            float: None,
69            boolean: None,
70            timestamp: None,
71            regex: None,
72            null: None,
73            undefined: None,
74            array: None,
75            object: None,
76        }
77    }
78
79    /// The "float" type state.
80    #[must_use]
81    pub const fn float() -> Self {
82        Self {
83            bytes: None,
84            integer: None,
85            float: Some(()),
86            boolean: None,
87            timestamp: None,
88            regex: None,
89            null: None,
90            undefined: None,
91            array: None,
92            object: None,
93        }
94    }
95
96    /// The "boolean" type state.
97    #[must_use]
98    pub const fn boolean() -> Self {
99        Self {
100            bytes: None,
101            integer: None,
102            float: None,
103            boolean: Some(()),
104            timestamp: None,
105            regex: None,
106            null: None,
107            undefined: None,
108            array: None,
109            object: None,
110        }
111    }
112
113    /// The "timestamp" type state.
114    #[must_use]
115    pub const fn timestamp() -> Self {
116        Self {
117            bytes: None,
118            integer: None,
119            float: None,
120            boolean: None,
121            timestamp: Some(()),
122            regex: None,
123            null: None,
124            undefined: None,
125            array: None,
126            object: None,
127        }
128    }
129
130    /// The "regex" type state.
131    #[must_use]
132    pub const fn regex() -> Self {
133        Self {
134            bytes: None,
135            integer: None,
136            float: None,
137            boolean: None,
138            timestamp: None,
139            regex: Some(()),
140            null: None,
141            undefined: None,
142            array: None,
143            object: None,
144        }
145    }
146
147    /// The "null" type state.
148    #[must_use]
149    pub const fn null() -> Self {
150        Self {
151            bytes: None,
152            integer: None,
153            float: None,
154            boolean: None,
155            timestamp: None,
156            regex: None,
157            null: Some(()),
158            undefined: None,
159            array: None,
160            object: None,
161        }
162    }
163
164    /// The "undefined" type state.
165    #[must_use]
166    pub const fn undefined() -> Self {
167        Self {
168            bytes: None,
169            integer: None,
170            float: None,
171            boolean: None,
172            timestamp: None,
173            regex: None,
174            null: None,
175            undefined: Some(()),
176            array: None,
177            object: None,
178        }
179    }
180
181    /// The "never" type state.
182    #[must_use]
183    pub const fn never() -> Self {
184        Self {
185            bytes: None,
186            integer: None,
187            float: None,
188            boolean: None,
189            timestamp: None,
190            regex: None,
191            null: None,
192            undefined: None,
193            array: None,
194            object: None,
195        }
196    }
197
198    /// The "array" type state.
199    #[must_use]
200    pub fn array(collection: impl Into<Collection<Index>>) -> Self {
201        Self {
202            bytes: None,
203            integer: None,
204            float: None,
205            boolean: None,
206            timestamp: None,
207            regex: None,
208            null: None,
209            undefined: None,
210            array: Some(collection.into()),
211            object: None,
212        }
213    }
214
215    /// The "object" type state.
216    #[must_use]
217    pub fn object(collection: impl Into<Collection<Field>>) -> Self {
218        Self {
219            bytes: None,
220            integer: None,
221            float: None,
222            boolean: None,
223            timestamp: None,
224            regex: None,
225            null: None,
226            undefined: None,
227            array: None,
228            object: Some(collection.into()),
229        }
230    }
231
232    /// An object that can have any fields.
233    #[must_use]
234    pub fn any_object() -> Self {
235        Self::object(Collection::any())
236    }
237}
238
239// `or_*` methods to extend the state of a type using a builder-like API.
240impl Kind {
241    /// Add the `bytes` state to the type.
242    #[must_use]
243    pub const fn or_bytes(mut self) -> Self {
244        self.bytes = Some(());
245        self
246    }
247
248    /// Add the `integer` state to the type.
249    #[must_use]
250    pub const fn or_integer(mut self) -> Self {
251        self.integer = Some(());
252        self
253    }
254
255    /// Add the `float` state to the type.
256    #[must_use]
257    pub const fn or_float(mut self) -> Self {
258        self.float = Some(());
259        self
260    }
261
262    /// Add the `boolean` state to the type.
263    #[must_use]
264    pub const fn or_boolean(mut self) -> Self {
265        self.boolean = Some(());
266        self
267    }
268
269    /// Add the `timestamp` state to the type.
270    #[must_use]
271    pub const fn or_timestamp(mut self) -> Self {
272        self.timestamp = Some(());
273        self
274    }
275
276    /// Add the `regex` state to the type.
277    #[must_use]
278    pub const fn or_regex(mut self) -> Self {
279        self.regex = Some(());
280        self
281    }
282
283    /// Add the `null` state to the type.
284    #[must_use]
285    pub const fn or_null(mut self) -> Self {
286        self.null = Some(());
287        self
288    }
289
290    /// Add the `undefined` state to the type.
291    #[must_use]
292    pub const fn or_undefined(mut self) -> Self {
293        self.undefined = Some(());
294        self
295    }
296
297    /// Add the `array` state to the type.
298    #[must_use]
299    pub fn or_array(mut self, collection: impl Into<Collection<Index>>) -> Self {
300        self.array = Some(collection.into());
301        self
302    }
303
304    /// Add the `object` state to the type.
305    #[must_use]
306    pub fn or_object(mut self, collection: impl Into<Collection<Field>>) -> Self {
307        self.object = Some(collection.into());
308        self
309    }
310}
311
312// `add_*` methods to extend the state of a type.
313impl Kind {
314    /// Add the `bytes` state to the type.
315    ///
316    /// If the type already included this state, the function returns `false`.
317    pub fn add_bytes(&mut self) -> bool {
318        self.bytes.replace(()).is_none()
319    }
320
321    /// Add the `integer` state to the type.
322    ///
323    /// If the type already included this state, the function returns `false`.
324    pub fn add_integer(&mut self) -> bool {
325        self.integer.replace(()).is_none()
326    }
327
328    /// Add the `float` state to the type.
329    ///
330    /// If the type already included this state, the function returns `false`.
331    pub fn add_float(&mut self) -> bool {
332        self.float.replace(()).is_none()
333    }
334
335    /// Add the `boolean` state to the type.
336    ///
337    /// If the type already included this state, the function returns `false`.
338    pub fn add_boolean(&mut self) -> bool {
339        self.boolean.replace(()).is_none()
340    }
341
342    /// Add the `timestamp` state to the type.
343    ///
344    /// If the type already included this state, the function returns `false`.
345    pub fn add_timestamp(&mut self) -> bool {
346        self.timestamp.replace(()).is_none()
347    }
348
349    /// Add the `regex` state to the type.
350    ///
351    /// If the type already included this state, the function returns `false`.
352    pub fn add_regex(&mut self) -> bool {
353        self.regex.replace(()).is_none()
354    }
355
356    /// Add the `null` state to the type.
357    ///
358    /// If the type already included this state, the function returns `false`.
359    pub fn add_null(&mut self) -> bool {
360        self.null.replace(()).is_none()
361    }
362
363    /// Add the `null` state to the type.
364    ///
365    /// If the type already included this state, the function returns `false`.
366    pub fn add_undefined(&mut self) -> bool {
367        self.undefined.replace(()).is_none()
368    }
369
370    /// Add the `array` state to the type.
371    ///
372    /// If the type already included this state, the function returns `false`.
373    pub fn add_array(&mut self, collection: impl Into<Collection<Index>>) -> bool {
374        self.array.replace(collection.into()).is_none()
375    }
376
377    /// Add the `object` state to the type.
378    ///
379    /// If the type already included this state, the function returns `false`.
380    pub fn add_object(&mut self, collection: impl Into<Collection<Field>>) -> bool {
381        self.object.replace(collection.into()).is_none()
382    }
383}
384
385// `remove_*` methods to narrow the state of a type.
386impl Kind {
387    /// Remove the `bytes` state from the type.
388    ///
389    /// If the type previously included this state, true is returned.
390    pub fn remove_bytes(&mut self) -> bool {
391        self.bytes.take().is_some()
392    }
393
394    /// Remove the `integer` state from the type.
395    ///
396    /// If the type previously included this state, true is returned.
397    pub fn remove_integer(&mut self) -> bool {
398        self.integer.take().is_some()
399    }
400
401    /// Remove the `float` state from the type.
402    ///
403    /// If the type previously included this state, true is returned.
404    pub fn remove_float(&mut self) -> bool {
405        self.float.take().is_some()
406    }
407
408    /// Remove the `boolean` state from the type.
409    ///
410    /// If the type previously included this state, true is returned.
411    pub fn remove_boolean(&mut self) -> bool {
412        self.boolean.take().is_some()
413    }
414
415    /// Remove the `timestamp` state from the type.
416    ///
417    /// If the type previously included this state, true is returned.
418    pub fn remove_timestamp(&mut self) -> bool {
419        self.timestamp.take().is_some()
420    }
421
422    /// Remove the `regex` state from the type.
423    ///
424    /// If the type previously included this state, true is returned.
425    pub fn remove_regex(&mut self) -> bool {
426        self.regex.take().is_some()
427    }
428
429    /// Remove the `null` state from the type.
430    ///
431    /// If the type previously included this state, true is returned.
432    pub fn remove_null(&mut self) -> bool {
433        self.null.take().is_some()
434    }
435
436    /// Remove the `undefined` state from the type.
437    ///
438    /// If the type previously included this state, true is returned.
439    pub fn remove_undefined(&mut self) -> bool {
440        self.undefined.take().is_some()
441    }
442
443    /// Remove the `array` state from the type.
444    ///
445    /// If the type previously included this state, true is returned.
446    pub fn remove_array(&mut self) -> bool {
447        self.array.take().is_some()
448    }
449
450    /// Remove the `object` state from the type.
451    ///
452    /// If the type previously included this state, true is returned.
453    pub fn remove_object(&mut self) -> bool {
454        self.object.take().is_some()
455    }
456}
457
458// `without_*` methods to narrow the state of a type (functional).
459impl Kind {
460    /// Remove the `undefined` state from the type, and return it.
461    #[must_use]
462    pub fn without_undefined(&self) -> Self {
463        let mut kind = self.clone();
464        kind.remove_undefined();
465        kind
466    }
467
468    /// Remove the `array` state from the type, and return it.
469    #[must_use]
470    pub fn without_array(&self) -> Self {
471        let mut kind = self.clone();
472        kind.remove_array();
473        kind
474    }
475
476    /// Remove the `object` state from the type, and return it.
477    #[must_use]
478    pub fn without_object(&self) -> Self {
479        let mut kind = self.clone();
480        kind.remove_object();
481        kind
482    }
483}