kelora 1.5.0

A command-line log analysis tool with embedded Rhai scripting
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
use rhai::{Array, Dynamic, Engine, ImmutableString, Map};

/// Safe equality check for a path
/// Usage: path_equals(e, "user.role", "admin")
pub fn path_equals(event: Map, path: ImmutableString, expected: Dynamic) -> bool {
    let path_str = path.as_str();
    let parts: Vec<&str> = path_str.split('.').collect();

    let mut current_map = event;

    for (i, part) in parts.iter().enumerate() {
        if let Some(value) = current_map.get(*part).cloned() {
            if i == parts.len() - 1 {
                // Last part - compare with expected
                return value.type_name() == expected.type_name()
                    && value.to_string() == expected.to_string();
            } else {
                // Intermediate part - must be a map to continue
                if let Some(nested_map) = value.read_lock::<Map>() {
                    current_map = nested_map.clone();
                } else {
                    return false;
                }
            }
        } else {
            return false;
        }
    }

    false
}

/// Convert value to integer (strict - returns () on error)
/// Usage: to_int(e.amount)
pub fn to_int_strict(value: Dynamic) -> Dynamic {
    // Already an integer
    if let Ok(num) = value.as_int() {
        return Dynamic::from(num);
    }

    // Try to parse string as integer
    if let Some(s) = value.read_lock::<ImmutableString>() {
        if let Ok(num) = s.parse::<i64>() {
            return Dynamic::from(num);
        }
    }

    // Return UNIT if conversion failed
    Dynamic::UNIT
}

/// Convert value to integer with default fallback
/// Usage: to_int_or(e.amount, 0)
pub fn to_int_or(value: Dynamic, default: Dynamic) -> Dynamic {
    // Already an integer
    if let Ok(num) = value.as_int() {
        return Dynamic::from(num);
    }

    // Try to parse string as integer
    if let Some(s) = value.read_lock::<ImmutableString>() {
        if let Ok(num) = s.parse::<i64>() {
            return Dynamic::from(num);
        }
    }

    // Return default if conversion failed
    default
}

/// Convert value to float (strict - returns () on error)
/// Usage: to_float(e.amount)
pub fn to_float_strict(value: Dynamic) -> Dynamic {
    // Already a float
    if let Ok(num) = value.as_float() {
        return Dynamic::from(num);
    }

    // Try to convert integer to float
    if let Ok(num) = value.as_int() {
        return Dynamic::from(num as f64);
    }

    // Try to parse string as float
    if let Some(s) = value.read_lock::<ImmutableString>() {
        if let Ok(num) = s.parse::<f64>() {
            return Dynamic::from(num);
        }
    }

    // Return UNIT if conversion failed
    Dynamic::UNIT
}

/// Convert value to float with default fallback
/// Usage: to_float_or(e.amount, 0.0)
pub fn to_float_or(value: Dynamic, default: Dynamic) -> Dynamic {
    // Already a float
    if let Ok(num) = value.as_float() {
        return Dynamic::from(num);
    }

    // Try to convert integer to float
    if let Ok(num) = value.as_int() {
        return Dynamic::from(num as f64);
    }

    // Try to parse string as float
    if let Some(s) = value.read_lock::<ImmutableString>() {
        if let Ok(num) = s.parse::<f64>() {
            return Dynamic::from(num);
        }
    }

    // Return default if conversion failed
    default
}

/// Convert value to integer with explicit thousands separator
/// Usage: to_int(',') for US format with comma thousands separator
pub fn to_int_with_format(value: Dynamic, thousands_sep: ImmutableString) -> Dynamic {
    // Try existing conversion first (for already-numeric values)
    if let Ok(num) = value.as_int() {
        return Dynamic::from(num);
    }

    // Clean and parse string
    if let Some(s) = value.read_lock::<ImmutableString>() {
        let cleaned = clean_number_string_int(s.as_str(), thousands_sep.as_str());
        if let Ok(num) = cleaned.parse::<i64>() {
            return Dynamic::from(num);
        }
    }

    Dynamic::UNIT
}

/// Convert value to integer with explicit thousands separator and default fallback
/// Usage: to_int_or(',', 0) for US format with comma thousands separator
pub fn to_int_or_with_format(
    value: Dynamic,
    thousands_sep: ImmutableString,
    default: Dynamic,
) -> Dynamic {
    // Try existing conversion first (for already-numeric values)
    if let Ok(num) = value.as_int() {
        return Dynamic::from(num);
    }

    // Clean and parse string
    if let Some(s) = value.read_lock::<ImmutableString>() {
        let cleaned = clean_number_string_int(s.as_str(), thousands_sep.as_str());
        if let Ok(num) = cleaned.parse::<i64>() {
            return Dynamic::from(num);
        }
    }

    default
}

/// Convert value to float with explicit format
/// Usage: to_float(',', '.') for US format (comma thousands, dot decimal)
pub fn to_float_with_format(
    value: Dynamic,
    thousands_sep: ImmutableString,
    decimal_sep: ImmutableString,
) -> Dynamic {
    // Validate decimal_sep: must be single char or empty
    if decimal_sep.chars().count() > 1 {
        return Dynamic::UNIT;
    }

    // Try existing conversion first (for already-numeric values)
    if let Ok(num) = value.as_float() {
        return Dynamic::from(num);
    }
    if let Ok(num) = value.as_int() {
        return Dynamic::from(num as f64);
    }

    // Clean and parse string
    if let Some(s) = value.read_lock::<ImmutableString>() {
        let cleaned =
            clean_number_string_float(s.as_str(), thousands_sep.as_str(), decimal_sep.as_str());
        if let Ok(num) = cleaned.parse::<f64>() {
            return Dynamic::from(num);
        }
    }

    Dynamic::UNIT
}

/// Convert value to float with explicit format and default fallback
/// Usage: to_float_or(',', '.', 0.0) for US format with default
pub fn to_float_or_with_format(
    value: Dynamic,
    thousands_sep: ImmutableString,
    decimal_sep: ImmutableString,
    default: Dynamic,
) -> Dynamic {
    // Validate decimal_sep: must be single char or empty
    if decimal_sep.chars().count() > 1 {
        return default;
    }

    // Try existing conversion first (for already-numeric values)
    if let Ok(num) = value.as_float() {
        return Dynamic::from(num);
    }
    if let Ok(num) = value.as_int() {
        return Dynamic::from(num as f64);
    }

    // Clean and parse string
    if let Some(s) = value.read_lock::<ImmutableString>() {
        let cleaned =
            clean_number_string_float(s.as_str(), thousands_sep.as_str(), decimal_sep.as_str());
        if let Ok(num) = cleaned.parse::<f64>() {
            return Dynamic::from(num);
        }
    }

    default
}

/// Helper to clean number string for integer parsing
/// Removes any character that appears in thousands_sep
fn clean_number_string_int(s: &str, thousands_sep: &str) -> String {
    if thousands_sep.is_empty() {
        return s.to_string();
    }

    // Remove any character that appears in thousands_sep
    thousands_sep
        .chars()
        .fold(s.to_string(), |acc, c| acc.replace(c, ""))
}

/// Helper to clean number string for float parsing
/// Removes any character that appears in thousands_sep
/// Replaces decimal_sep (must be single char or empty) with '.'
/// Note: thousands removal happens FIRST, then decimal replacement
fn clean_number_string_float(s: &str, thousands_sep: &str, decimal_sep: &str) -> String {
    let mut result = s.to_string();

    // Remove thousands separators FIRST (if not empty)
    // This must happen before decimal replacement to avoid removing the converted decimal
    // Example: "1.234,56" with thousands="." and decimal="," → remove dots → "1234,56" → replace comma → "1234.56"
    if !thousands_sep.is_empty() {
        result = thousands_sep
            .chars()
            .fold(result, |acc, c| acc.replace(c, ""));
    }

    // Then replace decimal separator with standard dot (if not empty and not already '.')
    if !decimal_sep.is_empty() && decimal_sep != "." {
        result = result.replace(decimal_sep, ".");
    }

    result
}

/// Extract value from a nested path with default fallback
/// Usage: get_path(e, "user.role", "guest")
/// Supports dot notation and bracket notation: "user.items[0].name"
pub fn get_path_with_default(event: Map, path: ImmutableString, default: Dynamic) -> Dynamic {
    extract_path_value(&Dynamic::from(event), path.as_str()).unwrap_or(default)
}

/// Extract value from a nested path (returns null if not found)
/// Usage: get_path(e, "user.role")
pub fn get_path(event: Map, path: ImmutableString) -> Dynamic {
    extract_path_value(&Dynamic::from(event), path.as_str()).unwrap_or(Dynamic::UNIT)
}

/// Extract value from a JSON string with default fallback
/// Usage: get_path(json_string, "user.role", "guest")
pub fn get_path_json_with_default(
    json_string: ImmutableString,
    path: ImmutableString,
    default: Dynamic,
) -> Dynamic {
    if let Ok(parsed) = serde_json::from_str::<serde_json::Value>(json_string.as_str()) {
        let dynamic = crate::event::json_to_dynamic(&parsed);
        extract_path_value(&dynamic, path.as_str()).unwrap_or(default)
    } else {
        default
    }
}

/// Extract value from a JSON string (returns null if not found)
/// Usage: get_path(json_string, "user.role")
pub fn get_path_json(json_string: ImmutableString, path: ImmutableString) -> Dynamic {
    if let Ok(parsed) = serde_json::from_str::<serde_json::Value>(json_string.as_str()) {
        let dynamic = crate::event::json_to_dynamic(&parsed);
        extract_path_value(&dynamic, path.as_str()).unwrap_or(Dynamic::UNIT)
    } else {
        Dynamic::UNIT
    }
}

/// Check if a nested path exists
/// Usage: has_path(e, "user.role")
pub fn has_path(event: Map, path: ImmutableString) -> bool {
    extract_path_value(&Dynamic::from(event), path.as_str()).is_some()
}

/// Internal function to extract value from a path string
/// Supports dot notation (user.name) and bracket notation (scores[0], scores[-1])
fn extract_path_value(value: &Dynamic, path: &str) -> Option<Dynamic> {
    let mut current = value.clone();

    // Parse the path into tokens
    let tokens = parse_path_tokens(path);

    for token in tokens {
        match token {
            PathToken::Field(field_name) => {
                let next_value = {
                    let map = current.read_lock::<Map>()?;
                    map.get(field_name.as_str())?.clone()
                };
                current = next_value;
            }
            PathToken::Index(index) => {
                let next_value = {
                    let array = current.read_lock::<Array>()?;
                    let array_len = array.len() as i64;
                    let actual_index = if index < 0 {
                        array_len + index // Negative indexing
                    } else {
                        index
                    };

                    if actual_index >= 0 && (actual_index as usize) < array.len() {
                        array[actual_index as usize].clone()
                    } else {
                        return None;
                    }
                };
                current = next_value;
            }
        }
    }

    Some(current)
}

/// Path token types for parsing
#[derive(Debug, Clone)]
enum PathToken {
    Field(String),
    Index(i64),
}

/// Parse a path string into tokens
/// Examples:
///   "user.name" -> [Field("user"), Field("name")]
///   "scores[0]" -> [Field("scores"), Index(0)]
///   "user.items[-1].name" -> [Field("user"), Field("items"), Index(-1), Field("name")]
fn parse_path_tokens(path: &str) -> Vec<PathToken> {
    let mut tokens = Vec::new();
    let mut current_token = String::new();
    let mut chars = path.chars().peekable();

    while let Some(ch) = chars.next() {
        match ch {
            '.' => {
                if !current_token.is_empty() {
                    tokens.push(PathToken::Field(current_token.clone()));
                    current_token.clear();
                }
            }
            '[' => {
                // Finish current field token if any
                if !current_token.is_empty() {
                    tokens.push(PathToken::Field(current_token.clone()));
                    current_token.clear();
                }

                // Parse the index
                let mut index_str = String::new();
                for inner_ch in chars.by_ref() {
                    if inner_ch == ']' {
                        break;
                    }
                    index_str.push(inner_ch);
                }

                if let Ok(index) = index_str.parse::<i64>() {
                    tokens.push(PathToken::Index(index));
                }
            }
            _ => {
                current_token.push(ch);
            }
        }
    }

    // Add final token if any
    if !current_token.is_empty() {
        tokens.push(PathToken::Field(current_token));
    }

    tokens
}

/// Convert value to boolean (strict - returns () on error)
/// Usage: to_bool(e.active)
pub fn to_bool_strict(value: Dynamic) -> Dynamic {
    // Already a boolean
    if let Ok(b) = value.as_bool() {
        return Dynamic::from(b);
    }

    // String conversion
    if let Some(s) = value.read_lock::<ImmutableString>() {
        let s_lower = s.to_lowercase();
        match s_lower.as_str() {
            "true" | "yes" | "1" | "on" => return Dynamic::from(true),
            "false" | "no" | "0" | "off" => return Dynamic::from(false),
            _ => {}
        }
    }

    // Number conversion (0 = false, non-zero = true)
    if let Ok(num) = value.as_int() {
        return Dynamic::from(num != 0);
    }
    if let Ok(num) = value.as_float() {
        return Dynamic::from(num != 0.0);
    }

    // Return UNIT if conversion failed
    Dynamic::UNIT
}

/// Convert value to boolean with default fallback
/// Usage: to_bool_or(e.active, false)
pub fn to_bool_or(value: Dynamic, default: Dynamic) -> Dynamic {
    // Already a boolean
    if let Ok(b) = value.as_bool() {
        return Dynamic::from(b);
    }

    // String conversion
    if let Some(s) = value.read_lock::<ImmutableString>() {
        let s_lower = s.to_lowercase();
        match s_lower.as_str() {
            "true" | "yes" | "1" | "on" => return Dynamic::from(true),
            "false" | "no" | "0" | "off" => return Dynamic::from(false),
            _ => {}
        }
    }

    // Number conversion (0 = false, non-zero = true)
    if let Ok(num) = value.as_int() {
        return Dynamic::from(num != 0);
    }
    if let Ok(num) = value.as_float() {
        return Dynamic::from(num != 0.0);
    }

    // Return default if conversion failed
    default
}

/// Register safety functions with the Rhai engine
pub fn register_functions(engine: &mut Engine) {
    // Path access functions
    engine.register_fn("get_path", get_path);
    engine.register_fn("get_path", get_path_with_default);
    engine.register_fn("get_path", get_path_json);
    engine.register_fn("get_path", get_path_json_with_default);
    engine.register_fn("has_path", has_path);

    // Other safety functions
    engine.register_fn("path_equals", path_equals);

    // Conversion functions - strict variants (return () on error)
    engine.register_fn("to_int", to_int_strict);
    engine.register_fn("to_float", to_float_strict);
    engine.register_fn("to_bool", to_bool_strict);

    // Conversion functions - with defaults
    engine.register_fn("to_int_or", to_int_or);
    engine.register_fn("to_float_or", to_float_or);
    engine.register_fn("to_bool_or", to_bool_or);

    // Conversion functions - with explicit format (NEW overloads)
    engine.register_fn("to_int", to_int_with_format);
    engine.register_fn("to_int_or", to_int_or_with_format);
    engine.register_fn("to_float", to_float_with_format);
    engine.register_fn("to_float_or", to_float_or_with_format);
}

#[cfg(test)]
mod tests {
    use super::*;
    use rhai::Map;

    fn create_test_event() -> Map {
        let mut event = Map::new();
        event.insert("name".into(), Dynamic::from("alice"));
        event.insert("score".into(), Dynamic::from(85));

        let mut user = Map::new();
        user.insert("role".into(), Dynamic::from("admin"));
        user.insert("active".into(), Dynamic::from(true));

        // Add arrays for testing
        let scores_array = vec![Dynamic::from(100), Dynamic::from(85), Dynamic::from(92)];
        event.insert("scores".into(), Dynamic::from(scores_array));

        // Add nested structure with arrays
        let mut items = Vec::new();
        let mut item1 = Map::new();
        item1.insert("name".into(), Dynamic::from("item1"));
        item1.insert("value".into(), Dynamic::from(42));
        items.push(Dynamic::from(item1));

        let mut item2 = Map::new();
        item2.insert("name".into(), Dynamic::from("item2"));
        item2.insert("value".into(), Dynamic::from(33));
        items.push(Dynamic::from(item2));

        let mut details = Map::new();
        details.insert("items".into(), Dynamic::from(items));
        user.insert("details".into(), Dynamic::from(details));

        event.insert("user".into(), Dynamic::from(user));

        event
    }

    #[test]
    fn test_path_equals() {
        let event = create_test_event();
        assert!(path_equals(
            event.clone(),
            "name".into(),
            Dynamic::from("alice")
        ));
        assert!(path_equals(
            event.clone(),
            "user.role".into(),
            Dynamic::from("admin")
        ));
        assert!(!path_equals(
            event.clone(),
            "name".into(),
            Dynamic::from("bob")
        ));
        assert!(!path_equals(
            event,
            "missing".into(),
            Dynamic::from("anything")
        ));
    }

    #[test]
    fn test_to_int_strict() {
        // Test integer input
        let result = to_int_strict(Dynamic::from(42i64));
        assert_eq!(result.as_int().unwrap(), 42i64);

        // Test string integer input
        let result = to_int_strict(Dynamic::from("123"));
        assert_eq!(result.as_int().unwrap(), 123i64);

        // Test invalid input returns UNIT
        let result = to_int_strict(Dynamic::from("invalid"));
        assert!(result.is_unit());

        // Test float input returns UNIT (strict int conversion)
        let result = to_int_strict(Dynamic::from(12.5));
        assert!(result.is_unit());
    }

    #[test]
    fn test_to_int_or() {
        // Test integer input
        let result = to_int_or(Dynamic::from(42i64), Dynamic::from(0i64));
        assert_eq!(result.as_int().unwrap(), 42i64);

        // Test string integer input
        let result = to_int_or(Dynamic::from("123"), Dynamic::from(0i64));
        assert_eq!(result.as_int().unwrap(), 123i64);

        // Test invalid input with default
        let result = to_int_or(Dynamic::from("invalid"), Dynamic::from(999i64));
        assert_eq!(result.as_int().unwrap(), 999i64);
    }

    #[test]
    fn test_to_float_strict() {
        // Test float input
        let result = to_float_strict(Dynamic::from(std::f64::consts::PI));
        assert_eq!(result.as_float().unwrap(), std::f64::consts::PI);

        // Test integer input (should convert to float)
        let result = to_float_strict(Dynamic::from(42i64));
        assert_eq!(result.as_float().unwrap(), 42.0);

        // Test string float input
        let result = to_float_strict(Dynamic::from("12.5"));
        assert_eq!(result.as_float().unwrap(), 12.5);

        // Test invalid input returns UNIT
        let result = to_float_strict(Dynamic::from("invalid"));
        assert!(result.is_unit());
    }

    #[test]
    fn test_to_float_or() {
        // Test float input
        let result = to_float_or(Dynamic::from(std::f64::consts::PI), Dynamic::from(0.0));
        assert_eq!(result.as_float().unwrap(), std::f64::consts::PI);

        // Test integer input (should convert to float)
        let result = to_float_or(Dynamic::from(42i64), Dynamic::from(0.0));
        assert_eq!(result.as_float().unwrap(), 42.0);

        // Test string float input
        let result = to_float_or(Dynamic::from("12.5"), Dynamic::from(0.0));
        assert_eq!(result.as_float().unwrap(), 12.5);

        // Test invalid input with default
        let result = to_float_or(Dynamic::from("invalid"), Dynamic::from(999.0));
        assert_eq!(result.as_float().unwrap(), 999.0);
    }

    #[test]
    fn test_to_bool_strict() {
        // Test boolean input
        assert!(to_bool_strict(Dynamic::from(true)).as_bool().unwrap());
        assert!(!to_bool_strict(Dynamic::from(false)).as_bool().unwrap());

        // Test string conversion
        assert!(to_bool_strict(Dynamic::from("yes")).as_bool().unwrap());
        assert!(to_bool_strict(Dynamic::from("1")).as_bool().unwrap());
        assert!(!to_bool_strict(Dynamic::from("false")).as_bool().unwrap());

        // Test number conversion
        assert!(to_bool_strict(Dynamic::from(1i64)).as_bool().unwrap());
        assert!(!to_bool_strict(Dynamic::from(0i64)).as_bool().unwrap());

        // Test invalid input returns UNIT
        let result = to_bool_strict(Dynamic::from("invalid"));
        assert!(result.is_unit());
    }

    #[test]
    fn test_to_bool_or() {
        assert!(to_bool_or(Dynamic::from(true), Dynamic::from(false))
            .as_bool()
            .unwrap());
        assert!(to_bool_or(Dynamic::from("yes"), Dynamic::from(false))
            .as_bool()
            .unwrap());
        assert!(to_bool_or(Dynamic::from("1"), Dynamic::from(false))
            .as_bool()
            .unwrap());
        assert!(!to_bool_or(Dynamic::from("false"), Dynamic::from(true))
            .as_bool()
            .unwrap());
        assert!(to_bool_or(Dynamic::from(1i64), Dynamic::from(false))
            .as_bool()
            .unwrap());
        assert!(!to_bool_or(Dynamic::from(0i64), Dynamic::from(true))
            .as_bool()
            .unwrap());
        assert!(to_bool_or(Dynamic::from("invalid"), Dynamic::from(true))
            .as_bool()
            .unwrap());
    }

    #[test]
    fn test_get_path() {
        let event = create_test_event();

        // Test simple field access
        let name = get_path(event.clone(), "name".into());
        assert_eq!(name.cast::<String>(), "alice");

        // Test nested field access
        let role = get_path(event.clone(), "user.role".into());
        assert_eq!(role.cast::<String>(), "admin");

        // Test missing field returns UNIT
        let missing = get_path(event.clone(), "missing".into());
        assert!(missing.is_unit());

        // Test missing nested field returns UNIT
        let missing_nested = get_path(event, "user.missing".into());
        assert!(missing_nested.is_unit());
    }

    #[test]
    fn test_get_path_with_default() {
        let event = create_test_event();

        // Test simple field access
        let name = get_path_with_default(event.clone(), "name".into(), Dynamic::from("default"));
        assert_eq!(name.cast::<String>(), "alice");

        // Test nested field access
        let role = get_path_with_default(event.clone(), "user.role".into(), Dynamic::from("guest"));
        assert_eq!(role.cast::<String>(), "admin");

        // Test missing field returns default
        let missing =
            get_path_with_default(event.clone(), "missing".into(), Dynamic::from("default"));
        assert_eq!(missing.cast::<String>(), "default");

        // Test missing nested field returns default
        let missing_nested =
            get_path_with_default(event, "user.missing".into(), Dynamic::from("guest"));
        assert_eq!(missing_nested.cast::<String>(), "guest");
    }

    #[test]
    fn test_get_path_array_access() {
        let event = create_test_event();

        // Test array index access
        let first_score = get_path(event.clone(), "scores[0]".into());
        // Handle both i32 and i64 integer types
        let first_val = if let Ok(val) = first_score.as_int() {
            val
        } else {
            first_score.cast::<i32>() as i64
        };
        assert_eq!(first_val, 100);

        let second_score = get_path(event.clone(), "scores[1]".into());
        let second_val = if let Ok(val) = second_score.as_int() {
            val
        } else {
            second_score.cast::<i32>() as i64
        };
        assert_eq!(second_val, 85);

        // Test negative indexing
        let last_score = get_path(event.clone(), "scores[-1]".into());
        let last_val = if let Ok(val) = last_score.as_int() {
            val
        } else {
            last_score.cast::<i32>() as i64
        };
        assert_eq!(last_val, 92);

        // Test out of bounds returns UNIT
        let out_of_bounds = get_path(event, "scores[10]".into());
        assert!(out_of_bounds.is_unit());
    }

    #[test]
    fn test_get_path_nested_array_access() {
        let event = create_test_event();

        // Test nested array access
        let item_name = get_path(event.clone(), "user.details.items[0].name".into());
        assert_eq!(item_name.cast::<String>(), "item1");

        let item_value = get_path(event.clone(), "user.details.items[1].value".into());
        let item_val = if let Ok(val) = item_value.as_int() {
            val
        } else {
            item_value.cast::<i32>() as i64
        };
        assert_eq!(item_val, 33);

        // Test with default for missing nested array item
        let missing_item = get_path_with_default(
            event,
            "user.details.items[10].name".into(),
            Dynamic::from("unknown"),
        );
        assert_eq!(missing_item.cast::<String>(), "unknown");
    }

    #[test]
    fn test_has_path() {
        let event = create_test_event();

        // Test existing paths
        assert!(has_path(event.clone(), "name".into()));
        assert!(has_path(event.clone(), "user.role".into()));
        assert!(has_path(event.clone(), "scores[0]".into()));
        assert!(has_path(event.clone(), "user.details.items[0].name".into()));

        // Test missing paths
        assert!(!has_path(event.clone(), "missing".into()));
        assert!(!has_path(event.clone(), "user.missing".into()));
        assert!(!has_path(event.clone(), "scores[10]".into()));
        assert!(!has_path(event, "user.details.items[10].name".into()));
    }

    #[test]
    fn test_get_path_json() {
        let json_string: ImmutableString =
            r#"{"user": {"role": "admin"}, "scores": [100, 85, 92]}"#.into();

        // Test JSON parsing with path access
        let role = get_path_json(json_string.clone(), "user.role".into());
        assert_eq!(role.cast::<String>(), "admin");

        // Test with default for missing path
        let missing =
            get_path_json_with_default(json_string, "missing".into(), Dynamic::from("default"));
        assert_eq!(missing.cast::<String>(), "default");
    }

    #[test]
    fn test_get_path_json_invalid() {
        let invalid_json: ImmutableString = "invalid json".into();

        // Test invalid JSON returns UNIT
        let result = get_path_json(invalid_json.clone(), "user.role".into());
        assert!(result.is_unit());

        // Test invalid JSON returns default
        let result_with_default =
            get_path_json_with_default(invalid_json, "user.role".into(), Dynamic::from("default"));
        assert_eq!(result_with_default.cast::<String>(), "default");
    }

    #[test]
    fn test_parse_path_tokens() {
        // Test simple field
        let tokens = parse_path_tokens("name");
        assert_eq!(tokens.len(), 1);
        match &tokens[0] {
            PathToken::Field(field) => assert_eq!(field, "name"),
            _ => panic!("Expected field token"),
        }

        // Test nested fields
        let tokens = parse_path_tokens("user.role");
        assert_eq!(tokens.len(), 2);
        match (&tokens[0], &tokens[1]) {
            (PathToken::Field(f1), PathToken::Field(f2)) => {
                assert_eq!(f1, "user");
                assert_eq!(f2, "role");
            }
            _ => panic!("Expected field tokens"),
        }

        // Test array access
        let tokens = parse_path_tokens("scores[0]");
        assert_eq!(tokens.len(), 2);
        match (&tokens[0], &tokens[1]) {
            (PathToken::Field(field), PathToken::Index(index)) => {
                assert_eq!(field, "scores");
                assert_eq!(*index, 0);
            }
            _ => panic!("Expected field and index tokens"),
        }

        // Test negative array access
        let tokens = parse_path_tokens("scores[-1]");
        assert_eq!(tokens.len(), 2);
        match &tokens[1] {
            PathToken::Index(index) => assert_eq!(*index, -1),
            _ => panic!("Expected negative index token"),
        }

        // Test complex path
        let tokens = parse_path_tokens("user.details.items[1].name");
        assert_eq!(tokens.len(), 5);
        match (&tokens[0], &tokens[1], &tokens[2], &tokens[3], &tokens[4]) {
            (
                PathToken::Field(f1),
                PathToken::Field(f2),
                PathToken::Field(f3),
                PathToken::Index(index),
                PathToken::Field(f4),
            ) => {
                assert_eq!(f1, "user");
                assert_eq!(f2, "details");
                assert_eq!(f3, "items");
                assert_eq!(*index, 1);
                assert_eq!(f4, "name");
            }
            _ => panic!("Expected complex path tokens"),
        }
    }

    #[test]
    fn test_to_int_with_multi_char_thousands_sep() {
        // Test removing any character in thousands_sep string
        let result = to_int_with_format(Dynamic::from("1,234'567"), ImmutableString::from(",'"));
        assert_eq!(result.as_int().unwrap(), 1234567);

        // Test with multiple separator chars (space and comma)
        let result = to_int_with_format(Dynamic::from("1,234 567"), ImmutableString::from(", "));
        assert_eq!(result.as_int().unwrap(), 1234567);

        // Test with underscore and dash
        let result = to_int_with_format(Dynamic::from("1_234-567"), ImmutableString::from("_-"));
        assert_eq!(result.as_int().unwrap(), 1234567);
    }

    #[test]
    fn test_to_float_with_multi_char_thousands_sep() {
        // Test removing any character in thousands_sep string (comma and apostrophe)
        let result = to_float_with_format(
            Dynamic::from("1,234'567.89"),
            ImmutableString::from(",'"),
            ImmutableString::from("."),
        );
        assert!((result.as_float().unwrap() - 1234567.89).abs() < 0.001);

        // Test with space and apostrophe for thousands, comma for decimal
        // Note: Can't use dot in thousands_sep when comma is decimal (they'd conflict)
        let result = to_float_with_format(
            Dynamic::from("1'234 567,89"),
            ImmutableString::from("' "),
            ImmutableString::from(","),
        );
        assert!((result.as_float().unwrap() - 1234567.89).abs() < 0.001);

        // Test with underscore and dash for thousands
        let result = to_float_with_format(
            Dynamic::from("1_234-567.89"),
            ImmutableString::from("_-"),
            ImmutableString::from("."),
        );
        assert!((result.as_float().unwrap() - 1234567.89).abs() < 0.001);
    }

    #[test]
    fn test_to_float_rejects_multi_char_decimal_sep() {
        // Multi-char decimal_sep should return UNIT
        let result = to_float_with_format(
            Dynamic::from("1234.56"),
            ImmutableString::from(","),
            ImmutableString::from(".,"),
        );
        assert!(result.is_unit());

        // Multi-char decimal_sep with default should return default
        let result = to_float_or_with_format(
            Dynamic::from("1234.56"),
            ImmutableString::from(","),
            ImmutableString::from(".,"),
            Dynamic::from(999.0),
        );
        assert_eq!(result.as_float().unwrap(), 999.0);
    }

    #[test]
    fn test_to_float_processing_order_eu_format() {
        // Critical test: EU format where decimal char appears in thousands_sep
        // This verifies that thousands removal happens BEFORE decimal replacement
        // "1.234,56" with thousands="." and decimal=","
        // Correct order: remove dots → "1234,56" → replace comma → "1234.56"
        // Wrong order: replace comma → "1.234.56" → remove dots → "123456" (WRONG!)
        let result = to_float_with_format(
            Dynamic::from("1.234,56"),
            ImmutableString::from("."),
            ImmutableString::from(","),
        );
        assert!((result.as_float().unwrap() - 1234.56).abs() < 0.001);

        // Test with larger EU number
        let result = to_float_with_format(
            Dynamic::from("1.234.567,89"),
            ImmutableString::from("."),
            ImmutableString::from(","),
        );
        assert!((result.as_float().unwrap() - 1234567.89).abs() < 0.001);
    }

    #[test]
    fn test_to_int_or_with_multi_char_thousands_sep() {
        // Test with default fallback
        let result = to_int_or_with_format(
            Dynamic::from("1,234'567"),
            ImmutableString::from(",'"),
            Dynamic::from(0i64),
        );
        assert_eq!(result.as_int().unwrap(), 1234567);

        // Test invalid input with default
        let result = to_int_or_with_format(
            Dynamic::from("invalid"),
            ImmutableString::from(",'"),
            Dynamic::from(999i64),
        );
        assert_eq!(result.as_int().unwrap(), 999);
    }

    #[test]
    fn test_to_float_or_with_multi_char_thousands_sep() {
        // Test with default fallback
        let result = to_float_or_with_format(
            Dynamic::from("1,234'567.89"),
            ImmutableString::from(",'"),
            ImmutableString::from("."),
            Dynamic::from(0.0),
        );
        assert!((result.as_float().unwrap() - 1234567.89).abs() < 0.001);

        // Test invalid input with default
        let result = to_float_or_with_format(
            Dynamic::from("invalid"),
            ImmutableString::from(",'"),
            ImmutableString::from("."),
            Dynamic::from(999.0),
        );
        assert_eq!(result.as_float().unwrap(), 999.0);
    }
}