oxihuman-core 0.1.2

Core data structures, algorithms, and asset management for OxiHuman
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
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0

#![allow(dead_code)]

//! A real TOML parser supporting sections, inline tables, arrays of tables,
//! multiline strings, literal strings, various numeric formats, booleans,
//! date/time values, and comments.

/// A TOML value.
#[derive(Debug, Clone, PartialEq)]
pub enum TomlValue {
    /// A string value.
    Str(String),
    /// A 64-bit integer value.
    Int(i64),
    /// A 64-bit floating-point value.
    Float(f64),
    /// A boolean value.
    Bool(bool),
    /// A date/time value stored as its original string representation.
    DateTime(String),
    /// An array of TOML values.
    Array(Vec<TomlValue>),
    /// A table (ordered map of key-value pairs).
    Table(TomlTable),
}

/// An ordered map of key-value pairs representing a TOML table.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct TomlTable {
    pub entries: Vec<(String, TomlValue)>,
}

impl TomlTable {
    /// Create an empty table.
    pub fn new() -> Self {
        Self::default()
    }

    /// Insert a key-value pair (overwrites if key exists).
    pub fn insert(&mut self, key: impl Into<String>, value: TomlValue) {
        let key = key.into();
        for entry in &mut self.entries {
            if entry.0 == key {
                entry.1 = value;
                return;
            }
        }
        self.entries.push((key, value));
    }

    /// Look up a value by simple (non-dotted) key.
    pub fn get(&self, key: &str) -> Option<&TomlValue> {
        self.entries.iter().find(|(k, _)| k == key).map(|(_, v)| v)
    }

    /// Look up a value by simple key (mutable).
    pub fn get_mut(&mut self, key: &str) -> Option<&mut TomlValue> {
        self.entries
            .iter_mut()
            .find(|(k, _)| k == key)
            .map(|(_, v)| v)
    }

    /// Look up a value by dotted key path (e.g. "section.key").
    pub fn get_path(&self, path: &str) -> Option<&TomlValue> {
        let parts: Vec<&str> = path.split('.').collect();
        if parts.is_empty() {
            return None;
        }
        if parts.len() == 1 {
            return self.get(parts[0]);
        }
        let mut current = self;
        for &part in &parts[..parts.len() - 1] {
            match current.get(part) {
                Some(TomlValue::Table(t)) => current = t,
                _ => return None,
            }
        }
        current.get(parts[parts.len() - 1])
    }

    /// Return the number of top-level entries.
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Check if empty.
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }
}

/// A parsed TOML document backed by a flat entries list (backward compatible).
#[derive(Debug, Clone, Default)]
pub struct TomlDoc {
    pub entries: Vec<(String, TomlValue)>,
}

impl TomlDoc {
    /// Look up a value by dotted key path (e.g. "section.key").
    pub fn get(&self, path: &str) -> Option<&TomlValue> {
        let parts: Vec<&str> = path.split('.').collect();
        if parts.is_empty() {
            return None;
        }
        self.get_path_inner(&parts)
    }

    fn get_path_inner(&self, parts: &[&str]) -> Option<&TomlValue> {
        if parts.is_empty() {
            return None;
        }
        if parts.len() == 1 {
            return self
                .entries
                .iter()
                .find(|(k, _)| k == parts[0])
                .map(|(_, v)| v);
        }
        let first = parts[0];
        let val = self
            .entries
            .iter()
            .find(|(k, _)| k == first)
            .map(|(_, v)| v)?;
        match val {
            TomlValue::Table(t) => t.get_path(&parts[1..].join(".")),
            _ => None,
        }
    }
}

/// TOML parse error with line number.
#[derive(Debug, Clone, PartialEq)]
pub struct TomlParseError {
    pub line: usize,
    pub col: usize,
    pub message: String,
}

impl std::fmt::Display for TomlParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "TOML parse error at line {}:{}: {}",
            self.line, self.col, self.message
        )
    }
}

impl std::error::Error for TomlParseError {}

fn mk_err(line: usize, col: usize, msg: impl Into<String>) -> TomlParseError {
    TomlParseError {
        line,
        col,
        message: msg.into(),
    }
}

// ---------------------------------------------------------------------------
// Character-level parser state
// ---------------------------------------------------------------------------

struct Parser<'a> {
    input: &'a str,
    chars: Vec<char>,
    pos: usize,
    line: usize,
    col: usize,
}

impl<'a> Parser<'a> {
    fn new(input: &'a str) -> Self {
        Self {
            input,
            chars: input.chars().collect(),
            pos: 0,
            line: 1,
            col: 1,
        }
    }

    fn peek(&self) -> Option<char> {
        self.chars.get(self.pos).copied()
    }

    fn peek_at(&self, offset: usize) -> Option<char> {
        self.chars.get(self.pos + offset).copied()
    }

    fn advance(&mut self) -> Option<char> {
        let ch = self.chars.get(self.pos).copied()?;
        self.pos += 1;
        if ch == '\n' {
            self.line += 1;
            self.col = 1;
        } else {
            self.col += 1;
        }
        Some(ch)
    }

    fn at_end(&self) -> bool {
        self.pos >= self.chars.len()
    }

    fn skip_ws(&mut self) {
        while let Some(ch) = self.peek() {
            if ch == ' ' || ch == '\t' {
                self.advance();
            } else {
                break;
            }
        }
    }

    fn skip_ws_and_nl(&mut self) {
        while let Some(ch) = self.peek() {
            if ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' {
                self.advance();
            } else {
                break;
            }
        }
    }

    fn skip_comment(&mut self) {
        if self.peek() == Some('#') {
            while let Some(ch) = self.peek() {
                if ch == '\n' {
                    break;
                }
                self.advance();
            }
        }
    }

    fn skip_insignificant(&mut self) {
        loop {
            self.skip_ws_and_nl();
            if self.peek() == Some('#') {
                self.skip_comment();
            } else {
                break;
            }
        }
    }

    fn skip_line_tail(&mut self) {
        self.skip_ws();
        if self.peek() == Some('#') {
            self.skip_comment();
        }
    }

    fn expect(&mut self, expected: char) -> Result<(), TomlParseError> {
        match self.advance() {
            Some(ch) if ch == expected => Ok(()),
            Some(ch) => Err(mk_err(
                self.line,
                self.col,
                format!("expected '{}', found '{}'", expected, ch),
            )),
            None => Err(mk_err(
                self.line,
                self.col,
                format!("expected '{}', found end of input", expected),
            )),
        }
    }
}

// ---------------------------------------------------------------------------
// Key parsing
// ---------------------------------------------------------------------------

fn parse_bare_key(p: &mut Parser) -> Result<String, TomlParseError> {
    let mut key = String::new();
    while let Some(ch) = p.peek() {
        if ch.is_ascii_alphanumeric() || ch == '-' || ch == '_' {
            key.push(ch);
            p.advance();
        } else {
            break;
        }
    }
    if key.is_empty() {
        return Err(mk_err(p.line, p.col, "expected a key"));
    }
    Ok(key)
}

fn parse_key(p: &mut Parser) -> Result<String, TomlParseError> {
    match p.peek() {
        Some('"') => parse_basic_string(p),
        Some('\'') => parse_literal_string(p),
        _ => parse_bare_key(p),
    }
}

fn parse_dotted_key(p: &mut Parser) -> Result<Vec<String>, TomlParseError> {
    let mut parts = vec![parse_key(p)?];
    loop {
        p.skip_ws();
        if p.peek() == Some('.') {
            p.advance();
            p.skip_ws();
            parts.push(parse_key(p)?);
        } else {
            break;
        }
    }
    Ok(parts)
}

// ---------------------------------------------------------------------------
// String parsing
// ---------------------------------------------------------------------------

fn parse_basic_string(p: &mut Parser) -> Result<String, TomlParseError> {
    let line = p.line;
    let col = p.col;
    p.expect('"')?;

    if p.peek() == Some('"') && p.peek_at(1) == Some('"') {
        p.advance();
        p.advance();
        return parse_ml_basic_string(p, line, col);
    }

    let mut s = String::new();
    loop {
        match p.advance() {
            Some('"') => return Ok(s),
            Some('\\') => {
                let esc = parse_escape(p)?;
                s.push(esc);
            }
            Some('\n') | None => {
                return Err(mk_err(line, col, "unterminated basic string"));
            }
            Some(ch) => s.push(ch),
        }
    }
}

fn parse_ml_basic_string(
    p: &mut Parser,
    sl: usize,
    sc: usize,
) -> Result<String, TomlParseError> {
    // Skip first newline after opening """
    if p.peek() == Some('\n') {
        p.advance();
    } else if p.peek() == Some('\r') && p.peek_at(1) == Some('\n') {
        p.advance();
        p.advance();
    }

    let mut s = String::new();
    loop {
        match p.peek() {
            Some('"') if p.peek_at(1) == Some('"') && p.peek_at(2) == Some('"') => {
                p.advance();
                p.advance();
                p.advance();
                return Ok(s);
            }
            Some('\\') => {
                p.advance();
                if matches!(p.peek(), Some('\n') | Some('\r')) {
                    while let Some(ch) = p.peek() {
                        if ch == '\n' || ch == '\r' || ch == ' ' || ch == '\t' {
                            p.advance();
                        } else {
                            break;
                        }
                    }
                } else {
                    let esc = parse_escape(p)?;
                    s.push(esc);
                }
            }
            Some(_) => {
                if let Some(ch) = p.advance() {
                    s.push(ch);
                }
            }
            None => {
                return Err(mk_err(sl, sc, "unterminated multiline basic string"));
            }
        }
    }
}

fn parse_escape(p: &mut Parser) -> Result<char, TomlParseError> {
    let line = p.line;
    let col = p.col;
    match p.advance() {
        Some('b') => Ok('\u{0008}'),
        Some('t') => Ok('\t'),
        Some('n') => Ok('\n'),
        Some('f') => Ok('\u{000C}'),
        Some('r') => Ok('\r'),
        Some('"') => Ok('"'),
        Some('\\') => Ok('\\'),
        Some('u') => parse_unicode_esc(p, 4),
        Some('U') => parse_unicode_esc(p, 8),
        Some(ch) => Err(mk_err(line, col, format!("invalid escape: \\{}", ch))),
        None => Err(mk_err(line, col, "unexpected end of input in escape")),
    }
}

fn parse_unicode_esc(p: &mut Parser, digits: usize) -> Result<char, TomlParseError> {
    let line = p.line;
    let col = p.col;
    let mut hex = String::with_capacity(digits);
    for _ in 0..digits {
        match p.advance() {
            Some(ch) if ch.is_ascii_hexdigit() => hex.push(ch),
            _ => return Err(mk_err(line, col, "invalid unicode escape")),
        }
    }
    let code = u32::from_str_radix(&hex, 16)
        .map_err(|_| mk_err(line, col, "invalid unicode escape value"))?;
    char::from_u32(code).ok_or_else(|| mk_err(line, col, "invalid unicode code point"))
}

fn parse_literal_string(p: &mut Parser) -> Result<String, TomlParseError> {
    let line = p.line;
    let col = p.col;
    p.expect('\'')?;

    if p.peek() == Some('\'') && p.peek_at(1) == Some('\'') {
        p.advance();
        p.advance();
        return parse_ml_literal_string(p, line, col);
    }

    let mut s = String::new();
    loop {
        match p.advance() {
            Some('\'') => return Ok(s),
            Some('\n') | None => {
                return Err(mk_err(line, col, "unterminated literal string"));
            }
            Some(ch) => s.push(ch),
        }
    }
}

fn parse_ml_literal_string(
    p: &mut Parser,
    sl: usize,
    sc: usize,
) -> Result<String, TomlParseError> {
    if p.peek() == Some('\n') {
        p.advance();
    } else if p.peek() == Some('\r') && p.peek_at(1) == Some('\n') {
        p.advance();
        p.advance();
    }

    let mut s = String::new();
    loop {
        match p.peek() {
            Some('\'') if p.peek_at(1) == Some('\'') && p.peek_at(2) == Some('\'') => {
                p.advance();
                p.advance();
                p.advance();
                return Ok(s);
            }
            Some(_) => {
                if let Some(ch) = p.advance() {
                    s.push(ch);
                }
            }
            None => {
                return Err(mk_err(sl, sc, "unterminated multiline literal string"));
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Value parsing
// ---------------------------------------------------------------------------

fn parse_value_full(p: &mut Parser) -> Result<TomlValue, TomlParseError> {
    p.skip_ws();
    let line = p.line;
    let col = p.col;

    match p.peek() {
        Some('"') => parse_basic_string(p).map(TomlValue::Str),
        Some('\'') => parse_literal_string(p).map(TomlValue::Str),
        Some('[') => parse_array_val(p),
        Some('{') => parse_inline_table(p),
        Some('t') => parse_bool_true(p),
        Some('f') => parse_bool_false(p),
        Some('i') => parse_kw_inf(p, 1.0),
        Some('n') => parse_kw_nan(p, 1.0),
        Some('+') | Some('-') => parse_signed(p),
        Some(ch) if ch.is_ascii_digit() => parse_num_or_dt(p),
        _ => Err(mk_err(line, col, "unexpected value")),
    }
}

fn parse_bool_true(p: &mut Parser) -> Result<TomlValue, TomlParseError> {
    let line = p.line;
    let col = p.col;
    for expected in ['t', 'r', 'u', 'e'] {
        match p.advance() {
            Some(ch) if ch == expected => {}
            _ => return Err(mk_err(line, col, "invalid boolean")),
        }
    }
    Ok(TomlValue::Bool(true))
}

fn parse_bool_false(p: &mut Parser) -> Result<TomlValue, TomlParseError> {
    let line = p.line;
    let col = p.col;
    for expected in ['f', 'a', 'l', 's', 'e'] {
        match p.advance() {
            Some(ch) if ch == expected => {}
            _ => return Err(mk_err(line, col, "invalid boolean")),
        }
    }
    Ok(TomlValue::Bool(false))
}

fn parse_kw_inf(p: &mut Parser, sign: f64) -> Result<TomlValue, TomlParseError> {
    let line = p.line;
    let col = p.col;
    for expected in ['i', 'n', 'f'] {
        match p.advance() {
            Some(ch) if ch == expected => {}
            _ => return Err(mk_err(line, col, "invalid value")),
        }
    }
    Ok(TomlValue::Float(sign * f64::INFINITY))
}

fn parse_kw_nan(p: &mut Parser, _sign: f64) -> Result<TomlValue, TomlParseError> {
    let line = p.line;
    let col = p.col;
    for expected in ['n', 'a', 'n'] {
        match p.advance() {
            Some(ch) if ch == expected => {}
            _ => return Err(mk_err(line, col, "invalid value")),
        }
    }
    Ok(TomlValue::Float(f64::NAN))
}

fn parse_signed(p: &mut Parser) -> Result<TomlValue, TomlParseError> {
    let sign_ch = p.advance().ok_or_else(|| mk_err(p.line, p.col, "unexpected eof"))?;
    let sign: f64 = if sign_ch == '-' { -1.0 } else { 1.0 };

    if p.peek() == Some('i') {
        return parse_kw_inf(p, sign);
    }
    if p.peek() == Some('n') {
        return parse_kw_nan(p, sign);
    }

    let num_str = collect_num_chars(p);
    let full = format!("{}{}", sign_ch, num_str);
    parse_num_string(&full, p.line, p.col)
}

fn collect_num_chars(p: &mut Parser) -> String {
    let mut s = String::new();
    while let Some(ch) = p.peek() {
        if ch.is_ascii_alphanumeric()
            || ch == '.'
            || ch == '_'
            || ch == '-'
            || ch == '+'
            || ch == ':'
            || ch == 'e'
            || ch == 'E'
            || ch == 'T'
            || ch == 'Z'
        {
            s.push(ch);
            p.advance();
        } else {
            break;
        }
    }
    s
}

fn parse_num_or_dt(p: &mut Parser) -> Result<TomlValue, TomlParseError> {
    let line = p.line;
    let col = p.col;
    let s = collect_num_chars(p);
    if looks_like_datetime(&s) {
        return Ok(TomlValue::DateTime(s));
    }
    parse_num_string(&s, line, col)
}

fn looks_like_datetime(s: &str) -> bool {
    let clean = s.replace('_', "");
    if clean.len() >= 10 {
        let bytes = clean.as_bytes();
        if bytes.len() >= 10
            && bytes[4] == b'-'
            && bytes[7] == b'-'
            && bytes[0].is_ascii_digit()
            && bytes[5].is_ascii_digit()
            && bytes[8].is_ascii_digit()
        {
            return true;
        }
    }
    if clean.len() >= 8 && clean.contains(':') {
        let parts: Vec<&str> = clean.split(':').collect();
        if parts.len() >= 2 && parts[0].len() == 2 && parts[0].chars().all(|c| c.is_ascii_digit())
        {
            return true;
        }
    }
    false
}

fn parse_num_string(s: &str, line: usize, col: usize) -> Result<TomlValue, TomlParseError> {
    let clean = s.replace('_', "");

    if clean.is_empty() {
        return Err(mk_err(line, col, "empty number"));
    }

    // Hex
    if clean.starts_with("0x") || clean.starts_with("0X") {
        let val = i64::from_str_radix(&clean[2..], 16)
            .map_err(|e| mk_err(line, col, format!("invalid hex integer: {}", e)))?;
        return Ok(TomlValue::Int(val));
    }
    // Octal
    if clean.starts_with("0o") || clean.starts_with("0O") {
        let val = i64::from_str_radix(&clean[2..], 8)
            .map_err(|e| mk_err(line, col, format!("invalid octal integer: {}", e)))?;
        return Ok(TomlValue::Int(val));
    }
    // Binary
    if clean.starts_with("0b") || clean.starts_with("0B") {
        let val = i64::from_str_radix(&clean[2..], 2)
            .map_err(|e| mk_err(line, col, format!("invalid binary integer: {}", e)))?;
        return Ok(TomlValue::Int(val));
    }

    // Float (contains '.', 'e', or 'E')
    if clean.contains('.') || clean.contains('e') || clean.contains('E') {
        let val: f64 = clean
            .parse()
            .map_err(|e| mk_err(line, col, format!("invalid float: {}", e)))?;
        return Ok(TomlValue::Float(val));
    }

    // Integer
    let val: i64 = clean
        .parse()
        .map_err(|e| mk_err(line, col, format!("invalid integer: {}", e)))?;
    Ok(TomlValue::Int(val))
}

fn parse_array_val(p: &mut Parser) -> Result<TomlValue, TomlParseError> {
    p.expect('[')?;
    let mut items = Vec::new();
    loop {
        p.skip_insignificant();
        if p.peek() == Some(']') {
            p.advance();
            return Ok(TomlValue::Array(items));
        }
        let val = parse_value_full(p)?;
        items.push(val);
        p.skip_insignificant();
        match p.peek() {
            Some(',') => {
                p.advance();
            }
            Some(']') => {}
            _ => {
                return Err(mk_err(p.line, p.col, "expected ',' or ']' in array"));
            }
        }
    }
}

fn parse_inline_table(p: &mut Parser) -> Result<TomlValue, TomlParseError> {
    p.expect('{')?;
    let mut table = TomlTable::new();
    p.skip_ws();
    if p.peek() == Some('}') {
        p.advance();
        return Ok(TomlValue::Table(table));
    }
    loop {
        p.skip_ws();
        let key_parts = parse_dotted_key(p)?;
        p.skip_ws();
        p.expect('=')?;
        p.skip_ws();
        let val = parse_value_full(p)?;
        insert_dotted(&mut table, &key_parts, val, p.line)?;
        p.skip_ws();
        match p.peek() {
            Some(',') => {
                p.advance();
            }
            Some('}') => {
                p.advance();
                return Ok(TomlValue::Table(table));
            }
            _ => {
                return Err(mk_err(p.line, p.col, "expected ',' or '}' in inline table"));
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Table insertion helpers
// ---------------------------------------------------------------------------

fn insert_dotted(
    table: &mut TomlTable,
    parts: &[String],
    value: TomlValue,
    line: usize,
) -> Result<(), TomlParseError> {
    if parts.is_empty() {
        return Err(mk_err(line, 0, "empty key"));
    }
    if parts.len() == 1 {
        table.insert(parts[0].clone(), value);
        return Ok(());
    }
    let first = &parts[0];
    let existing = table.get_mut(first);
    match existing {
        Some(TomlValue::Table(sub)) => {
            insert_dotted(sub, &parts[1..], value, line)?;
        }
        None => {
            let mut sub = TomlTable::new();
            insert_dotted(&mut sub, &parts[1..], value, line)?;
            table.insert(first.clone(), TomlValue::Table(sub));
        }
        _ => {
            return Err(mk_err(line, 0, format!("key '{}' is not a table", first)));
        }
    }
    Ok(())
}

fn ensure_table_path<'a>(
    root: &'a mut TomlTable,
    parts: &[String],
    line: usize,
) -> Result<&'a mut TomlTable, TomlParseError> {
    let mut current = root;
    for part in parts {
        let exists = current.get(part).is_some();
        if !exists {
            current.insert(part.clone(), TomlValue::Table(TomlTable::new()));
        }
        let entry = current.get_mut(part);
        match entry {
            Some(TomlValue::Table(t)) => current = t,
            _ => {
                return Err(mk_err(
                    line,
                    0,
                    format!("key '{}' conflicts with existing non-table value", part),
                ));
            }
        }
    }
    Ok(current)
}

fn ensure_aot_path<'a>(
    root: &'a mut TomlTable,
    parts: &[String],
    line: usize,
) -> Result<&'a mut TomlTable, TomlParseError> {
    if parts.is_empty() {
        return Err(mk_err(line, 0, "empty array-of-tables header"));
    }
    let (prefix, last) = parts.split_at(parts.len() - 1);
    let last_key = &last[0];
    let parent = ensure_table_path(root, prefix, line)?;
    let exists = parent.get(last_key).is_some();
    if !exists {
        parent.insert(last_key.clone(), TomlValue::Array(Vec::new()));
    }
    match parent.get_mut(last_key) {
        Some(TomlValue::Array(arr)) => {
            arr.push(TomlValue::Table(TomlTable::new()));
            let idx = arr.len() - 1;
            match &mut arr[idx] {
                TomlValue::Table(t) => Ok(t),
                _ => Err(mk_err(line, 0, "internal error")),
            }
        }
        _ => Err(mk_err(
            line,
            0,
            format!("key '{}' is not an array of tables", last_key),
        )),
    }
}

fn get_last_aot<'a>(
    root: &'a mut TomlTable,
    parts: &[String],
    line: usize,
) -> Result<&'a mut TomlTable, TomlParseError> {
    if parts.is_empty() {
        return Err(mk_err(line, 0, "empty path"));
    }
    let (prefix, last) = parts.split_at(parts.len() - 1);
    let last_key = &last[0];
    let parent = ensure_table_path(root, prefix, line)?;
    match parent.get_mut(last_key) {
        Some(TomlValue::Array(arr)) => {
            let len = arr.len();
            if len == 0 {
                return Err(mk_err(line, 0, "array of tables is empty"));
            }
            match &mut arr[len - 1] {
                TomlValue::Table(t) => Ok(t),
                _ => Err(mk_err(line, 0, "last element is not a table")),
            }
        }
        _ => Err(mk_err(
            line,
            0,
            format!("key '{}' is not an array of tables", last_key),
        )),
    }
}

// ---------------------------------------------------------------------------
// Section header parsing
// ---------------------------------------------------------------------------

enum SectionKind {
    Table(Vec<String>),
    ArrayOfTables(Vec<String>),
}

fn parse_section_header(p: &mut Parser) -> Result<SectionKind, TomlParseError> {
    p.expect('[')?;
    let is_aot = p.peek() == Some('[');
    if is_aot {
        p.advance();
    }
    p.skip_ws();
    let parts = parse_dotted_key(p)?;
    p.skip_ws();
    if is_aot {
        p.expect(']')?;
    }
    p.expect(']')?;
    if is_aot {
        Ok(SectionKind::ArrayOfTables(parts))
    } else {
        Ok(SectionKind::Table(parts))
    }
}

// ---------------------------------------------------------------------------
// Top-level document parser
// ---------------------------------------------------------------------------

/// Parse a TOML string into a `TomlDoc` (full parser).
///
/// Supports sections, inline tables, arrays of tables, multiline strings,
/// literal strings, various numeric formats, booleans, date/time values,
/// comments, and dotted keys.
pub fn parse_toml_full(input: &str) -> Result<TomlDoc, TomlParseError> {
    let mut p = Parser::new(input);
    let mut root = TomlTable::new();
    let mut current_path: Vec<String> = Vec::new();
    let mut is_aot = false;

    loop {
        p.skip_insignificant();
        if p.at_end() {
            break;
        }

        match p.peek() {
            Some('[') => {
                let header = parse_section_header(&mut p)?;
                p.skip_line_tail();
                if p.peek() == Some('\n') {
                    p.advance();
                } else if p.peek() == Some('\r') {
                    p.advance();
                    if p.peek() == Some('\n') {
                        p.advance();
                    }
                }
                match header {
                    SectionKind::Table(parts) => {
                        current_path = parts.clone();
                        is_aot = false;
                        ensure_table_path(&mut root, &parts, p.line)?;
                    }
                    SectionKind::ArrayOfTables(parts) => {
                        current_path = parts.clone();
                        is_aot = true;
                        ensure_aot_path(&mut root, &parts, p.line)?;
                    }
                }
            }
            Some('#') => {
                p.skip_comment();
            }
            Some(_) => {
                let key_parts = parse_dotted_key(&mut p)?;
                p.skip_ws();
                p.expect('=')?;
                p.skip_ws();
                let val = parse_value_full(&mut p)?;
                p.skip_line_tail();

                if current_path.is_empty() {
                    insert_dotted(&mut root, &key_parts, val, p.line)?;
                } else if is_aot {
                    let target = get_last_aot(&mut root, &current_path, p.line)?;
                    insert_dotted(target, &key_parts, val, p.line)?;
                } else {
                    let target = ensure_table_path(&mut root, &current_path, p.line)?;
                    insert_dotted(target, &key_parts, val, p.line)?;
                }
            }
            None => break,
        }
    }

    Ok(TomlDoc {
        entries: root.entries,
    })
}

// ---------------------------------------------------------------------------
// Backward-compatible simple parser
// ---------------------------------------------------------------------------

/// Parse a simple `key = value` text format into a `TomlDoc`.
///
/// Delegates to the full parser; falls back to line-by-line on error.
pub fn parse_toml_simple(text: &str) -> TomlDoc {
    match parse_toml_full(text) {
        Ok(doc) => doc,
        Err(_) => {
            let mut doc = TomlDoc::default();
            for line in text.lines() {
                let line = line.trim();
                if line.is_empty() || line.starts_with('#') {
                    continue;
                }
                if let Some(eq_pos) = line.find('=') {
                    let key = line[..eq_pos].trim().to_string();
                    let val_str = line[eq_pos + 1..].trim();
                    let value = parse_value_simple_fallback(val_str);
                    doc.entries.push((key, value));
                }
            }
            doc
        }
    }
}

fn parse_value_simple_fallback(s: &str) -> TomlValue {
    if s == "true" {
        return TomlValue::Bool(true);
    }
    if s == "false" {
        return TomlValue::Bool(false);
    }
    if s.starts_with('"') && s.ends_with('"') && s.len() >= 2 {
        return TomlValue::Str(s[1..s.len() - 1].to_string());
    }
    if s.starts_with('[') && s.ends_with(']') {
        let inner = &s[1..s.len() - 1];
        let items: Vec<TomlValue> = inner
            .split(',')
            .map(|item| parse_value_simple_fallback(item.trim()))
            .collect();
        return TomlValue::Array(items);
    }
    if s.contains('.') {
        if let Ok(f) = s.parse::<f64>() {
            return TomlValue::Float(f);
        }
    }
    if let Ok(i) = s.parse::<i64>() {
        return TomlValue::Int(i);
    }
    TomlValue::Str(s.to_string())
}

// ---------------------------------------------------------------------------
// Public accessor helpers (backward compatible)
// ---------------------------------------------------------------------------

/// Get an integer value by key.
pub fn toml_get_int(doc: &TomlDoc, key: &str) -> Option<i64> {
    doc.entries.iter().find(|(k, _)| k == key).and_then(|(_, v)| {
        if let TomlValue::Int(i) = v {
            Some(*i)
        } else {
            None
        }
    })
}

/// Get a float value by key.
pub fn toml_get_float(doc: &TomlDoc, key: &str) -> Option<f64> {
    doc.entries.iter().find(|(k, _)| k == key).and_then(|(_, v)| {
        if let TomlValue::Float(f) = v {
            Some(*f)
        } else {
            None
        }
    })
}

/// Get a string value by key.
pub fn toml_get_str<'a>(doc: &'a TomlDoc, key: &str) -> Option<&'a str> {
    doc.entries.iter().find(|(k, _)| k == key).and_then(|(_, v)| {
        if let TomlValue::Str(s) = v {
            Some(s.as_str())
        } else {
            None
        }
    })
}

/// Get a boolean value by key.
pub fn toml_get_bool(doc: &TomlDoc, key: &str) -> Option<bool> {
    doc.entries.iter().find(|(k, _)| k == key).and_then(|(_, v)| {
        if let TomlValue::Bool(b) = v {
            Some(*b)
        } else {
            None
        }
    })
}

/// Get the number of entries in the document.
pub fn entry_count(doc: &TomlDoc) -> usize {
    doc.entries.len()
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    // --- Original backward-compatibility tests ---

    #[test]
    fn parse_int() {
        let doc = parse_toml_simple("age = 42");
        assert_eq!(toml_get_int(&doc, "age"), Some(42));
    }

    #[test]
    fn parse_float() {
        let doc = parse_toml_simple("ratio = 1.5");
        assert!((toml_get_float(&doc, "ratio").expect("should succeed") - 1.5).abs() < 1e-9);
    }

    #[test]
    fn parse_bool_true() {
        let doc = parse_toml_simple("enabled = true");
        assert_eq!(toml_get_bool(&doc, "enabled"), Some(true));
    }

    #[test]
    fn parse_bool_false() {
        let doc = parse_toml_simple("debug = false");
        assert_eq!(toml_get_bool(&doc, "debug"), Some(false));
    }

    #[test]
    fn parse_string() {
        let doc = parse_toml_simple(r#"name = "Alice""#);
        assert_eq!(toml_get_str(&doc, "name"), Some("Alice"));
    }

    #[test]
    fn missing_key_returns_none() {
        let doc = parse_toml_simple("x = 1");
        assert_eq!(toml_get_int(&doc, "y"), None);
    }

    #[test]
    fn entry_count_correct() {
        let doc = parse_toml_simple("a = 1\nb = 2\nc = 3");
        assert_eq!(entry_count(&doc), 3);
    }

    #[test]
    fn comment_ignored() {
        let doc = parse_toml_simple("# comment\nval = 7");
        assert_eq!(toml_get_int(&doc, "val"), Some(7));
        assert_eq!(entry_count(&doc), 1);
    }

    #[test]
    fn empty_input() {
        let doc = parse_toml_simple("");
        assert_eq!(entry_count(&doc), 0);
    }

    #[test]
    fn parse_array() {
        let doc = parse_toml_simple("nums = [1, 2, 3]");
        let entry = doc.entries.iter().find(|(k, _)| k == "nums");
        assert!(entry.is_some());
        if let Some((_, TomlValue::Array(items))) = entry {
            assert_eq!(items.len(), 3);
        }
    }

    // --- Full parser tests ---

    #[test]
    fn test_section_header() {
        let input = "[server]\nport = 8080\nhost = \"localhost\"\n";
        let doc = parse_toml_full(input).expect("should succeed");
        let server = doc.get("server");
        assert!(server.is_some());
        if let Some(TomlValue::Table(t)) = server {
            assert_eq!(t.get("port"), Some(&TomlValue::Int(8080)));
            assert_eq!(
                t.get("host"),
                Some(&TomlValue::Str("localhost".to_string()))
            );
        } else {
            panic!("expected table");
        }
    }

    #[test]
    fn test_nested_section() {
        let input = "[database.connection]\ntimeout = 30\n";
        let doc = parse_toml_full(input).expect("should succeed");
        let val = doc.get("database.connection.timeout");
        assert_eq!(val, Some(&TomlValue::Int(30)));
    }

    #[test]
    fn test_inline_table() {
        let input = "point = { x = 1, y = 2 }\n";
        let doc = parse_toml_full(input).expect("should succeed");
        if let Some(TomlValue::Table(t)) = doc.get("point") {
            assert_eq!(t.get("x"), Some(&TomlValue::Int(1)));
            assert_eq!(t.get("y"), Some(&TomlValue::Int(2)));
        } else {
            panic!("expected inline table");
        }
    }

    #[test]
    fn test_array_of_tables() {
        let input = "[[products]]\nname = \"Hammer\"\n\n[[products]]\nname = \"Nail\"\n";
        let doc = parse_toml_full(input).expect("should succeed");
        if let Some(TomlValue::Array(arr)) = doc.get("products") {
            assert_eq!(arr.len(), 2);
            if let TomlValue::Table(t) = &arr[0] {
                assert_eq!(t.get("name"), Some(&TomlValue::Str("Hammer".to_string())));
            }
            if let TomlValue::Table(t) = &arr[1] {
                assert_eq!(t.get("name"), Some(&TomlValue::Str("Nail".to_string())));
            }
        } else {
            panic!("expected array of tables");
        }
    }

    #[test]
    fn test_multiline_basic_string() {
        let input = "bio = \"\"\"\nRoses are red\nViolets are blue\"\"\"\n";
        let doc = parse_toml_full(input).expect("should succeed");
        assert_eq!(
            doc.get("bio"),
            Some(&TomlValue::Str("Roses are red\nViolets are blue".to_string()))
        );
    }

    #[test]
    fn test_multiline_literal_string() {
        let input = "regex = '''\n\\d{2,3}\n'''\n";
        let doc = parse_toml_full(input).expect("should succeed");
        assert_eq!(
            doc.get("regex"),
            Some(&TomlValue::Str("\\d{2,3}\n".to_string()))
        );
    }

    #[test]
    fn test_literal_string() {
        let input = "path = 'C:\\Users\\admin'\n";
        let doc = parse_toml_full(input).expect("should succeed");
        assert_eq!(
            doc.get("path"),
            Some(&TomlValue::Str("C:\\Users\\admin".to_string()))
        );
    }

    #[test]
    fn test_hex_integer() {
        let input = "color = 0xFF00FF\n";
        let doc = parse_toml_full(input).expect("should succeed");
        assert_eq!(doc.get("color"), Some(&TomlValue::Int(0xFF00FF)));
    }

    #[test]
    fn test_octal_integer() {
        let input = "perm = 0o755\n";
        let doc = parse_toml_full(input).expect("should succeed");
        assert_eq!(doc.get("perm"), Some(&TomlValue::Int(0o755)));
    }

    #[test]
    fn test_binary_integer() {
        let input = "mask = 0b11010110\n";
        let doc = parse_toml_full(input).expect("should succeed");
        assert_eq!(doc.get("mask"), Some(&TomlValue::Int(0b11010110)));
    }

    #[test]
    fn test_underscore_integer() {
        let input = "big = 1_000_000\n";
        let doc = parse_toml_full(input).expect("should succeed");
        assert_eq!(doc.get("big"), Some(&TomlValue::Int(1_000_000)));
    }

    #[test]
    fn test_float_exponent() {
        let input = "sci = 5e+22\n";
        let doc = parse_toml_full(input).expect("should succeed");
        if let Some(TomlValue::Float(f)) = doc.get("sci") {
            assert!((f - 5e22).abs() < 1e16);
        } else {
            panic!("expected float");
        }
    }

    #[test]
    fn test_inf_nan() {
        let input = "a = inf\nb = -inf\nc = nan\n";
        let doc = parse_toml_full(input).expect("should succeed");
        if let Some(TomlValue::Float(f)) = doc.get("a") {
            assert!(f.is_infinite() && f.is_sign_positive());
        } else {
            panic!("expected +inf");
        }
        if let Some(TomlValue::Float(f)) = doc.get("b") {
            assert!(f.is_infinite() && f.is_sign_negative());
        } else {
            panic!("expected -inf");
        }
        if let Some(TomlValue::Float(f)) = doc.get("c") {
            assert!(f.is_nan());
        } else {
            panic!("expected nan");
        }
    }

    #[test]
    fn test_datetime() {
        let input = "created = 2024-01-15T10:30:00Z\n";
        let doc = parse_toml_full(input).expect("should succeed");
        assert_eq!(
            doc.get("created"),
            Some(&TomlValue::DateTime("2024-01-15T10:30:00Z".to_string()))
        );
    }

    #[test]
    fn test_dotted_key() {
        let input = "fruit.apple.color = \"red\"\n";
        let doc = parse_toml_full(input).expect("should succeed");
        let val = doc.get("fruit.apple.color");
        assert_eq!(val, Some(&TomlValue::Str("red".to_string())));
    }

    #[test]
    fn test_nested_array() {
        let input = "data = [[1, 2], [3, 4]]\n";
        let doc = parse_toml_full(input).expect("should succeed");
        if let Some(TomlValue::Array(outer)) = doc.get("data") {
            assert_eq!(outer.len(), 2);
            if let TomlValue::Array(inner) = &outer[0] {
                assert_eq!(inner.len(), 2);
            }
        } else {
            panic!("expected nested array");
        }
    }

    #[test]
    fn test_error_line_number() {
        let input = "a = 1\nb = 2\nc = @invalid\n";
        let result = parse_toml_full(input);
        assert!(result.is_err());
        if let Err(e) = result {
            assert_eq!(e.line, 3);
        }
    }

    #[test]
    fn test_escape_sequences() {
        let input = r#"msg = "hello\nworld\t!""#;
        let doc = parse_toml_full(input).expect("should succeed");
        assert_eq!(
            doc.get("msg"),
            Some(&TomlValue::Str("hello\nworld\t!".to_string()))
        );
    }

    #[test]
    fn test_empty_inline_table() {
        let input = "empty = {}\n";
        let doc = parse_toml_full(input).expect("should succeed");
        if let Some(TomlValue::Table(t)) = doc.get("empty") {
            assert!(t.is_empty());
        } else {
            panic!("expected empty table");
        }
    }

    #[test]
    fn test_mixed_doc() {
        let input = r#"
title = "Test"

[owner]
name = "Tom"
age = 30

[database]
ports = [8001, 8001, 8002]
enabled = true
"#;
        let doc = parse_toml_full(input).expect("should succeed");
        assert_eq!(doc.get("title"), Some(&TomlValue::Str("Test".to_string())));
        assert_eq!(
            doc.get("owner.name"),
            Some(&TomlValue::Str("Tom".to_string()))
        );
        assert_eq!(doc.get("owner.age"), Some(&TomlValue::Int(30)));
        assert_eq!(doc.get("database.enabled"), Some(&TomlValue::Bool(true)));
        if let Some(TomlValue::Array(ports)) = doc.get("database.ports") {
            assert_eq!(ports.len(), 3);
        } else {
            panic!("expected ports array");
        }
    }

    #[test]
    fn test_toml_table_get_path() {
        let mut root = TomlTable::new();
        let mut sub = TomlTable::new();
        sub.insert("key", TomlValue::Int(42));
        root.insert("section", TomlValue::Table(sub));
        assert_eq!(root.get_path("section.key"), Some(&TomlValue::Int(42)));
    }

    #[test]
    fn test_negative_integer() {
        let input = "temp = -40\n";
        let doc = parse_toml_full(input).expect("should succeed");
        assert_eq!(doc.get("temp"), Some(&TomlValue::Int(-40)));
    }

    #[test]
    fn test_positive_float() {
        let input = "val = +3.14\n";
        let doc = parse_toml_full(input).expect("should succeed");
        if let Some(TomlValue::Float(f)) = doc.get("val") {
            assert!((f - 3.14).abs() < 1e-9);
        } else {
            panic!("expected float");
        }
    }

    #[test]
    fn test_date_only() {
        let input = "day = 2024-06-15\n";
        let doc = parse_toml_full(input).expect("should succeed");
        assert_eq!(
            doc.get("day"),
            Some(&TomlValue::DateTime("2024-06-15".to_string()))
        );
    }

    #[test]
    fn test_quoted_key() {
        let input = r#""weird key" = 1"#;
        let doc = parse_toml_full(input).expect("should succeed");
        assert_eq!(doc.get("weird key"), Some(&TomlValue::Int(1)));
    }

    #[test]
    fn test_inline_comment_after_value() {
        let input = "x = 42 # this is a comment\n";
        let doc = parse_toml_full(input).expect("should succeed");
        assert_eq!(doc.get("x"), Some(&TomlValue::Int(42)));
    }
}