tellaro-query-language 1.3.8

A flexible, human-friendly query language for searching and filtering structured data
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//! Encoding and decoding mutators for TQL.
//!
//! Provides transformations for base64, URL, and hex encoding/decoding.

use super::{Mutator, MutatorParams};
use crate::error::{Result, TqlError};
use base64::{engine::general_purpose, Engine as _};
use serde_json::Value as JsonValue;

/// Mutator that encodes string values to Base64
pub struct Base64EncodeMutator {
    _params: MutatorParams,
}

impl Base64EncodeMutator {
    pub fn new(params: MutatorParams) -> Self {
        Self { _params: params }
    }
}

impl Mutator for Base64EncodeMutator {
    fn apply(
        &self,
        _field_name: &str,
        _record: &JsonValue,
        value: &JsonValue,
    ) -> Result<JsonValue> {
        match value {
            JsonValue::String(s) => {
                let encoded = general_purpose::STANDARD.encode(s.as_bytes());
                Ok(JsonValue::String(encoded))
            }
            JsonValue::Array(arr) => {
                let transformed: Vec<JsonValue> = arr
                    .iter()
                    .map(|item| {
                        if let JsonValue::String(s) = item {
                            JsonValue::String(general_purpose::STANDARD.encode(s.as_bytes()))
                        } else {
                            item.clone()
                        }
                    })
                    .collect();
                Ok(JsonValue::Array(transformed))
            }
            _ => Ok(value.clone()),
        }
    }

    fn name(&self) -> &str {
        "b64encode"
    }
}

/// Mutator that decodes Base64-encoded string values
pub struct Base64DecodeMutator {
    _params: MutatorParams,
}

impl Base64DecodeMutator {
    pub fn new(params: MutatorParams) -> Self {
        Self { _params: params }
    }
}

impl Mutator for Base64DecodeMutator {
    fn apply(
        &self,
        _field_name: &str,
        _record: &JsonValue,
        value: &JsonValue,
    ) -> Result<JsonValue> {
        match value {
            JsonValue::String(s) => {
                let decoded_bytes = general_purpose::STANDARD
                    .decode(s.as_bytes())
                    .map_err(|e| TqlError::MutatorError(format!("Base64 decode error: {}", e)))?;

                let decoded_str = String::from_utf8(decoded_bytes)
                    .map_err(|e| TqlError::MutatorError(format!("UTF-8 decode error: {}", e)))?;

                Ok(JsonValue::String(decoded_str))
            }
            JsonValue::Array(arr) => {
                let transformed: Result<Vec<JsonValue>> = arr
                    .iter()
                    .map(|item| {
                        if let JsonValue::String(s) = item {
                            let decoded_bytes = general_purpose::STANDARD
                                .decode(s.as_bytes())
                                .map_err(|e| {
                                    TqlError::MutatorError(format!("Base64 decode error: {}", e))
                                })?;

                            let decoded_str = String::from_utf8(decoded_bytes).map_err(|e| {
                                TqlError::MutatorError(format!("UTF-8 decode error: {}", e))
                            })?;

                            Ok(JsonValue::String(decoded_str))
                        } else {
                            Ok(item.clone())
                        }
                    })
                    .collect();
                Ok(JsonValue::Array(transformed?))
            }
            _ => Ok(value.clone()),
        }
    }

    fn name(&self) -> &str {
        "b64decode"
    }
}

/// Mutator that decodes URL-encoded string values
pub struct URLDecodeMutator {
    _params: MutatorParams,
}

impl URLDecodeMutator {
    pub fn new(params: MutatorParams) -> Self {
        Self { _params: params }
    }
}

impl Mutator for URLDecodeMutator {
    fn apply(
        &self,
        _field_name: &str,
        _record: &JsonValue,
        value: &JsonValue,
    ) -> Result<JsonValue> {
        match value {
            JsonValue::String(s) => {
                // Use percent_decode for URL decoding
                let decoded = percent_encoding::percent_decode_str(s)
                    .decode_utf8()
                    .map_err(|e| TqlError::MutatorError(format!("URL decode error: {}", e)))?
                    .to_string();
                Ok(JsonValue::String(decoded))
            }
            JsonValue::Array(arr) => {
                let transformed: Result<Vec<JsonValue>> = arr
                    .iter()
                    .map(|item| {
                        if let JsonValue::String(s) = item {
                            let decoded = percent_encoding::percent_decode_str(s)
                                .decode_utf8()
                                .map_err(|e| {
                                    TqlError::MutatorError(format!("URL decode error: {}", e))
                                })?
                                .to_string();
                            Ok(JsonValue::String(decoded))
                        } else {
                            Ok(item.clone())
                        }
                    })
                    .collect();
                Ok(JsonValue::Array(transformed?))
            }
            _ => Ok(value.clone()),
        }
    }

    fn name(&self) -> &str {
        "urldecode"
    }
}

/// Mutator that encodes string values to hexadecimal
pub struct HexEncodeMutator {
    _params: MutatorParams,
}

impl HexEncodeMutator {
    pub fn new(params: MutatorParams) -> Self {
        Self { _params: params }
    }
}

impl Mutator for HexEncodeMutator {
    fn apply(
        &self,
        _field_name: &str,
        _record: &JsonValue,
        value: &JsonValue,
    ) -> Result<JsonValue> {
        match value {
            JsonValue::String(s) => {
                let encoded = hex::encode(s.as_bytes());
                Ok(JsonValue::String(encoded))
            }
            JsonValue::Array(arr) => {
                let transformed: Vec<JsonValue> = arr
                    .iter()
                    .map(|item| {
                        if let JsonValue::String(s) = item {
                            JsonValue::String(hex::encode(s.as_bytes()))
                        } else {
                            item.clone()
                        }
                    })
                    .collect();
                Ok(JsonValue::Array(transformed))
            }
            _ => Ok(value.clone()),
        }
    }

    fn name(&self) -> &str {
        "hexencode"
    }
}

/// Mutator that decodes hexadecimal string values
pub struct HexDecodeMutator {
    _params: MutatorParams,
}

impl HexDecodeMutator {
    pub fn new(params: MutatorParams) -> Self {
        Self { _params: params }
    }
}

impl Mutator for HexDecodeMutator {
    fn apply(
        &self,
        _field_name: &str,
        _record: &JsonValue,
        value: &JsonValue,
    ) -> Result<JsonValue> {
        match value {
            JsonValue::String(s) => {
                let decoded_bytes = hex::decode(s)
                    .map_err(|e| TqlError::MutatorError(format!("Hex decode error: {}", e)))?;

                let decoded_str = String::from_utf8(decoded_bytes)
                    .map_err(|e| TqlError::MutatorError(format!("UTF-8 decode error: {}", e)))?;

                Ok(JsonValue::String(decoded_str))
            }
            JsonValue::Array(arr) => {
                let transformed: Result<Vec<JsonValue>> = arr
                    .iter()
                    .map(|item| {
                        if let JsonValue::String(s) = item {
                            let decoded_bytes = hex::decode(s).map_err(|e| {
                                TqlError::MutatorError(format!("Hex decode error: {}", e))
                            })?;

                            let decoded_str = String::from_utf8(decoded_bytes).map_err(|e| {
                                TqlError::MutatorError(format!("UTF-8 decode error: {}", e))
                            })?;

                            Ok(JsonValue::String(decoded_str))
                        } else {
                            Ok(item.clone())
                        }
                    })
                    .collect();
                Ok(JsonValue::Array(transformed?))
            }
            _ => Ok(value.clone()),
        }
    }

    fn name(&self) -> &str {
        "hexdecode"
    }
}

/// Mutator that calculates MD5 hash of string values
pub struct MD5Mutator {
    _params: MutatorParams,
}

impl MD5Mutator {
    pub fn new(params: MutatorParams) -> Self {
        Self { _params: params }
    }
}

impl Mutator for MD5Mutator {
    fn apply(
        &self,
        _field_name: &str,
        _record: &JsonValue,
        value: &JsonValue,
    ) -> Result<JsonValue> {
        match value {
            JsonValue::String(s) => {
                let digest = md5::compute(s.as_bytes());
                Ok(JsonValue::String(format!("{:x}", digest)))
            }
            JsonValue::Array(arr) => {
                let transformed: Vec<JsonValue> = arr
                    .iter()
                    .map(|item| {
                        if let JsonValue::String(s) = item {
                            let digest = md5::compute(s.as_bytes());
                            JsonValue::String(format!("{:x}", digest))
                        } else {
                            item.clone()
                        }
                    })
                    .collect();
                Ok(JsonValue::Array(transformed))
            }
            _ => Ok(value.clone()),
        }
    }

    fn name(&self) -> &str {
        "md5"
    }
}

/// Mutator that calculates SHA256 hash of string values
pub struct SHA256Mutator {
    _params: MutatorParams,
}

impl SHA256Mutator {
    pub fn new(params: MutatorParams) -> Self {
        Self { _params: params }
    }
}

impl Mutator for SHA256Mutator {
    fn apply(
        &self,
        _field_name: &str,
        _record: &JsonValue,
        value: &JsonValue,
    ) -> Result<JsonValue> {
        use sha2::{Digest, Sha256};

        match value {
            JsonValue::String(s) => {
                let mut hasher = Sha256::new();
                hasher.update(s.as_bytes());
                let result = hasher.finalize();
                Ok(JsonValue::String(format!("{:x}", result)))
            }
            JsonValue::Array(arr) => {
                let transformed: Vec<JsonValue> = arr
                    .iter()
                    .map(|item| {
                        if let JsonValue::String(s) = item {
                            let mut hasher = Sha256::new();
                            hasher.update(s.as_bytes());
                            let result = hasher.finalize();
                            JsonValue::String(format!("{:x}", result))
                        } else {
                            item.clone()
                        }
                    })
                    .collect();
                Ok(JsonValue::Array(transformed))
            }
            _ => Ok(value.clone()),
        }
    }

    fn name(&self) -> &str {
        "sha256"
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    use std::collections::HashMap;

    #[test]
    fn test_base64_encode_mutator() {
        let mutator = Base64EncodeMutator::new(HashMap::new());
        let record = json!({});

        // Test string encoding
        let value = json!("hello world");
        let result = mutator.apply("field", &record, &value).unwrap();
        assert_eq!(result, json!("aGVsbG8gd29ybGQ="));

        // Test array encoding
        let value = json!(["hello", "world"]);
        let result = mutator.apply("field", &record, &value).unwrap();
        assert_eq!(result, json!(["aGVsbG8=", "d29ybGQ="]));
    }

    #[test]
    fn test_base64_decode_mutator() {
        let mutator = Base64DecodeMutator::new(HashMap::new());
        let record = json!({});

        // Test string decoding
        let value = json!("aGVsbG8gd29ybGQ=");
        let result = mutator.apply("field", &record, &value).unwrap();
        assert_eq!(result, json!("hello world"));

        // Test array decoding
        let value = json!(["aGVsbG8=", "d29ybGQ="]);
        let result = mutator.apply("field", &record, &value).unwrap();
        assert_eq!(result, json!(["hello", "world"]));
    }

    #[test]
    fn test_base64_encode_decode_round_trip() {
        let encode_mutator = Base64EncodeMutator::new(HashMap::new());
        let decode_mutator = Base64DecodeMutator::new(HashMap::new());
        let record = json!({});

        let original = json!("test string 123");
        let encoded = encode_mutator.apply("field", &record, &original).unwrap();
        let decoded = decode_mutator.apply("field", &record, &encoded).unwrap();
        assert_eq!(decoded, original);
    }

    #[test]
    fn test_base64_decode_invalid() {
        let mutator = Base64DecodeMutator::new(HashMap::new());
        let record = json!({});

        let value = json!("invalid!!!base64");
        let result = mutator.apply("field", &record, &value);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Base64 decode error"));
    }

    #[test]
    fn test_url_decode_mutator() {
        let mutator = URLDecodeMutator::new(HashMap::new());
        let record = json!({});

        // Test simple URL decoding
        let value = json!("hello%20world");
        let result = mutator.apply("field", &record, &value).unwrap();
        assert_eq!(result, json!("hello world"));

        // Test special characters
        let value = json!("test%2Fpath%3Fquery%3Dvalue");
        let result = mutator.apply("field", &record, &value).unwrap();
        assert_eq!(result, json!("test/path?query=value"));

        // Test array decoding
        let value = json!(["hello%20world", "test%2Fpath"]);
        let result = mutator.apply("field", &record, &value).unwrap();
        assert_eq!(result, json!(["hello world", "test/path"]));
    }

    #[test]
    fn test_encoding_mutators_with_non_strings() {
        let b64_encode = Base64EncodeMutator::new(HashMap::new());
        let url_decode = URLDecodeMutator::new(HashMap::new());
        let record = json!({});

        // Numbers should pass through unchanged
        let value = json!(42);
        assert_eq!(
            b64_encode.apply("field", &record, &value).unwrap(),
            json!(42)
        );
        assert_eq!(
            url_decode.apply("field", &record, &value).unwrap(),
            json!(42)
        );

        // Booleans should pass through unchanged
        let value = json!(true);
        assert_eq!(
            b64_encode.apply("field", &record, &value).unwrap(),
            json!(true)
        );
        assert_eq!(
            url_decode.apply("field", &record, &value).unwrap(),
            json!(true)
        );
    }

    #[test]
    fn test_hex_encode_mutator() {
        let mutator = HexEncodeMutator::new(HashMap::new());
        let record = json!({});

        let value = json!("hello");
        let result = mutator.apply("field", &record, &value).unwrap();
        assert_eq!(result, json!("68656c6c6f"));
    }

    #[test]
    fn test_hex_decode_mutator() {
        let mutator = HexDecodeMutator::new(HashMap::new());
        let record = json!({});

        let value = json!("68656c6c6f");
        let result = mutator.apply("field", &record, &value).unwrap();
        assert_eq!(result, json!("hello"));
    }

    #[test]
    fn test_md5_mutator() {
        let mutator = MD5Mutator::new(HashMap::new());
        let record = json!({});

        let value = json!("hello");
        let result = mutator.apply("field", &record, &value).unwrap();
        assert_eq!(result, json!("5d41402abc4b2a76b9719d911017c592"));
    }

    #[test]
    fn test_sha256_mutator() {
        let mutator = SHA256Mutator::new(HashMap::new());
        let record = json!({});

        let value = json!("hello");
        let result = mutator.apply("field", &record, &value).unwrap();
        assert_eq!(
            result,
            json!("2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824")
        );
    }
}