faker-rust 0.1.0

A high-performance, locale-aware fake data generator for Rust
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
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
//! Locale data loader - loads and provides access to locale YAML files
#![allow(dead_code)]
#![allow(unused_variables)]
#![allow(clippy::collapsible_match)]
#![allow(clippy::manual_strip)]
use once_cell::sync::Lazy;
use serde_yaml::Value;
use std::collections::HashMap;
use std::env;
use std::fs;
use std::path::PathBuf;

fn get_locales_path() -> PathBuf {
    let exe_dir = env::current_exe()
        .ok()
        .and_then(|p| p.parent().map(|p| p.to_path_buf()))
        .unwrap_or_else(|| PathBuf::from("."));

    let possible_paths = vec![
        exe_dir.join("locales"),
        exe_dir.join("..").join("locales"),
        PathBuf::from("locales"),
        PathBuf::from("..").join("locales"),
    ];

    for path in possible_paths {
        if path.exists() && path.is_dir() {
            return path;
        }
    }

    PathBuf::from("locales")
}

static LOCALE_DATA: Lazy<HashMap<String, Value>> = Lazy::new(load_locale_data);

fn load_locale_data() -> HashMap<String, Value> {
    let mut result: HashMap<String, Value> = HashMap::new();
    let locales_path = get_locales_path();

    if !locales_path.exists() {
        return result;
    }

    if let Ok(entries) = fs::read_dir(&locales_path) {
        for entry in entries.flatten() {
            let path = entry.path();

            if path.is_file() && path.extension().is_some_and(|ext| ext == "yml") {
                if let Some(filename) = path.file_stem() {
                    let locale_name = filename.to_string_lossy().to_string();
                    if let Ok(contents) = fs::read_to_string(&path) {
                        if let Ok(yaml) = serde_yaml::from_str::<Value>(&contents) {
                            result.insert(locale_name, yaml);
                        }
                    }
                }
            }

            if path.is_dir() {
                let dir_name = path
                    .file_name()
                    .map(|s| s.to_string_lossy().to_string())
                    .unwrap_or_default();

                if let Ok(sub_entries) = fs::read_dir(&path) {
                    for sub_entry in sub_entries.flatten() {
                        let sub_path = sub_entry.path();
                        if sub_path.is_file()
                            && sub_path.extension().is_some_and(|ext| ext == "yml")
                        {
                            let file_stem = sub_path
                                .file_stem()
                                .map(|s| s.to_string_lossy().to_string())
                                .unwrap_or_default();

                            let locale_name = format!("{}_{}", dir_name, file_stem);
                            if let Ok(contents) = fs::read_to_string(&sub_path) {
                                if let Ok(yaml) = serde_yaml::from_str::<Value>(&contents) {
                                    result.insert(locale_name, yaml);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    result
}

/// Sample from locale data and resolve any placeholders in the result
/// Sample from locale data and resolve any placeholders in the result
pub fn sample_with_resolve(v: &[String], context_category: Option<&str>) -> String {
    if v.is_empty() {
        return String::new();
    }
    use crate::config::FakerConfig;
    let config = FakerConfig::current();
    let idx = config.rand_usize(v.len());
    let selected = &v[idx];
    resolve_placeholder(selected, context_category)
}

pub fn fetch_locale(key: &str, locale: &str) -> Option<Vec<String>> {
    fetch_locale_with_context(key, locale, None)
}

fn fetch_locale_no_resolve(
    key: &str,
    locale: &str,
    context_category: Option<&str>,
) -> Option<Vec<String>> {
    let locales_to_try: Vec<String> = if locale.contains('_') || locale.contains('-') {
        let short = locale.split(['-', '_']).next().unwrap();
        vec![locale.to_string(), short.to_string()]
    } else {
        vec![locale.to_string()]
    };

    for loc in locales_to_try.iter() {
        // If we have context_category, try the subdir file directly with simple key
        if let Some(cat) = context_category {
            let subdir_key = format!("{}_{}", loc, cat);
            if let Some(locale_data) = LOCALE_DATA.get(&subdir_key) {
                // Try with just the key directly
                if let Some(result) = try_extract(locale_data, loc, key, None) {
                    return Some(result);
                }
                // Try with faker prefix
                let faker_key = format!("faker.{}.{}", cat, key);
                if let Some(result) = try_extract(locale_data, loc, &faker_key, None) {
                    return Some(result);
                }
            }
        }
    }

    // Fallback to original fetch_locale_with_context for other cases
    fetch_locale_with_context(key, locale, context_category)
}

pub fn fetch_locale_with_context(
    key: &str,
    locale: &str,
    context_category: Option<&str>,
) -> Option<Vec<String>> {
    let locales_to_try: Vec<String> = if locale.contains('_') || locale.contains('-') {
        let short = locale.split(['-', '_']).next().unwrap();
        vec![locale.to_string(), short.to_string()]
    } else {
        vec![locale.to_string()]
    };

    for loc in locales_to_try.iter() {
        // 1. Try full locale file (en.yml)
        if let Some(locale_data) = LOCALE_DATA.get(loc) {
            if let Some(result) = try_extract(locale_data, loc, key, context_category) {
                return Some(result);
            }
        }

        // 2. Try subdirectory files (en_name.yml, en_address.yml, etc.)
        let parts: Vec<&str> = key.split('.').collect();
        if parts.len() >= 2 {
            let category = parts[0];
            let subdir_key = format!("{}_{}", loc, category);
            if let Some(locale_data) = LOCALE_DATA.get(&subdir_key) {
                if let Some(result) = try_extract(locale_data, loc, key, context_category) {
                    return Some(result);
                }
                let faker_key = format!("faker.{}", key);
                if let Some(result) = try_extract(locale_data, loc, &faker_key, context_category) {
                    return Some(result);
                }
                if let Some(result) = try_extract(
                    locale_data,
                    loc,
                    parts.last().unwrap_or(&key),
                    context_category,
                ) {
                    return Some(result);
                }
            }
        }

        // 3. NEW: If we have context_category and key is simple (like "city_prefix")
        // Try using the context_category as subdir directly
        if let Some(cat) = context_category {
            let subdir_key = format!("{}_{}", loc, cat);
            if let Some(locale_data) = LOCALE_DATA.get(&subdir_key) {
                // Key is just the simple field name
                if let Some(result) = try_extract(locale_data, loc, key, context_category) {
                    return Some(result);
                }
                // Try with faker prefix
                let faker_key = format!("faker.{}.{}", cat, key);
                if let Some(result) = try_extract(locale_data, loc, &faker_key, context_category) {
                    return Some(result);
                }
            }
        }

        // 4. Try fallback subdir with second part of key
        let subdir_key = format!("{}_{}", loc, key.split('.').nth(1).unwrap_or("name"));
        if let Some(locale_data) = LOCALE_DATA.get(&subdir_key) {
            // Try with original key
            if let Some(result) = try_extract(locale_data, loc, key, context_category) {
                return Some(result);
            }
            // Also try with just the last part
            if let Some(result) = try_extract(
                locale_data,
                loc,
                parts.last().unwrap_or(&key),
                context_category,
            ) {
                return Some(result);
            }
            // Also try with category prefix
            if parts.len() >= 2 {
                let category = parts[0];
                let full_key = format!("{}.{}", category, parts.last().unwrap_or(&key));
                if let Some(result) = try_extract(locale_data, loc, &full_key, context_category) {
                    return Some(result);
                }
            }
        }

        // 4. For simple keys without category (e.g., "city_prefix" with context "address")
        // Try to find the subdir directly
        if let Some(cat) = context_category {
            let subdir_key = format!("{}_{}", loc, cat);
            if let Some(locale_data) = LOCALE_DATA.get(&subdir_key) {
                // Try simple field name
                if let Some(result) = try_extract(locale_data, loc, key, context_category) {
                    return Some(result);
                }
                // Try with faker prefix
                let faker_key = format!("faker.{}.{}", cat, key);
                if let Some(result) = try_extract(locale_data, loc, &faker_key, context_category) {
                    return Some(result);
                }
            }
        }
    }

    if locale != "en" {
        return fetch_locale_with_context(key, "en", context_category);
    }

    None
}

fn try_extract(
    locale_data: &Value,
    locale: &str,
    key: &str,
    _context_category: Option<&str>,
) -> Option<Vec<String>> {
    if let Value::Mapping(top) = locale_data {
        // Case 1: en.faker.name.first_name (file starts with locale name)
        if let Some(inner) = top.get(Value::String(locale.to_string())) {
            if let Value::Mapping(inner_map) = inner {
                if let Some(faker) = inner_map.get(Value::String("faker".to_string())) {
                    let clean_key = clean_key_for_extraction(key);
                    return extract_nested_array_raw(faker, &clean_key);
                }
            }
        }

        // Case 2: { "faker": {...} } (subdirectory files like en_name.yml)
        if let Some(faker) = top.get(Value::String("faker".to_string())) {
            let clean_key = clean_key_for_extraction(key);
            return extract_nested_array_raw(faker, &clean_key);
        }

        // Case 3: en.faker.name.first_name -> try without faker prefix
        if let Some(inner) = top.get(Value::String(locale.to_string())) {
            if let Value::Mapping(inner_map) = inner {
                let parts: Vec<&str> = key.split('.').collect();
                if parts.len() >= 2 {
                    let category = parts[0].to_lowercase();
                    if let Some(cat_data) = inner_map.get(Value::String(category.clone())) {
                        if let Value::Mapping(cat_map) = cat_data {
                            let field = if parts.len() > 1 { parts[1] } else { "" };
                            if let Some(field_data) = cat_map.get(Value::String(field.to_string()))
                            {
                                if let Value::Sequence(seq) = field_data {
                                    return extract_strings_raw(seq);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    None
}

fn clean_key_for_extraction(key: &str) -> String {
    if key.starts_with("faker.") {
        key[6..].to_string()
    } else if key.starts_with("faker_") {
        key[6..].replace('_', ".")
    } else if key.contains("faker.") {
        key.replace("faker.", "").replace('_', ".")
    } else {
        key.replace('_', ".")
    }
}

fn extract_nested_array(
    data: &Value,
    key: &str,
    context_category: Option<&str>,
) -> Option<Vec<String>> {
    let parts: Vec<&str> = key.split('.').collect();
    let mut current: &Value = data;

    for (i, part) in parts.iter().enumerate() {
        let part_str = part.to_string();
        let part_lower = part_str.to_lowercase();
        match current {
            Value::Mapping(map) => {
                // Try exact match first, then case-insensitive
                if let Some(next) = map.get(Value::String(part_str.clone())) {
                    current = next;
                } else if let Some(next) = map.get(Value::String(part_lower)) {
                    current = next;
                } else {
                    return None;
                }
            }
            Value::Sequence(seq) => {
                if i == parts.len() - 1 {
                    return extract_strings_from_seq(seq, context_category);
                }
                return None;
            }
            _ => return None,
        }
    }

    match current {
        Value::Sequence(seq) => extract_strings_from_seq(seq, context_category),
        _ => None,
    }
}

fn extract_strings_from_seq(seq: &[Value], _context_category: Option<&str>) -> Option<Vec<String>> {
    // Return raw strings without resolving placeholders - placeholders will be resolved later
    let arr: Vec<String> = seq
        .iter()
        .filter_map(|v| match v {
            Value::String(s) => Some(s.clone()),
            _ => None,
        })
        .collect();
    if arr.is_empty() { None } else { Some(arr) }
}

fn extract_nested_array_raw(data: &Value, key: &str) -> Option<Vec<String>> {
    let parts: Vec<&str> = key.split('.').collect();
    let mut current: &Value = data;

    for (i, part) in parts.iter().enumerate() {
        let part_str = part.to_string();
        let part_lower = part_str.to_lowercase();
        match current {
            Value::Mapping(map) => {
                if let Some(next) = map.get(Value::String(part_str.clone())) {
                    current = next;
                } else if let Some(next) = map.get(Value::String(part_lower)) {
                    current = next;
                } else {
                    return None;
                }
            }
            Value::Sequence(seq) => {
                if i == parts.len() - 1 {
                    return extract_strings_raw(seq);
                }
                return None;
            }
            _ => return None,
        }
    }

    match current {
        Value::Sequence(seq) => extract_strings_raw(seq),
        _ => None,
    }
}

fn extract_strings_raw(seq: &[Value]) -> Option<Vec<String>> {
    let arr: Vec<String> = seq
        .iter()
        .filter_map(|v| match v {
            Value::String(s) => Some(s.clone()),
            _ => None,
        })
        .collect();
    if arr.is_empty() { None } else { Some(arr) }
}

fn resolve_placeholder(s: &str, context_category: Option<&str>) -> String {
    resolve_multiple(s, context_category, 0, 10)
}

fn resolve_multiple(
    s: &str,
    context_category: Option<&str>,
    depth: usize,
    max_depth: usize,
) -> String {
    if depth >= max_depth {
        return s.to_string();
    }

    if !s.contains("#{") {
        return s.to_string();
    }

    // Track resolution to detect loops
    let original = s.to_string();
    let mut result = original.clone();
    let mut changed = true;

    while changed && depth < max_depth {
        changed = false;

        while let Some(start_byte) = result.find("#{") {
            let rest = &result[start_byte + 2..];
            let mut brace_depth = 1;
            let mut end_byte = start_byte + 2;

            for (char_idx, c) in rest.char_indices() {
                if c == '{' {
                    brace_depth += 1;
                } else if c == '}' {
                    brace_depth -= 1;
                    if brace_depth == 0 {
                        end_byte = start_byte + 2 + char_idx + 1;
                        break;
                    }
                }
            }

            if brace_depth == 0 {
                let placeholder = &result[start_byte..end_byte];
                if let Some(inner) = placeholder.get(2..placeholder.len() - 1) {
                    let replacement = resolve_inner(inner, context_category, depth + 1);
                    // Check if we got back the same placeholder (infinite loop)
                    if replacement == placeholder {
                        result = result.replace(placeholder, &format!("[UNRESOLVED:{}]", inner));
                    } else {
                        result = result.replace(placeholder, &replacement);
                    }
                    changed = true;
                    break;
                }
            } else {
                break;
            }
        }
    }

    result
}

fn resolve_inner(inner: &str, context_category: Option<&str>, depth: usize) -> String {
    if depth > 3 {
        return format!("[{}]", inner);
    }

    // Handle "Name.first_name" or "name.first_name" notation
    if inner.contains('.') {
        let parts: Vec<&str> = inner.split('.').collect();
        if parts.len() >= 2 {
            let cat = parts[0].to_lowercase();
            let field = parts[1].to_lowercase();

            // Use fetch_locale_with_context to get proper context lookup
            if let Some(result) = fetch_locale_with_context(&field, "en", Some(&cat)) {
                if !result.is_empty() {
                    use crate::config::FakerConfig;
                    let config = FakerConfig::current();
                    let idx = config.rand_usize(result.len());
                    let v = result[idx].clone();
                    return resolve_multiple(&v, context_category, depth + 1, 10);
                }
            }
        }
    } else {
        // Simple key (no dot notation) - try multiple common contexts
        // to find where this key belongs
        let contexts_to_try: Vec<&str> = if let Some(cat) = context_category {
            vec![cat, "name", "address", "company", "internet"]
        } else {
            vec!["name", "address", "company", "internet"]
        };

        for cat in contexts_to_try {
            if let Some(result) = fetch_locale_with_context(inner, "en", Some(cat)) {
                if !result.is_empty() {
                    use crate::config::FakerConfig;
                    let config = FakerConfig::current();
                    let idx = config.rand_usize(result.len());
                    let v = result[idx].clone();
                    return resolve_multiple(&v, context_category, depth + 1, 10);
                }
            }
        }
    }

    inner.to_string()
}

fn resolve_simple(s: &str, context_category: Option<&str>) -> String {
    // If no placeholder, return as-is
    if !(s.starts_with("#{") && s.ends_with('}')) {
        return s.to_string();
    }

    // Extract inner part
    let inner = &s[2..s.len() - 1];

    // Handle dot notation like "name.first_name" or "Name.first_name"
    if inner.contains('.') {
        let parts: Vec<&str> = inner.split('.').collect();
        if parts.len() >= 2 {
            let cat = parts[0].to_lowercase();
            let field = parts[1].to_lowercase();
            let lookup_key = format!("{}_{}", cat, field);

            if let Some(values) = fetch_locale_with_context(&lookup_key, "en", None) {
                return values.first().cloned().unwrap_or_else(|| inner.to_string());
            }

            // Try en_name.yml, en_address.yml directly
            let en_cat_key = format!("en_{}", cat);
            if let Some(subdata) = LOCALE_DATA.get(&en_cat_key) {
                if let Some(v) = extract_direct_value(subdata, &field) {
                    return v;
                }
            }
        }
    }

    // Simple case - try with context
    if let Some(cat) = context_category {
        // First, try just the inner key directly (not prefixed) - but without recursive resolution
        if let Some(values) = fetch_locale_no_resolve(inner, "en", Some(cat)) {
            return values.first().cloned().unwrap_or_else(|| inner.to_string());
        }
    }

    // Try without context
    if let Some(values) = fetch_locale_with_context(inner, "en", None) {
        return values.first().cloned().unwrap_or_else(|| inner.to_string());
    }

    inner.to_string()
}

fn extract_direct_value(data: &Value, field: &str) -> Option<String> {
    if let Value::Mapping(top) = data {
        // Try en.faker.category.field
        if let Some(en) = top.get(Value::String("en".to_string())) {
            if let Value::Mapping(en_map) = en {
                if let Some(faker) = en_map.get(Value::String("faker".to_string())) {
                    if let Value::Mapping(faker_m) = faker {
                        for (_, cat_val) in faker_m.iter() {
                            if let Value::Mapping(cat_m) = cat_val {
                                if let Some(field_val) =
                                    cat_m.get(Value::String(field.to_string()))
                                {
                                    if let Value::Sequence(seq) = field_val {
                                        if let Some(first) = seq.first() {
                                            if let Value::String(s) = first {
                                                return Some(s.clone());
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        // Try direct faker.category.field
        if let Some(faker) = top.get(Value::String("faker".to_string())) {
            if let Value::Mapping(faker_m) = faker {
                for (_, cat_val) in faker_m.iter() {
                    if let Value::Mapping(cat_m) = cat_val {
                        if let Some(field_val) = cat_m.get(Value::String(field.to_string())) {
                            if let Value::Sequence(seq) = field_val {
                                if let Some(first) = seq.first() {
                                    if let Value::String(s) = first {
                                        return Some(s.clone());
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    None
}

fn resolve_placeholder_recursive(
    s: &str,
    context_category: Option<&str>,
    depth: usize,
    max_depth: usize,
) -> String {
    if depth >= max_depth {
        return s.to_string();
    }

    // Handle multiple placeholders like "#{foo}#{bar}"
    if s.contains("#{") {
        if depth >= max_depth {
            return s.to_string();
        }

        let mut result = s.to_string();
        // Keep replacing until no more placeholders or max depth
        loop {
            let placeholder_start = result.find("#{");
            if placeholder_start.is_none() {
                break;
            }
            let after_start = &result[placeholder_start.unwrap()..];
            let placeholder_end = after_start.find('}');
            if placeholder_end.is_none() {
                break;
            }

            let full_placeholder = &after_start[..placeholder_end.unwrap() + 1];
            let inner = &full_placeholder[2..full_placeholder.len() - 1];

            let replacement = resolve_single_placeholder(inner, context_category, depth, max_depth);

            // Check if replacement still contains placeholders and we can recurse
            if replacement.contains("#{") && depth < max_depth - 1 {
                let resolved = resolve_placeholder_recursive(
                    &replacement,
                    context_category,
                    depth + 1,
                    max_depth,
                );
                result = result.replace(full_placeholder, &resolved);
            } else {
                result = result.replace(full_placeholder, &replacement);
            }

            // Safety check to prevent infinite loops
            if depth > 5 {
                break;
            }
        }
        return result;
    }

    // Handle single placeholder
    if s.starts_with("#{") && s.ends_with('}') {
        let inner = &s[2..s.len() - 1];
        resolve_single_placeholder(inner, context_category, depth, max_depth)
    } else {
        s.to_string()
    }
}

fn resolve_single_placeholder(
    inner: &str,
    context_category: Option<&str>,
    depth: usize,
    max_depth: usize,
) -> String {
    // Handle cases like "Name.first_name" or "name.first_name"
    let parts: Vec<&str> = inner.split('.').collect();
    if parts.len() >= 2 {
        let cat = parts[0].to_lowercase();
        let field = parts[1].to_lowercase();
        let lookup_key = format!("{}_{}", cat, field);

        if let Some(values) = fetch_locale_with_context(&lookup_key, "en", None) {
            if let Some(v) = values.first() {
                if depth < max_depth - 1 && v.contains("#{") {
                    return resolve_placeholder_recursive(
                        v,
                        context_category,
                        depth + 1,
                        max_depth,
                    );
                }
                return v.to_string();
            }
        }

        let en_cat_key = format!("en_{}", cat);
        if let Some(subdata) = LOCALE_DATA.get(&en_cat_key) {
            if let Some(v) = extract_nested_value_simple(subdata, &field) {
                if depth < max_depth - 1 && v.contains("#{") {
                    return resolve_placeholder_recursive(
                        &v,
                        context_category,
                        depth + 1,
                        max_depth,
                    );
                }
                return v;
            }
        }

        let faker_lookup = format!("faker.{}.{}", cat, field);
        if let Some(values) = fetch_locale(&faker_lookup, "en") {
            if let Some(v) = values.first() {
                if depth < max_depth - 1 && v.contains("#{") {
                    return resolve_placeholder_recursive(
                        v,
                        context_category,
                        depth + 1,
                        max_depth,
                    );
                }
                return v.to_string();
            }
        }
    }

    // Simple case like "first_name", "city_prefix"
    if let Some(cat) = context_category {
        let key = format!("{}_{}", cat, inner);
        if let Some(values) = fetch_locale_with_context(&key, "en", Some(cat)) {
            if let Some(v) = values.first() {
                if depth < max_depth - 1 && v.contains("#{") {
                    return resolve_placeholder_recursive(
                        v,
                        context_category,
                        depth + 1,
                        max_depth,
                    );
                }
                return v.to_string();
            }
        }
    }

    if let Some(values) = fetch_locale_with_context(inner, "en", None) {
        if let Some(v) = values.first() {
            if depth < max_depth - 1 && v.contains("#{") {
                return resolve_placeholder_recursive(v, context_category, depth + 1, max_depth);
            }
            return v.to_string();
        }
    }

    inner.to_string()
}

fn extract_nested_value_simple(data: &Value, field: &str) -> Option<String> {
    if let Value::Mapping(top) = data {
        // Check for locale prefix (en)
        if let Some(en) = top.get(Value::String("en".to_string())) {
            if let Value::Mapping(en_map) = en {
                // Check for faker
                if let Some(faker) = en_map.get(Value::String("faker".to_string())) {
                    if let Value::Mapping(faker_m) = faker {
                        // Try each category
                        for (_, cat_val) in faker_m.iter() {
                            if let Value::Mapping(cat_m) = cat_val {
                                if let Some(field_val) =
                                    cat_m.get(Value::String(field.to_string()))
                                {
                                    if let Value::Sequence(seq) = field_val {
                                        if let Some(first) = seq.first() {
                                            if let Value::String(s) = first {
                                                return Some(s.clone());
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        // Try direct faker key
        if let Some(faker) = top.get(Value::String("faker".to_string())) {
            if let Value::Mapping(faker_m) = faker {
                for (_, cat_val) in faker_m.iter() {
                    if let Value::Mapping(cat_m) = cat_val {
                        if let Some(field_val) = cat_m.get(Value::String(field.to_string())) {
                            if let Value::Sequence(seq) = field_val {
                                if let Some(first) = seq.first() {
                                    if let Value::String(s) = first {
                                        return Some(s.clone());
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    None
}

fn extract_nested_value(
    data: &Value,
    key: &str,
    _context_category: Option<&str>,
) -> Option<String> {
    let parts: Vec<&str> = key.split('.').collect();
    let mut current: &Value = data;

    for part in parts.iter() {
        let part_lower = part.to_lowercase();
        match current {
            Value::Mapping(map) => {
                if let Some(next) = map.get(Value::String(part_lower.clone())) {
                    current = next;
                } else if let Some(next) = map.get(Value::String(part.to_string())) {
                    current = next;
                } else {
                    return None;
                }
            }
            _ => return None,
        }
    }

    match current {
        Value::String(s) => Some(s.clone()),
        Value::Sequence(seq) if !seq.is_empty() => {
            if let Some(first) = seq.first() {
                match first {
                    Value::String(s) => Some(s.clone()),
                    _ => None,
                }
            } else {
                None
            }
        }
        _ => None,
    }
}

fn extract_array_from_value(data: &Value, key: &str) -> Option<Vec<String>> {
    // key can be like: "internet.user_agent.chrome" or "user_agent.chrome" or "faker.internet.user_agent.chrome"
    let parts: Vec<&str> = key.split('.').collect();

    // Determine where to start - find "faker" or the first real category
    let mut start_idx = 0;
    for (i, part) in parts.iter().enumerate() {
        if *part == "faker" {
            start_idx = i + 1;
            break;
        }
        if i > 0 && i < parts.len() - 1 {
            // If we find a reasonable category name (not faker), start from there
            if *part != "internet" && *part != "name" && *part != "address" && *part != "company" {
                // Keep looking
            }
        }
    }

    // Try to get to the "faker" level first
    let faker_map = match data {
        Value::Mapping(m) => {
            // Look for "faker" key
            if let Some(faker) = m.get(Value::String("faker".to_string())) {
                faker
            } else {
                // No "faker" key, use the data as-is
                return extract_array_simple(data, key);
            }
        }
        _ => return None,
    };

    // Now navigate from faker through the rest of the path
    let mut current = faker_map;

    // Skip "internet" if present, or start from the right part
    let mut found_internet = false;
    for part in parts.iter() {
        if *part == "internet" {
            found_internet = true;
            continue;
        }
        if found_internet || start_idx > 0 {
            let part_str = part.to_string();
            current = match current {
                Value::Mapping(map) => {
                    if let Some(v) = map.get(Value::String(part_str.clone())) {
                        v
                    } else {
                        // Try without "internet" prefix
                        return extract_array_simple(data, &parts[parts.len() - 1..].join("."));
                    }
                }
                _ => return None,
            };
        }
    }

    match current {
        Value::Sequence(seq) => {
            let arr: Vec<String> = seq
                .iter()
                .filter_map(|v| match v {
                    Value::String(s) => Some(s.clone()),
                    _ => None,
                })
                .collect();
            if arr.is_empty() { None } else { Some(arr) }
        }
        _ => None,
    }
}

fn extract_array_simple(data: &Value, key: &str) -> Option<Vec<String>> {
    // Simplified version that just looks for the last key in the data
    let parts: Vec<&str> = key.split('.').collect();
    let last_key = parts.last().unwrap_or(&key);

    let faker_map = match data {
        Value::Mapping(m) => m.get(Value::String("faker".to_string()))?,
        _ => return None,
    };

    match faker_map {
        Value::Mapping(faker_m) => {
            // Try each category in order until we find the key
            for category in ["internet", "name", "address", "company"] {
                if let Some(cat) = faker_m.get(Value::String(category.to_string())) {
                    if let Value::Mapping(cat_m) = cat {
                        if let Some(result) = cat_m.get(Value::String(last_key.to_string())) {
                            if let Value::Sequence(seq) = result {
                                return Some(
                                    seq.iter()
                                        .filter_map(|v| match v {
                                            Value::String(s) => Some(s.clone()),
                                            _ => None,
                                        })
                                        .collect(),
                                );
                            }
                        }
                    }
                }
            }
            None
        }
        _ => None,
    }
}

fn extract_array(data: &Value, key: &str) -> Option<Vec<String>> {
    let parts: Vec<&str> = key.split('.').collect();

    // The key can be:
    // - "faker.name.first_name" (with faker prefix)
    // - "name.first_name" (without faker prefix)
    // - "internet.user_agent.chrome" (nested category with vendor)

    // Find where to start - either at "faker" or at the first category
    let start_idx = if parts.first() == Some(&"faker") {
        1
    } else {
        0
    };

    // First, look for "faker" key in the data mapping
    // If not found (subdirectory files), just start from parts[0]
    let faker_map = match data {
        Value::Mapping(m) => {
            // Try to get faker first, if not, try the first part of the path
            if let Some(faker) = m.get(Value::String("faker".to_string())) {
                faker
            } else if let Some(first) = m.get(Value::String(parts[start_idx].to_string())) {
                first
            } else {
                m.get(Value::String(parts[0].to_string()))?
            }
        }
        _ => return None,
    };

    let mut current = faker_map;

    // Now traverse the rest of the path
    for part in parts.iter().skip(start_idx) {
        let part_str = part.to_string();
        current = match current {
            Value::Mapping(map) => map.get(Value::String(part_str))?,
            _ => return None,
        };
    }

    match current {
        Value::Sequence(seq) => {
            let arr: Vec<String> = seq
                .iter()
                .filter_map(|v| match v {
                    Value::String(s) => Some(s.clone()),
                    _ => None,
                })
                .collect();
            if arr.is_empty() { None } else { Some(arr) }
        }
        _ => None,
    }
}

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

    #[test]
    fn test_fetch_locale() {
        let result = fetch_locale("faker.name.male_first_name", "en");
        assert!(result.is_some());
        let names = result.unwrap();
        assert!(!names.is_empty());
    }
}