dotscope 0.6.0

A high-performance, cross-platform framework for analyzing and reverse engineering .NET PE executables
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
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
//! Resource type definitions and parsing for .NET resource files.
//!
//! This module provides comprehensive support for parsing and representing the various data types
//! that can be stored in .NET resource files (.resources). It implements the complete type system
//! defined by the .NET resource format specification, including primitive types, special types,
//! and user-defined types.
//!
//! # .NET Resource Type System
//!
//! The .NET resource format supports a hierarchical type system:
//! - **Primitive Types (0x00-0x1F)**: Built-in .NET value types like integers, strings, booleans
//! - **Special Types (0x20-0x3F)**: Complex types with special serialization like byte arrays
//! - **User Types (0x40+)**: Custom types serialized using the binary formatter
//!
//! # Magic Number
//!
//! All .NET resource files begin with the magic number `0xBEEFCACE` to identify the format.
//!
//! # Examples
//!
//! ## Basic Type Parsing
//!
//! ```ignore
//! use dotscope::metadata::resources::types::{ResourceType, RESOURCE_MAGIC};
//! use dotscope::file::parser::Parser;
//!
//! // Parse a resource file header
//! let mut parser = Parser::new(&data);
//! let magic = parser.read_le::<u32>()?;
//! assert_eq!(magic, RESOURCE_MAGIC);
//!
//! // Parse a type from type byte
//! let type_byte = parser.read_le::<u8>()?;
//! let resource_type = ResourceType::from_type_byte(type_byte, &mut parser)?;
//!
//! match resource_type {
//!     ResourceType::String(s) => println!("Found string: {}", s),
//!     ResourceType::Int32(i) => println!("Found integer: {}", i),
//!     ResourceType::ByteArray(bytes) => println!("Found byte array: {} bytes", bytes.len()),
//!     _ => println!("Found other type"),
//! }
//! ```
//!
//! ## Type Name Resolution
//!
//! ```ignore
//! use dotscope::metadata::resources::types::ResourceType;
//! use dotscope::file::parser::Parser;
//!
//! let mut parser = Parser::new(&data);
//!
//! // Parse using type name instead of type byte
//! let resource_type = ResourceType::from_type_name("System.String", &mut parser)?;
//! if let ResourceType::String(s) = resource_type {
//!     println!("Parsed string from type name: {}", s);
//! }
//! ```
//!
//! # Thread Safety
//!
//! All types in this module are thread-safe:
//! - [`ResourceType`] implements `Send + Sync` for safe sharing across threads
//! - Parsing operations are stateless and can be performed concurrently
//! - No global state is maintained during parsing operations

/// The magic number that identifies a .NET resource file (0xBEEFCACE)
pub const RESOURCE_MAGIC: u32 = 0xBEEF_CACE;

use crate::{file::parser::Parser, utils::compressed_uint_size, Error::TypeError, Result};

/// Represents all data types that can be stored in .NET resource files.
///
/// This enum provides a complete representation of the type system used in .NET resource files,
/// including all primitive types, special collection types, and extensibility for user-defined types.
/// Each variant corresponds to specific type codes defined in the .NET resource format specification.
///
/// # Type Code Ranges
///
/// - **0x00-0x1F**: Primitive and built-in types (null, strings, numbers, dates)
/// - **0x20-0x3F**: Special types with custom serialization (byte arrays, streams)
/// - **0x40+**: User-defined types serialized with the binary formatter
///
/// # Examples
///
/// ```ignore
/// use dotscope::metadata::resources::types::ResourceType;
/// use dotscope::file::parser::Parser;
///
/// // Parse different resource types
/// let mut parser = Parser::new(&data);
///
/// // Parse a string resource (type code 0x01)
/// let string_resource = ResourceType::from_type_byte(0x01, &mut parser)?;
/// if let ResourceType::String(s) = string_resource {
///     println!("String resource: {}", s);
/// }
///
/// // Parse an integer resource (type code 0x08)
/// let int_resource = ResourceType::from_type_byte(0x08, &mut parser)?;
/// if let ResourceType::Int32(i) = int_resource {
///     println!("Integer resource: {}", i);
/// }
///
/// // Parse a byte array resource (type code 0x20)
/// let bytes_resource = ResourceType::from_type_byte(0x20, &mut parser)?;
/// if let ResourceType::ByteArray(bytes) = bytes_resource {
///     println!("Byte array: {} bytes", bytes.len());
/// }
/// ```
///
/// # Thread Safety
///
/// All variants are thread-safe and can be safely shared across threads without synchronization.
#[derive(Debug, Clone, PartialEq)]
pub enum ResourceType {
    /// Null resource value (type code 0x00)
    Null,
    /// UTF-8 string resource with length prefix (type code 0x01)
    String(String),
    /// Boolean resource value, false=0, true=non-zero (type code 0x02)
    Boolean(bool),
    /// Single character resource stored as UTF-16 code unit (type code 0x03)
    Char(char),
    /// Unsigned 8-bit integer resource (type code 0x04)
    Byte(u8),
    /// Signed 8-bit integer resource (type code 0x05)
    SByte(i8),
    /// Signed 16-bit integer resource, little-endian (type code 0x06)
    Int16(i16),
    /// Unsigned 16-bit integer resource, little-endian (type code 0x07)
    UInt16(u16),
    /// Signed 32-bit integer resource, little-endian (type code 0x08)
    Int32(i32),
    /// Unsigned 32-bit integer resource, little-endian (type code 0x09)
    UInt32(u32),
    /// Signed 64-bit integer resource, little-endian (type code 0x0A)
    Int64(i64),
    /// Unsigned 64-bit integer resource, little-endian (type code 0x0B)
    UInt64(u64),
    /// 32-bit floating point resource, little-endian (type code 0x0C)
    Single(f32),
    /// 64-bit floating point resource, little-endian (type code 0x0D)
    Double(f64),
    /// .NET Decimal resource value stored as raw bits (type code 0x0E)
    ///
    /// Represents a 128-bit decimal number using the same binary format as .NET's
    /// `System.Decimal`. The four 32-bit integers represent:
    /// - `lo`: Low 32 bits of the 96-bit mantissa
    /// - `mid`: Middle 32 bits of the 96-bit mantissa
    /// - `hi`: High 32 bits of the 96-bit mantissa
    /// - `flags`: Sign (bit 31) and scale (bits 16-23, valid range 0-28)
    ///
    /// # Binary Format
    ///
    /// The decimal is stored as 16 bytes (4 consecutive little-endian i32 values):
    /// ```text
    /// Bytes 0-3:   lo    (low 32 bits of mantissa)
    /// Bytes 4-7:   mid   (middle 32 bits of mantissa)
    /// Bytes 8-11:  hi    (high 32 bits of mantissa)
    /// Bytes 12-15: flags (sign in bit 31, scale in bits 16-23)
    /// ```
    ///
    /// # Example
    ///
    /// The value `3.261` would be represented as mantissa `3261` with scale `3`.
    Decimal {
        /// Low 32 bits of the 96-bit mantissa
        lo: i32,
        /// Middle 32 bits of the 96-bit mantissa
        mid: i32,
        /// High 32 bits of the 96-bit mantissa
        hi: i32,
        /// Flags containing sign (bit 31) and scale (bits 16-23)
        flags: i32,
    },
    /// .NET DateTime resource value stored as binary ticks (type code 0x0F)
    ///
    /// Represents a point in time using the same binary format as .NET's
    /// `System.DateTime.ToBinary()`. The 64-bit value encodes both the ticks
    /// (100-nanosecond intervals since 12:00 midnight, January 1, 0001 CE)
    /// and the `DateTimeKind` (UTC, Local, or Unspecified).
    ///
    /// # Binary Format
    ///
    /// The value is stored as a single little-endian i64:
    /// - Bits 0-61: Ticks (number of 100-nanosecond intervals)
    /// - Bits 62-63: DateTimeKind (0=Unspecified, 1=UTC, 2=Local)
    ///
    /// # Conversion
    ///
    /// To extract the components:
    /// ```text
    /// ticks = binary_value & 0x3FFFFFFFFFFFFFFF
    /// kind  = (binary_value >> 62) & 0x3
    /// ```
    ///
    /// To reconstruct a .NET DateTime, use `DateTime.FromBinary(binary_value)`.
    DateTime(i64),
    /// .NET TimeSpan resource value stored as ticks (type code 0x10)
    ///
    /// Represents a time interval using the same format as .NET's
    /// `System.TimeSpan.Ticks`. The 64-bit signed value represents the number
    /// of 100-nanosecond intervals in the time span.
    ///
    /// # Binary Format
    ///
    /// The value is stored as a single little-endian i64 representing ticks.
    /// Negative values represent negative time spans.
    ///
    /// # Conversion Examples
    ///
    /// Common conversions:
    /// ```text
    /// 1 tick         = 100 nanoseconds
    /// 10,000 ticks   = 1 millisecond
    /// 10,000,000 ticks = 1 second
    /// 600,000,000 ticks = 1 minute
    /// 36,000,000,000 ticks = 1 hour
    /// ```
    ///
    /// To convert to seconds: `seconds = ticks / 10_000_000`
    TimeSpan(i64),
    /// Byte array resource with length prefix (type code 0x20)
    ByteArray(Vec<u8>),
    /// Stream resource with length prefix (type code 0x21)
    Stream(Vec<u8>),
    /// Marker for the beginning of user-defined types (type code 0x40+)
    StartOfUserTypes,
}

impl ResourceType {
    /// Returns the .NET type name for this resource type.
    ///
    /// Provides the canonical .NET Framework type name that corresponds to this
    /// resource type. This is used for .NET resource file format encoding and
    /// type resolution during resource serialization.
    ///
    /// # Returns
    ///
    /// Returns the .NET type name as a string slice, or `None` for types that
    /// don't have a corresponding .NET type name (like `Null` or unimplemented types).
    ///
    /// # Examples
    ///
    /// ```
    /// use dotscope::metadata::resources::ResourceType;
    ///
    /// let string_type = ResourceType::String("hello".to_string());
    /// assert_eq!(string_type.as_str(), Some("System.String"));
    ///
    /// let int_type = ResourceType::Int32(42);
    /// assert_eq!(int_type.as_str(), Some("System.Int32"));
    ///
    /// let null_type = ResourceType::Null;
    /// assert_eq!(null_type.as_str(), None);
    /// ```
    #[must_use]
    pub fn as_str(&self) -> Option<&'static str> {
        match self {
            ResourceType::String(_) => Some("System.String"),
            ResourceType::Boolean(_) => Some("System.Boolean"),
            ResourceType::Char(_) => Some("System.Char"),
            ResourceType::Byte(_) => Some("System.Byte"),
            ResourceType::SByte(_) => Some("System.SByte"),
            ResourceType::Int16(_) => Some("System.Int16"),
            ResourceType::UInt16(_) => Some("System.UInt16"),
            ResourceType::Int32(_) => Some("System.Int32"),
            ResourceType::UInt32(_) => Some("System.UInt32"),
            ResourceType::Int64(_) => Some("System.Int64"),
            ResourceType::UInt64(_) => Some("System.UInt64"),
            ResourceType::Single(_) => Some("System.Single"),
            ResourceType::Double(_) => Some("System.Double"),
            ResourceType::Decimal { .. } => Some("System.Decimal"),
            ResourceType::DateTime(_) => Some("System.DateTime"),
            ResourceType::TimeSpan(_) => Some("System.TimeSpan"),
            ResourceType::ByteArray(_) => Some("System.Byte[]"),
            ResourceType::Stream(_) => Some("System.IO.Stream"),
            // Types without .NET equivalents
            ResourceType::Null | ResourceType::StartOfUserTypes => None,
        }
    }

    /// Returns the hard-coded type index for this resource type.
    ///
    /// Provides the index that this resource type should have in .NET resource file
    /// type tables. This method returns constant indices that match the standard
    /// .NET resource file type ordering, providing O(1) constant-time access without
    /// needing HashMap lookups.
    ///
    /// The indices correspond to the standard ordering used in .NET resource files:
    /// - Boolean: 0
    /// - Byte: 1  
    /// - SByte: 2
    /// - Char: 3
    /// - Int16: 4
    /// - UInt16: 5
    /// - Int32: 6
    /// - UInt32: 7
    /// - Int64: 8
    /// - UInt64: 9
    /// - Single: 10
    /// - Double: 11
    /// - String: 12
    /// - ByteArray: 13
    ///
    /// # Returns
    ///
    /// Returns the type index as a `u32`, or `None` for types that don't have
    /// a corresponding index in the standard .NET resource type table.
    ///
    /// # Examples
    ///
    /// ```
    /// use dotscope::metadata::resources::ResourceType;
    ///
    /// let string_type = ResourceType::String("hello".to_string());
    /// assert_eq!(string_type.index(), Some(12));
    ///
    /// let int_type = ResourceType::Int32(42);
    /// assert_eq!(int_type.index(), Some(6));
    ///
    /// let null_type = ResourceType::Null;
    /// assert_eq!(null_type.index(), None);
    /// ```
    #[must_use]
    pub fn index(&self) -> Option<u32> {
        match self {
            ResourceType::Boolean(_) => Some(0),
            ResourceType::Byte(_) => Some(1),
            ResourceType::SByte(_) => Some(2),
            ResourceType::Char(_) => Some(3),
            ResourceType::Int16(_) => Some(4),
            ResourceType::UInt16(_) => Some(5),
            ResourceType::Int32(_) => Some(6),
            ResourceType::UInt32(_) => Some(7),
            ResourceType::Int64(_) => Some(8),
            ResourceType::UInt64(_) => Some(9),
            ResourceType::Single(_) => Some(10),
            ResourceType::Double(_) => Some(11),
            ResourceType::String(_) => Some(12),
            ResourceType::Decimal { .. } => Some(13),
            ResourceType::DateTime(_) => Some(14),
            ResourceType::TimeSpan(_) => Some(15),
            ResourceType::ByteArray(_) => Some(16),
            ResourceType::Stream(_) => Some(17),
            // Types without .NET equivalents
            ResourceType::Null | ResourceType::StartOfUserTypes => None,
        }
    }

    /// Returns the official .NET type code for this resource type for encoding.
    ///
    /// This method returns the official .NET type code that should be used when encoding
    /// this resource type in .NET resource format files. These codes match the official
    /// ResourceTypeCode enumeration from the .NET runtime.
    ///
    /// # Returns
    ///
    /// - `Some(type_code)` for supported .NET resource types
    /// - `None` for types that don't have direct .NET equivalents or are not yet implemented
    ///
    /// # Official .NET Type Code Mapping
    ///
    /// The returned codes map to the official .NET ResourceTypeCode enumeration:
    /// - 0x01: String
    /// - 0x02: Boolean
    /// - 0x03: Char
    /// - 0x04: Byte  
    /// - 0x05: SByte
    /// - 0x06: Int16
    /// - 0x07: UInt16
    /// - 0x08: Int32
    /// - 0x09: UInt32
    /// - 0x0A: Int64
    /// - 0x0B: UInt64
    /// - 0x0C: Single
    /// - 0x0D: Double
    /// - 0x0E: Decimal
    /// - 0x0F: DateTime
    /// - 0x10: TimeSpan
    /// - 0x20: ByteArray
    /// - 0x21: Stream
    ///
    /// # Examples
    ///
    /// ```
    /// use dotscope::metadata::resources::ResourceType;
    ///
    /// let string_type = ResourceType::String("Hello".to_string());
    /// assert_eq!(string_type.type_code(), Some(0x01));
    ///
    /// let int_type = ResourceType::Int32(42);
    /// assert_eq!(int_type.type_code(), Some(0x08));
    ///
    /// let null_type = ResourceType::Null;
    /// assert_eq!(null_type.type_code(), None); // No .NET equivalent
    /// ```
    #[must_use]
    pub fn type_code(&self) -> Option<u32> {
        match self {
            ResourceType::String(_) => Some(0x01),
            ResourceType::Boolean(_) => Some(0x02),
            ResourceType::Char(_) => Some(0x03),
            ResourceType::Byte(_) => Some(0x04),
            ResourceType::SByte(_) => Some(0x05),
            ResourceType::Int16(_) => Some(0x06),
            ResourceType::UInt16(_) => Some(0x07),
            ResourceType::Int32(_) => Some(0x08),
            ResourceType::UInt32(_) => Some(0x09),
            ResourceType::Int64(_) => Some(0x0A),
            ResourceType::UInt64(_) => Some(0x0B),
            ResourceType::Single(_) => Some(0x0C),
            ResourceType::Double(_) => Some(0x0D),
            ResourceType::Decimal { .. } => Some(0x0E),
            ResourceType::DateTime(_) => Some(0x0F),
            ResourceType::TimeSpan(_) => Some(0x10),
            ResourceType::ByteArray(_) => Some(0x20),
            ResourceType::Stream(_) => Some(0x21),
            // Types without .NET equivalents
            ResourceType::Null | ResourceType::StartOfUserTypes => None,
        }
    }

    /// Returns the size in bytes that this resource's data will occupy when encoded.
    ///
    /// Calculates the exact number of bytes this resource will take when written
    /// in .NET resource file format, including length prefixes for variable-length
    /// data but excluding the type index.
    ///
    /// # Returns
    ///
    /// Returns the data size in bytes, or `None` for types that are not yet
    /// implemented or cannot be encoded.
    ///
    /// # Examples
    ///
    /// ```
    /// use dotscope::metadata::resources::ResourceType;
    ///
    /// let string_type = ResourceType::String("hello".to_string());
    /// assert_eq!(string_type.data_size(), Some(6)); // 1 byte length + 5 bytes UTF-8
    ///
    /// let int_type = ResourceType::Int32(42);
    /// assert_eq!(int_type.data_size(), Some(4)); // 4 bytes for i32
    ///
    /// let bool_type = ResourceType::Boolean(true);
    /// assert_eq!(bool_type.data_size(), Some(1)); // 1 byte for boolean
    ///
    /// let bytes_type = ResourceType::ByteArray(vec![1, 2, 3]);
    /// assert_eq!(bytes_type.data_size(), Some(7)); // 4 bytes LE length + 3 bytes data
    /// ```
    #[must_use]
    pub fn data_size(&self) -> Option<u32> {
        match self {
            ResourceType::String(s) => {
                // UTF-8 byte length (7-bit encoded) + UTF-8 bytes
                let utf8_byte_count = s.len();
                let utf8_size = u32::try_from(utf8_byte_count).ok()?;
                let prefix_size = u32::try_from(compressed_uint_size(utf8_size as usize)).ok()?;
                Some(prefix_size + utf8_size)
            }
            ResourceType::Boolean(_) | ResourceType::Byte(_) | ResourceType::SByte(_) => Some(1), // Single byte
            ResourceType::Char(_) | ResourceType::Int16(_) | ResourceType::UInt16(_) => Some(2), // 2 bytes
            ResourceType::Int32(_) | ResourceType::UInt32(_) | ResourceType::Single(_) => Some(4), // 4 bytes
            ResourceType::Int64(_)
            | ResourceType::UInt64(_)
            | ResourceType::Double(_)
            | ResourceType::DateTime(_)  // i64 binary ticks
            | ResourceType::TimeSpan(_)  // i64 ticks
            => Some(8), // 8 bytes
            ResourceType::Decimal { .. } => Some(16), // 4 × i32 = 16 bytes
            ResourceType::ByteArray(data) | ResourceType::Stream(data) => {
                // Length (4-byte LE integer) + data bytes
                // Per .NET specification: ByteArray and Stream use fixed 4-byte LE length
                // Note: Type code is NOT included here - encoder adds type_code_size separately
                let data_size = u32::try_from(data.len()).ok()?;
                Some(4 + data_size)
            }
            // Types without .NET equivalents
            ResourceType::Null | ResourceType::StartOfUserTypes => None,
        }
    }

    /// Parses a resource type from its binary type code.
    ///
    /// This method reads a resource value from the parser based on the provided type byte,
    /// which corresponds to the type codes defined in the .NET resource format specification.
    /// Each type code indicates both the data type and how to parse the following bytes.
    ///
    /// # Arguments
    ///
    /// * `byte` - The type code byte (0x00-0xFF) that identifies the data type
    /// * `parser` - A mutable reference to the parser positioned after the type byte
    ///
    /// # Returns
    ///
    /// Returns a [`crate::Result<ResourceType>`] containing the parsed resource value,
    /// or an error if the type code is unsupported or parsing fails.
    ///
    /// # Supported Type Codes
    ///
    /// - `0x01`: UTF-8 string with length prefix
    /// - `0x02`: Boolean value (0 = false, non-zero = true)
    /// - `0x03`: Single character as byte
    /// - `0x04`: Unsigned 8-bit integer
    /// - `0x05`: Signed 8-bit integer
    /// - `0x06`: Signed 16-bit integer (little-endian)
    /// - `0x07`: Unsigned 16-bit integer (little-endian)
    /// - `0x08`: Signed 32-bit integer (little-endian)
    /// - `0x09`: Unsigned 32-bit integer (little-endian)
    /// - `0x0A`: Signed 64-bit integer (little-endian)
    /// - `0x0B`: Unsigned 64-bit integer (little-endian)
    /// - `0x0C`: 32-bit floating point (little-endian)
    /// - `0x0D`: 64-bit floating point (little-endian)
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use dotscope::metadata::resources::types::ResourceType;
    /// use dotscope::file::parser::Parser;
    ///
    /// let mut parser = Parser::new(&data);
    ///
    /// // Parse a string resource (type code 0x01)
    /// let string_type = ResourceType::from_type_byte(0x01, &mut parser)?;
    /// if let ResourceType::String(s) = string_type {
    ///     println!("Found string: {}", s);
    /// }
    ///
    /// // Parse an integer resource (type code 0x08)
    /// let int_type = ResourceType::from_type_byte(0x08, &mut parser)?;
    /// if let ResourceType::Int32(value) = int_type {
    ///     println!("Found integer: {}", value);
    /// }
    /// ```
    ///
    /// # Errors
    ///
    /// - [`crate::Error::TypeError`]: If the type byte is not supported
    /// - Parser errors: If reading the underlying data fails (e.g., truncated data)
    pub fn from_type_byte(byte: u8, parser: &mut Parser) -> Result<Self> {
        match byte {
            0x0 => {
                // ResourceTypeCode.Null - no data to read
                Ok(ResourceType::Null)
            }
            0x1 => {
                // .NET string resources use UTF-8 encoding with 7-bit encoded byte length prefix
                // (Resource names use UTF-16, but string DATA values use UTF-8)
                Ok(ResourceType::String(parser.read_prefixed_string_utf8()?))
            }
            0x2 => Ok(ResourceType::Boolean(parser.read_le::<u8>()? > 0)),
            0x3 => {
                // ResourceTypeCode.Char - 2 bytes (UInt16)
                // .NET writes chars as UInt16 (UTF-16 code unit), not as single byte
                // See: ResourceWriter.WriteValue writes `(UInt16)(char)value`
                //      ResourceReader._LoadObjectV2 reads `(char)_store.ReadUInt16()`
                let code_unit = parser.read_le::<u16>()?;
                Ok(ResourceType::Char(
                    char::from_u32(u32::from(code_unit)).ok_or_else(|| {
                        TypeError("Invalid UTF-16 code unit for Char".to_string())
                    })?,
                ))
            }
            0x4 => Ok(ResourceType::Byte(parser.read_le::<u8>()?)),
            0x5 => Ok(ResourceType::SByte(parser.read_le::<i8>()?)),
            0x6 => Ok(ResourceType::Int16(parser.read_le::<i16>()?)),
            0x7 => Ok(ResourceType::UInt16(parser.read_le::<u16>()?)),
            0x8 => Ok(ResourceType::Int32(parser.read_le::<i32>()?)),
            0x9 => Ok(ResourceType::UInt32(parser.read_le::<u32>()?)),
            0xA => Ok(ResourceType::Int64(parser.read_le::<i64>()?)),
            0xB => Ok(ResourceType::UInt64(parser.read_le::<u64>()?)),
            0xC => Ok(ResourceType::Single(parser.read_le::<f32>()?)),
            0xD => Ok(ResourceType::Double(parser.read_le::<f64>()?)),
            0xE => {
                // ResourceTypeCode.Decimal - 16 bytes (4 × i32)
                // Format: lo, mid, hi, flags (all little-endian i32)
                let lo = parser.read_le::<i32>()?;
                let mid = parser.read_le::<i32>()?;
                let hi = parser.read_le::<i32>()?;
                let flags = parser.read_le::<i32>()?;
                Ok(ResourceType::Decimal { lo, mid, hi, flags })
            }
            0xF => {
                // ResourceTypeCode.DateTime - 8 bytes (i64 binary format)
                // Contains ticks (bits 0-61) and DateTimeKind (bits 62-63)
                let binary_value = parser.read_le::<i64>()?;
                Ok(ResourceType::DateTime(binary_value))
            }
            0x10 => {
                // ResourceTypeCode.TimeSpan - 8 bytes (i64 ticks)
                // Ticks represent 100-nanosecond intervals
                let ticks = parser.read_le::<i64>()?;
                Ok(ResourceType::TimeSpan(ticks))
            }
            0x20 => {
                let length = parser.read_le::<u32>()?;
                let start_pos = parser.pos();
                let end_pos = start_pos + length as usize;

                if end_pos > parser.data().len() {
                    return Err(out_of_bounds_error!());
                }

                let data = parser.data()[start_pos..end_pos].to_vec();
                if end_pos < parser.data().len() {
                    parser.seek(end_pos)?;
                }
                Ok(ResourceType::ByteArray(data))
            }
            0x21 => {
                let length = parser.read_le::<u32>()?;
                let start_pos = parser.pos();
                let end_pos = start_pos + length as usize;

                if end_pos > parser.data().len() {
                    return Err(out_of_bounds_error!());
                }

                let data = parser.data()[start_pos..end_pos].to_vec();
                if end_pos < parser.data().len() {
                    parser.seek(end_pos)?;
                }
                // Stream uses same format as ByteArray, just different type code
                Ok(ResourceType::Stream(data))
            }
            0x40..=0xFF => {
                // User types - these require a type table for resolution
                // According to .NET ResourceReader, if we have user types but no type table,
                // this is a BadImageFormat error
                Err(TypeError(format!(
                    "TypeByte - {byte:X} is a user type (>=0x40) but requires type table resolution which is not yet implemented"
                )))
            }
            _ => Err(TypeError(format!(
                "TypeByte - {byte:X} is currently not supported"
            ))),
        }
    }

    /// Parses a resource type from its .NET type name.
    ///
    /// This method provides an alternative parsing mechanism that uses .NET type names instead
    /// of type bytes. It maps common .NET Framework type names to their corresponding binary
    /// representations and delegates to [`Self::from_type_byte`] for the actual parsing.
    ///
    /// This approach is commonly used in resource files that store type information as strings
    /// rather than numeric type codes, particularly in older .NET resource formats.
    ///
    /// # Arguments
    ///
    /// * `type_name` - The fully qualified .NET type name (e.g., "System.String")
    /// * `parser` - A mutable reference to the parser positioned at the resource value
    ///
    /// # Returns
    ///
    /// Returns a [`crate::Result<ResourceType>`] containing the parsed resource value,
    /// or an error if the type name is unsupported or parsing fails.
    ///
    /// # Supported Type Names
    ///
    /// - `"System.Null"`: Null value
    /// - `"System.String"`: UTF-8 string with length prefix
    /// - `"System.Boolean"`: Boolean value
    /// - `"System.Char"`: Single character
    /// - `"System.Byte"`: Unsigned 8-bit integer
    /// - `"System.SByte"`: Signed 8-bit integer
    /// - `"System.Int16"`: Signed 16-bit integer
    /// - `"System.UInt16"`: Unsigned 16-bit integer
    /// - `"System.Int32"`: Signed 32-bit integer
    /// - `"System.UInt32"`: Unsigned 32-bit integer
    /// - `"System.Int64"`: Signed 64-bit integer
    /// - `"System.UInt64"`: Unsigned 64-bit integer
    /// - `"System.Single"`: 32-bit floating point
    /// - `"System.Double"`: 64-bit floating point
    /// - `"System.Byte[]"`: Byte array
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use dotscope::metadata::resources::types::ResourceType;
    /// use dotscope::file::parser::Parser;
    ///
    /// let mut parser = Parser::new(&data);
    ///
    /// // Parse using .NET type names
    /// let string_resource = ResourceType::from_type_name("System.String", &mut parser)?;
    /// if let ResourceType::String(s) = string_resource {
    ///     println!("String resource: {}", s);
    /// }
    ///
    /// let int_resource = ResourceType::from_type_name("System.Int32", &mut parser)?;
    /// if let ResourceType::Int32(value) = int_resource {
    ///     println!("Integer resource: {}", value);
    /// }
    ///
    /// let bytes_resource = ResourceType::from_type_name("System.Byte[]", &mut parser)?;
    /// if let ResourceType::ByteArray(bytes) = bytes_resource {
    ///     println!("Byte array: {} bytes", bytes.len());
    /// }
    /// ```
    ///
    /// # Errors
    ///
    /// - [`crate::Error::TypeError`]: If the type name is not supported
    /// - Parser errors: If reading the underlying data fails
    pub fn from_type_name(type_name: &str, parser: &mut Parser) -> Result<Self> {
        match type_name {
            "System.Null" => ResourceType::from_type_byte(0x0, parser),
            "System.String" => ResourceType::from_type_byte(0x1, parser),
            "System.Boolean" => ResourceType::from_type_byte(0x2, parser),
            "System.Char" => ResourceType::from_type_byte(0x3, parser),
            "System.Byte" => ResourceType::from_type_byte(0x4, parser),
            "System.SByte" => ResourceType::from_type_byte(0x5, parser),
            "System.Int16" => ResourceType::from_type_byte(0x6, parser),
            "System.UInt16" => ResourceType::from_type_byte(0x7, parser),
            "System.Int32" => ResourceType::from_type_byte(0x8, parser),
            "System.UInt32" => ResourceType::from_type_byte(0x9, parser),
            "System.Int64" => ResourceType::from_type_byte(0xA, parser),
            "System.UInt64" => ResourceType::from_type_byte(0xB, parser),
            "System.Single" => ResourceType::from_type_byte(0xC, parser),
            "System.Double" => ResourceType::from_type_byte(0xD, parser),
            "System.Decimal" => ResourceType::from_type_byte(0xE, parser),
            "System.DateTime" => ResourceType::from_type_byte(0xF, parser),
            "System.TimeSpan" => ResourceType::from_type_byte(0x10, parser),
            "System.Byte[]" => ResourceType::from_type_byte(0x20, parser),
            "System.IO.Stream" => ResourceType::from_type_byte(0x21, parser),
            _ => Err(TypeError(format!(
                "TypeName - {type_name} is currently not supported"
            ))),
        }
    }
}

/// A parsed .NET resource entry with owned data.
///
/// This structure contains owned copies of resource data. For zero-copy access to large
/// resources (like embedded ZIP archives), see [`ResourceEntryRef`].
pub struct ResourceEntry {
    /// The name of the resource
    pub name: String,
    /// The hash of the name
    pub name_hash: u32,
    /// The parsed resource
    pub data: ResourceType,
}

/// Zero-copy variant of [`ResourceType`] that borrows data instead of copying.
///
/// This enum is identical to [`ResourceType`] except that `String` and `ByteArray` variants
/// hold borrowed references instead of owned data. This enables efficient access to large
/// embedded resources (like ZIP archives) without allocating copies that could be hundreds
/// of megabytes or gigabytes.
///
/// # Lifetime
///
/// The lifetime parameter `'a` represents the lifetime of the source data buffer. All borrowed
/// data (strings and byte arrays) will remain valid for this lifetime.
///
/// # Usage
///
/// Use this type when:
/// - Working with large embedded resources (e.g., ZIP archives, large binary data)
/// - Memory-mapping resource files for efficient access
/// - Avoiding unnecessary allocations for performance-critical code
///
/// Use [`ResourceType`] (the owned variant) when:
/// - You need to store resources beyond the lifetime of the source buffer
/// - Working with small resources where copying overhead is negligible
/// - You prefer simpler APIs without lifetime parameters
///
/// # Examples
///
/// ```ignore
/// use dotscope::metadata::resources::parse_dotnet_resource_ref;
///
/// let resource_data = std::fs::read("MyApp.resources")?;
/// let resources = parse_dotnet_resource_ref(&resource_data)?;
///
/// for (name, entry) in &resources {
///     match &entry.data {
///         ResourceTypeRef::ByteArray(bytes) => {
///             // Zero-copy access to potentially large byte array
///             println!("Resource '{}': {} bytes (no copy!)", name, bytes.len());
///
///             // Can pass directly to functions expecting &[u8]
///             process_zip_archive(bytes)?;
///         }
///         ResourceTypeRef::String(s) => {
///             // Zero-copy access to string data
///             println!("String resource: {}", s);
///         }
///         _ => {}
///     }
/// }
/// # Ok::<(), dotscope::Error>(())
/// ```
///
/// # Thread Safety
///
/// All variants are thread-safe and can be safely shared across threads without synchronization,
/// as long as the underlying data buffer remains valid.
#[derive(Debug, Clone, PartialEq)]
pub enum ResourceTypeRef<'a> {
    /// Null resource value (type code 0x00)
    Null,
    /// UTF-8 string resource with borrowed data (type code 0x01)
    String(&'a str),
    /// Boolean resource value, false=0, true=non-zero (type code 0x02)
    Boolean(bool),
    /// Single character resource stored as UTF-16 code unit (type code 0x03)
    Char(char),
    /// Unsigned 8-bit integer resource (type code 0x04)
    Byte(u8),
    /// Signed 8-bit integer resource (type code 0x05)
    SByte(i8),
    /// Signed 16-bit integer resource, little-endian (type code 0x06)
    Int16(i16),
    /// Unsigned 16-bit integer resource, little-endian (type code 0x07)
    UInt16(u16),
    /// Signed 32-bit integer resource, little-endian (type code 0x08)
    Int32(i32),
    /// Unsigned 32-bit integer resource, little-endian (type code 0x09)
    UInt32(u32),
    /// Signed 64-bit integer resource, little-endian (type code 0x0A)
    Int64(i64),
    /// Unsigned 64-bit integer resource, little-endian (type code 0x0B)
    UInt64(u64),
    /// 32-bit floating point resource, little-endian (type code 0x0C)
    Single(f32),
    /// 64-bit floating point resource, little-endian (type code 0x0D)
    Double(f64),
    /// .NET Decimal resource value stored as raw bits (type code 0x0E)
    ///
    /// See [`ResourceType::Decimal`] for detailed documentation on the binary format.
    Decimal {
        /// Low 32 bits of the 96-bit mantissa
        lo: i32,
        /// Middle 32 bits of the 96-bit mantissa
        mid: i32,
        /// High 32 bits of the 96-bit mantissa
        hi: i32,
        /// Flags containing sign (bit 31) and scale (bits 16-23)
        flags: i32,
    },
    /// .NET DateTime resource value stored as binary ticks (type code 0x0F)
    ///
    /// See [`ResourceType::DateTime`] for detailed documentation on the binary format.
    DateTime(i64),
    /// .NET TimeSpan resource value stored as ticks (type code 0x10)
    ///
    /// See [`ResourceType::TimeSpan`] for detailed documentation on the binary format.
    TimeSpan(i64),
    /// Byte array resource with borrowed data (type code 0x20)
    ByteArray(&'a [u8]),
    /// Stream resource with borrowed data (type code 0x21)
    Stream(&'a [u8]),
    /// Marker for the beginning of user-defined types (type code 0x40+)
    StartOfUserTypes,
}

/// A parsed .NET resource entry with borrowed data for zero-copy access.
///
/// This is the zero-copy variant of [`ResourceEntry`] that borrows resource data instead of
/// copying it. Use this when working with large embedded resources to avoid allocating
/// potentially hundreds of megabytes or gigabytes of memory.
///
/// # Lifetime
///
/// The lifetime parameter `'a` represents the lifetime of the source resource data buffer.
/// The resource data will remain valid for this lifetime.
///
/// # Examples
///
/// ```ignore
/// use dotscope::metadata::resources::Resource;
///
/// let resource_data = std::fs::read("MyApp.resources")?;
/// let mut resource = Resource::parse(&resource_data)?;
/// let resources = resource.read_resources_ref(&resource_data)?;
///
/// for (name, entry) in &resources {
///     println!("Resource: {} (Hash: 0x{:08X})", name, entry.name_hash);
///
///     match &entry.data {
///         ResourceTypeRef::ByteArray(bytes) => {
///             // Extract embedded ZIP without copying
///             if bytes.starts_with(b"PK\x03\x04") {
///                 println!("  Found ZIP archive: {} bytes", bytes.len());
///                 extract_zip(bytes)?;
///             }
///         }
///         ResourceTypeRef::String(s) => {
///             println!("  String: '{}'", s);
///         }
///         _ => {}
///     }
/// }
/// # Ok::<(), dotscope::Error>(())
/// ```
pub struct ResourceEntryRef<'a> {
    /// The name of the resource (owned for efficient map key usage)
    pub name: String,
    /// The hash of the name
    pub name_hash: u32,
    /// The parsed resource with borrowed data
    pub data: ResourceTypeRef<'a>,
}

impl<'a> ResourceTypeRef<'a> {
    /// Parses a resource type from its binary type code with zero-copy semantics.
    ///
    /// This is the zero-copy variant of [`ResourceType::from_type_byte`]. Instead of allocating
    /// owned copies for string and byte array data, it returns borrowed slices directly into
    /// the parser's underlying data buffer.
    ///
    /// This method reads a resource value from the parser based on the provided type byte,
    /// which corresponds to the type codes defined in the .NET resource format specification.
    /// Each type code indicates both the data type and how to parse the following bytes.
    ///
    /// # Arguments
    ///
    /// * `byte` - The type code byte (0x00-0xFF) that identifies the data type
    /// * `parser` - A mutable reference to the parser positioned after the type byte
    ///
    /// # Returns
    ///
    /// Returns a [`crate::Result<ResourceTypeRef<'a>>`] containing the parsed resource value
    /// with borrowed data where applicable (strings and byte arrays).
    ///
    /// # Supported Type Codes
    ///
    /// All type codes from [`ResourceType::from_type_byte`] are supported with the same
    /// semantics, except:
    /// - `0x01`: UTF-8 string returns `&'a str` instead of `String`
    /// - `0x20`: Byte array returns `&'a [u8]` instead of `Vec<u8>`
    /// - `0x21`: Stream returns `&'a [u8]` instead of `Vec<u8>`
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use dotscope::metadata::resources::types::ResourceTypeRef;
    /// use dotscope::file::parser::Parser;
    ///
    /// let data = b"\x05hello"; // Type byte 0x01, length 5, "hello"
    /// let mut parser = Parser::new(data);
    ///
    /// // Parse a string resource (type code 0x01) - zero-copy
    /// let string_type = ResourceTypeRef::from_type_byte_ref(0x01, &mut parser)?;
    /// if let ResourceTypeRef::String(s) = string_type {
    ///     println!("Found string: {}", s); // No allocation occurred
    /// }
    ///
    /// // Parse a byte array (type code 0x20) - zero-copy
    /// let data = b"\x04\x01\x02\x03\x04"; // Length 4, followed by 4 bytes
    /// let mut parser = Parser::new(data);
    /// let bytes_type = ResourceTypeRef::from_type_byte_ref(0x20, &mut parser)?;
    /// if let ResourceTypeRef::ByteArray(bytes) = bytes_type {
    ///     println!("Found {} bytes (no copy!)", bytes.len());
    /// }
    /// ```
    ///
    /// # Errors
    ///
    /// - [`crate::Error::TypeError`]: If the type byte is not supported
    /// - Parser errors: If reading the underlying data fails (e.g., truncated data)
    ///
    /// # Implementation Note
    ///
    /// Both `parser` and `data` parameters are required even though `parser.data()` returns `&'a [u8]`.
    /// This is because the parser is typically a local variable with a shorter lifetime than `'a`,
    /// and we need to return slices with lifetime `'a`. The `data` parameter provides the stable
    /// reference with lifetime `'a` that our returned slices borrow from, while `parser` is used
    /// for position tracking and reading operations.
    pub fn from_type_byte_ref(byte: u8, parser: &mut Parser<'a>, data: &'a [u8]) -> Result<Self> {
        match byte {
            0x0 => {
                // ResourceTypeCode.Null - no data to read
                Ok(ResourceTypeRef::Null)
            }
            0x1 => {
                // .NET string resources use UTF-8 encoding with 7-bit encoded byte length prefix
                // Zero-copy variant - returns borrowed slice
                Ok(ResourceTypeRef::String(
                    parser.read_prefixed_string_utf8_ref()?,
                ))
            }
            0x2 => Ok(ResourceTypeRef::Boolean(parser.read_le::<u8>()? > 0)),
            0x3 => {
                // ResourceTypeCode.Char - 2 bytes (UInt16)
                // .NET writes chars as UInt16 (UTF-16 code unit), not as single byte
                // See: ResourceWriter.WriteValue writes `(UInt16)(char)value`
                //      ResourceReader._LoadObjectV2 reads `(char)_store.ReadUInt16()`
                let code_unit = parser.read_le::<u16>()?;
                Ok(ResourceTypeRef::Char(
                    char::from_u32(u32::from(code_unit)).ok_or_else(|| {
                        TypeError("Invalid UTF-16 code unit for Char".to_string())
                    })?,
                ))
            }
            0x4 => Ok(ResourceTypeRef::Byte(parser.read_le::<u8>()?)),
            0x5 => Ok(ResourceTypeRef::SByte(parser.read_le::<i8>()?)),
            0x6 => Ok(ResourceTypeRef::Int16(parser.read_le::<i16>()?)),
            0x7 => Ok(ResourceTypeRef::UInt16(parser.read_le::<u16>()?)),
            0x8 => Ok(ResourceTypeRef::Int32(parser.read_le::<i32>()?)),
            0x9 => Ok(ResourceTypeRef::UInt32(parser.read_le::<u32>()?)),
            0xA => Ok(ResourceTypeRef::Int64(parser.read_le::<i64>()?)),
            0xB => Ok(ResourceTypeRef::UInt64(parser.read_le::<u64>()?)),
            0xC => Ok(ResourceTypeRef::Single(parser.read_le::<f32>()?)),
            0xD => Ok(ResourceTypeRef::Double(parser.read_le::<f64>()?)),
            0xE => {
                // ResourceTypeCode.Decimal - 16 bytes (4 × i32)
                // Format: lo, mid, hi, flags (all little-endian i32)
                let lo = parser.read_le::<i32>()?;
                let mid = parser.read_le::<i32>()?;
                let hi = parser.read_le::<i32>()?;
                let flags = parser.read_le::<i32>()?;
                Ok(ResourceTypeRef::Decimal { lo, mid, hi, flags })
            }
            0xF => {
                // ResourceTypeCode.DateTime - 8 bytes (i64 binary format)
                // Contains ticks (bits 0-61) and DateTimeKind (bits 62-63)
                let binary_value = parser.read_le::<i64>()?;
                Ok(ResourceTypeRef::DateTime(binary_value))
            }
            0x10 => {
                // ResourceTypeCode.TimeSpan - 8 bytes (i64 ticks)
                // Ticks represent 100-nanosecond intervals
                let ticks = parser.read_le::<i64>()?;
                Ok(ResourceTypeRef::TimeSpan(ticks))
            }
            0x20 => {
                let length = parser.read_le::<u32>()?;
                let start_pos = parser.pos();
                let end_pos = start_pos + length as usize;

                if end_pos > data.len() {
                    return Err(out_of_bounds_error!());
                }

                if end_pos < data.len() {
                    parser.seek(end_pos)?;
                }

                Ok(ResourceTypeRef::ByteArray(&data[start_pos..end_pos]))
            }
            0x21 => {
                let length = parser.read_le::<u32>()?;
                let start_pos = parser.pos();
                let end_pos = start_pos + length as usize;

                if end_pos > data.len() {
                    return Err(out_of_bounds_error!());
                }

                if end_pos < data.len() {
                    parser.seek(end_pos)?;
                }

                // Stream uses same format as ByteArray, just different type code
                Ok(ResourceTypeRef::Stream(&data[start_pos..end_pos]))
            }
            0x40..=0xFF => {
                // User types - these require a type table for resolution
                Err(TypeError(format!(
                    "TypeByte - {byte:X} is a user type (>=0x40) but requires type table resolution which is not yet implemented"
                )))
            }
            _ => Err(TypeError(format!(
                "TypeByte - {byte:X} is currently not supported"
            ))),
        }
    }

    /// Parses a resource type from its .NET type name with zero-copy semantics.
    ///
    /// This is the zero-copy variant of [`ResourceType::from_type_name`]. It maps common
    /// .NET Framework type names to their corresponding binary representations and delegates
    /// to [`Self::from_type_byte_ref`] for the actual parsing with zero-copy semantics.
    ///
    /// # Arguments
    ///
    /// * `type_name` - The fully qualified .NET type name (e.g., "System.String")
    /// * `parser` - A mutable reference to the parser positioned at the resource value
    ///
    /// # Returns
    ///
    /// Returns a [`crate::Result<ResourceTypeRef<'a>>`] containing the parsed resource value
    /// with borrowed data for strings and byte arrays.
    ///
    /// # Supported Type Names
    ///
    /// All type names from [`ResourceType::from_type_name`] are supported. String and
    /// ByteArray types return borrowed slices instead of owned data.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use dotscope::metadata::resources::types::ResourceTypeRef;
    /// use dotscope::file::parser::Parser;
    ///
    /// let data = b"\x05hello";
    /// let mut parser = Parser::new(data);
    ///
    /// // Parse using .NET type name - zero-copy
    /// let string_resource = ResourceTypeRef::from_type_name_ref("System.String", &mut parser)?;
    /// if let ResourceTypeRef::String(s) = string_resource {
    ///     println!("String resource: {}", s); // No allocation
    /// }
    /// ```
    ///
    /// # Errors
    ///
    /// - [`crate::Error::TypeError`]: If the type name is not supported
    /// - Parser errors: If reading the underlying data fails
    pub fn from_type_name_ref(
        type_name: &str,
        parser: &mut Parser<'a>,
        data: &'a [u8],
    ) -> Result<Self> {
        match type_name {
            "System.Null" => ResourceTypeRef::from_type_byte_ref(0x0, parser, data),
            "System.String" => ResourceTypeRef::from_type_byte_ref(0x1, parser, data),
            "System.Boolean" => ResourceTypeRef::from_type_byte_ref(0x2, parser, data),
            "System.Char" => ResourceTypeRef::from_type_byte_ref(0x3, parser, data),
            "System.Byte" => ResourceTypeRef::from_type_byte_ref(0x4, parser, data),
            "System.SByte" => ResourceTypeRef::from_type_byte_ref(0x5, parser, data),
            "System.Int16" => ResourceTypeRef::from_type_byte_ref(0x6, parser, data),
            "System.UInt16" => ResourceTypeRef::from_type_byte_ref(0x7, parser, data),
            "System.Int32" => ResourceTypeRef::from_type_byte_ref(0x8, parser, data),
            "System.UInt32" => ResourceTypeRef::from_type_byte_ref(0x9, parser, data),
            "System.Int64" => ResourceTypeRef::from_type_byte_ref(0xA, parser, data),
            "System.UInt64" => ResourceTypeRef::from_type_byte_ref(0xB, parser, data),
            "System.Single" => ResourceTypeRef::from_type_byte_ref(0xC, parser, data),
            "System.Double" => ResourceTypeRef::from_type_byte_ref(0xD, parser, data),
            "System.Decimal" => ResourceTypeRef::from_type_byte_ref(0xE, parser, data),
            "System.DateTime" => ResourceTypeRef::from_type_byte_ref(0xF, parser, data),
            "System.TimeSpan" => ResourceTypeRef::from_type_byte_ref(0x10, parser, data),
            "System.Byte[]" => ResourceTypeRef::from_type_byte_ref(0x20, parser, data),
            "System.IO.Stream" => ResourceTypeRef::from_type_byte_ref(0x21, parser, data),
            _ => Err(TypeError(format!(
                "TypeName - {type_name} is currently not supported"
            ))),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::file::parser::Parser;

    #[test]
    fn test_resource_magic_constant() {
        assert_eq!(RESOURCE_MAGIC, 0xBEEFCACE);
    }

    #[test]
    fn test_from_type_byte_string() {
        // UTF-8 encoding: length (5 bytes) + "hello" as UTF-8
        let data = b"\x05hello";
        let mut parser = Parser::new(data);
        let result = ResourceType::from_type_byte(0x1, &mut parser).unwrap();

        if let ResourceType::String(s) = result {
            assert_eq!(s, "hello");
        } else {
            panic!("Expected String variant");
        }
    }

    #[test]
    fn test_from_type_byte_boolean_true() {
        let data = b"\x01";
        let mut parser = Parser::new(data);
        let result = ResourceType::from_type_byte(0x2, &mut parser).unwrap();

        if let ResourceType::Boolean(b) = result {
            assert!(b);
        } else {
            panic!("Expected Boolean variant");
        }
    }

    #[test]
    fn test_from_type_byte_boolean_false() {
        let data = b"\x00";
        let mut parser = Parser::new(data);
        let result = ResourceType::from_type_byte(0x2, &mut parser).unwrap();

        if let ResourceType::Boolean(b) = result {
            assert!(!b);
        } else {
            panic!("Expected Boolean variant");
        }
    }

    #[test]
    fn test_from_type_byte_char() {
        // Char is stored as UInt16 (2 bytes) in .NET resource format
        // 'A' = 0x0041 in UTF-16 LE
        let data = &[0x41, 0x00];
        let mut parser = Parser::new(data);
        let result = ResourceType::from_type_byte(0x3, &mut parser).unwrap();

        if let ResourceType::Char(c) = result {
            assert_eq!(c, 'A');
        } else {
            panic!("Expected Char variant");
        }

        // Test non-ASCII char (e.g., '€' = U+20AC = 0x20AC in UTF-16 LE)
        let data_euro = &[0xAC, 0x20];
        let mut parser_euro = Parser::new(data_euro);
        let result_euro = ResourceType::from_type_byte(0x3, &mut parser_euro).unwrap();

        if let ResourceType::Char(c) = result_euro {
            assert_eq!(c, '');
        } else {
            panic!("Expected Char variant for euro sign");
        }
    }

    #[test]
    fn test_from_type_byte_byte() {
        let data = b"\xFF";
        let mut parser = Parser::new(data);
        let result = ResourceType::from_type_byte(0x4, &mut parser).unwrap();

        if let ResourceType::Byte(b) = result {
            assert_eq!(b, 255);
        } else {
            panic!("Expected Byte variant");
        }
    }

    #[test]
    fn test_from_type_byte_sbyte() {
        let data = b"\xFF";
        let mut parser = Parser::new(data);
        let result = ResourceType::from_type_byte(0x5, &mut parser).unwrap();

        if let ResourceType::SByte(sb) = result {
            assert_eq!(sb, -1);
        } else {
            panic!("Expected SByte variant");
        }
    }

    #[test]
    fn test_from_type_byte_int16() {
        let data = b"\xFF\xFF";
        let mut parser = Parser::new(data);
        let result = ResourceType::from_type_byte(0x6, &mut parser).unwrap();

        if let ResourceType::Int16(i) = result {
            assert_eq!(i, -1);
        } else {
            panic!("Expected Int16 variant");
        }
    }

    #[test]
    fn test_from_type_byte_uint16() {
        let data = b"\xFF\xFF";
        let mut parser = Parser::new(data);
        let result = ResourceType::from_type_byte(0x7, &mut parser).unwrap();

        if let ResourceType::UInt16(u) = result {
            assert_eq!(u, 65535);
        } else {
            panic!("Expected UInt16 variant");
        }
    }

    #[test]
    fn test_from_type_byte_int32() {
        let data = b"\x2A\x00\x00\x00";
        let mut parser = Parser::new(data);
        let result = ResourceType::from_type_byte(0x8, &mut parser).unwrap();

        if let ResourceType::Int32(i) = result {
            assert_eq!(i, 42);
        } else {
            panic!("Expected Int32 variant");
        }
    }

    #[test]
    fn test_from_type_byte_uint32() {
        let data = b"\xFF\xFF\xFF\xFF";
        let mut parser = Parser::new(data);
        let result = ResourceType::from_type_byte(0x9, &mut parser).unwrap();

        if let ResourceType::UInt32(u) = result {
            assert_eq!(u, 4294967295);
        } else {
            panic!("Expected UInt32 variant");
        }
    }

    #[test]
    fn test_from_type_byte_int64() {
        let data = b"\x2A\x00\x00\x00\x00\x00\x00\x00";
        let mut parser = Parser::new(data);
        let result = ResourceType::from_type_byte(0xA, &mut parser).unwrap();

        if let ResourceType::Int64(i) = result {
            assert_eq!(i, 42);
        } else {
            panic!("Expected Int64 variant");
        }
    }

    #[test]
    fn test_from_type_byte_uint64() {
        let data = b"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF";
        let mut parser = Parser::new(data);
        let result = ResourceType::from_type_byte(0xB, &mut parser).unwrap();

        if let ResourceType::UInt64(u) = result {
            assert_eq!(u, 18446744073709551615);
        } else {
            panic!("Expected UInt64 variant");
        }
    }

    #[test]
    fn test_from_type_byte_single() {
        let data = b"\x00\x00\x28\x42"; // 42.0 as f32 in little endian
        let mut parser = Parser::new(data);
        let result = ResourceType::from_type_byte(0xC, &mut parser).unwrap();

        if let ResourceType::Single(f) = result {
            assert_eq!(f, 42.0);
        } else {
            panic!("Expected Single variant");
        }
    }

    #[test]
    fn test_from_type_byte_double() {
        let data = b"\x00\x00\x00\x00\x00\x00\x45\x40"; // 42.0 as f64 in little endian
        let mut parser = Parser::new(data);
        let result = ResourceType::from_type_byte(0xD, &mut parser).unwrap();

        if let ResourceType::Double(d) = result {
            assert_eq!(d, 42.0);
        } else {
            panic!("Expected Double variant");
        }
    }

    #[test]
    fn test_from_type_byte_unsupported() {
        let data = b"";
        let mut parser = Parser::new(data);
        let result = ResourceType::from_type_byte(0xFF, &mut parser);

        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("FF is a user type (>=0x40) but requires type table resolution which is not yet implemented"));
    }

    #[test]
    fn test_from_type_name_null() {
        let data = b"";
        let mut parser = Parser::new(data);
        let result = ResourceType::from_type_name("System.Null", &mut parser);

        // This should successfully parse as ResourceType::Null (type code 0)
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), ResourceType::Null);
    }

    #[test]
    fn test_from_type_name_string() {
        // UTF-8 encoding: length (5 bytes) + "hello" as UTF-8
        let data = b"\x05hello";
        let mut parser = Parser::new(data);
        let result = ResourceType::from_type_name("System.String", &mut parser).unwrap();

        if let ResourceType::String(s) = result {
            assert_eq!(s, "hello");
        } else {
            panic!("Expected String variant");
        }
    }

    #[test]
    fn test_from_type_name_boolean() {
        let data = b"\x01";
        let mut parser = Parser::new(data);
        let result = ResourceType::from_type_name("System.Boolean", &mut parser).unwrap();

        if let ResourceType::Boolean(b) = result {
            assert!(b);
        } else {
            panic!("Expected Boolean variant");
        }
    }

    #[test]
    fn test_from_type_name_all_supported_types() {
        // Test each type individually since they have different data sizes

        // String
        let mut parser = Parser::new(b"\x05hello");
        assert!(ResourceType::from_type_name("System.String", &mut parser).is_ok());

        // Boolean
        let mut parser = Parser::new(b"\x01");
        assert!(ResourceType::from_type_name("System.Boolean", &mut parser).is_ok());

        // Char (2 bytes - UInt16 in .NET)
        let mut parser = Parser::new(&[0x41, 0x00]); // 'A' in UTF-16 LE
        assert!(ResourceType::from_type_name("System.Char", &mut parser).is_ok());

        // Byte
        let mut parser = Parser::new(b"\xFF");
        assert!(ResourceType::from_type_name("System.Byte", &mut parser).is_ok());

        // SByte
        let mut parser = Parser::new(b"\xFF");
        assert!(ResourceType::from_type_name("System.SByte", &mut parser).is_ok());

        // Int16
        let mut parser = Parser::new(b"\xFF\xFF");
        assert!(ResourceType::from_type_name("System.Int16", &mut parser).is_ok());

        // UInt16
        let mut parser = Parser::new(b"\xFF\xFF");
        assert!(ResourceType::from_type_name("System.UInt16", &mut parser).is_ok());

        // Int32
        let mut parser = Parser::new(b"\x2A\x00\x00\x00");
        assert!(ResourceType::from_type_name("System.Int32", &mut parser).is_ok());

        // UInt32
        let mut parser = Parser::new(b"\xFF\xFF\xFF\xFF");
        assert!(ResourceType::from_type_name("System.UInt32", &mut parser).is_ok());

        // Int64
        let mut parser = Parser::new(b"\x2A\x00\x00\x00\x00\x00\x00\x00");
        assert!(ResourceType::from_type_name("System.Int64", &mut parser).is_ok());

        // UInt64
        let mut parser = Parser::new(b"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF");
        assert!(ResourceType::from_type_name("System.UInt64", &mut parser).is_ok());

        // Single
        let mut parser = Parser::new(b"\x00\x00\x28\x42");
        assert!(ResourceType::from_type_name("System.Single", &mut parser).is_ok());

        // Double
        let mut parser = Parser::new(b"\x00\x00\x00\x00\x00\x00\x45\x40");
        assert!(ResourceType::from_type_name("System.Double", &mut parser).is_ok());
    }

    #[test]
    fn test_from_type_name_unsupported() {
        let data = b"";
        let mut parser = Parser::new(data);
        let result = ResourceType::from_type_name("System.NotSupported", &mut parser);

        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("System.NotSupported is currently not supported"));
    }

    #[test]
    fn test_resource_entry_creation() {
        let entry = ResourceEntry {
            name: "TestResource".to_string(),
            name_hash: 12345,
            data: ResourceType::String("test_data".to_string()),
        };

        assert_eq!(entry.name, "TestResource");
        assert_eq!(entry.name_hash, 12345);

        if let ResourceType::String(s) = &entry.data {
            assert_eq!(s, "test_data");
        } else {
            panic!("Expected String data");
        }
    }

    #[test]
    fn test_resource_type_debug() {
        let resource = ResourceType::String("test".to_string());
        let debug_str = format!("{resource:?}");
        assert!(debug_str.contains("String"));
        assert!(debug_str.contains("test"));
    }

    #[test]
    fn test_resource_type_clone() {
        let original = ResourceType::Int32(42);
        let cloned = original.clone();

        assert_eq!(original, cloned);

        if let (ResourceType::Int32(orig), ResourceType::Int32(clone)) = (&original, &cloned) {
            assert_eq!(orig, clone);
        } else {
            panic!("Clone should preserve type and value");
        }
    }

    #[test]
    fn test_resource_type_partial_eq() {
        let res1 = ResourceType::String("test".to_string());
        let res2 = ResourceType::String("test".to_string());
        let res3 = ResourceType::String("different".to_string());
        let res4 = ResourceType::Int32(42);

        assert_eq!(res1, res2);
        assert_ne!(res1, res3);
        assert_ne!(res1, res4);
    }

    #[test]
    fn test_resource_type_as_str() {
        // Test implemented types
        assert_eq!(
            ResourceType::String("test".to_string()).as_str(),
            Some("System.String")
        );
        assert_eq!(ResourceType::Boolean(true).as_str(), Some("System.Boolean"));
        assert_eq!(ResourceType::Int32(42).as_str(), Some("System.Int32"));
        assert_eq!(
            ResourceType::ByteArray(vec![1, 2, 3]).as_str(),
            Some("System.Byte[]")
        );
        assert_eq!(
            ResourceType::Double(std::f64::consts::PI).as_str(),
            Some("System.Double")
        );

        // Test special types (without data)
        assert_eq!(ResourceType::Null.as_str(), None);
        assert_eq!(ResourceType::StartOfUserTypes.as_str(), None);

        // Test implemented types (with data)
        assert_eq!(
            ResourceType::Decimal {
                lo: 0,
                mid: 0,
                hi: 0,
                flags: 0
            }
            .as_str(),
            Some("System.Decimal")
        );
        assert_eq!(ResourceType::DateTime(0).as_str(), Some("System.DateTime"));
        assert_eq!(ResourceType::TimeSpan(0).as_str(), Some("System.TimeSpan"));
    }

    #[test]
    fn test_resource_type_index() {
        // Test that all implemented types have correct indices
        assert_eq!(ResourceType::Boolean(true).index(), Some(0));
        assert_eq!(ResourceType::Byte(255).index(), Some(1));
        assert_eq!(ResourceType::SByte(-1).index(), Some(2));
        assert_eq!(ResourceType::Char('A').index(), Some(3));
        assert_eq!(ResourceType::Int16(42).index(), Some(4));
        assert_eq!(ResourceType::UInt16(65535).index(), Some(5));
        assert_eq!(ResourceType::Int32(42).index(), Some(6));
        assert_eq!(ResourceType::UInt32(42).index(), Some(7));
        assert_eq!(ResourceType::Int64(42).index(), Some(8));
        assert_eq!(ResourceType::UInt64(42).index(), Some(9));
        assert_eq!(ResourceType::Single(std::f32::consts::PI).index(), Some(10));
        assert_eq!(ResourceType::Double(std::f64::consts::PI).index(), Some(11));
        assert_eq!(ResourceType::String("test".to_string()).index(), Some(12));
        assert_eq!(
            ResourceType::Decimal {
                lo: 0,
                mid: 0,
                hi: 0,
                flags: 0
            }
            .index(),
            Some(13)
        );
        assert_eq!(ResourceType::DateTime(0).index(), Some(14));
        assert_eq!(ResourceType::TimeSpan(0).index(), Some(15));
        assert_eq!(ResourceType::ByteArray(vec![1, 2, 3]).index(), Some(16));
        assert_eq!(ResourceType::Stream(vec![]).index(), Some(17));

        // Test special types (without data)
        assert_eq!(ResourceType::Null.index(), None);
        assert_eq!(ResourceType::StartOfUserTypes.index(), None);
    }

    #[test]
    fn test_resource_type_index_consistency() {
        // Test that types with as_str() also have index() and vice versa
        let test_types = [
            ResourceType::Boolean(false),
            ResourceType::Byte(0),
            ResourceType::SByte(0),
            ResourceType::Char('A'),
            ResourceType::Int16(0),
            ResourceType::UInt16(0),
            ResourceType::Int32(0),
            ResourceType::UInt32(0),
            ResourceType::Int64(0),
            ResourceType::UInt64(0),
            ResourceType::Single(0.0),
            ResourceType::Double(0.0),
            ResourceType::String("".to_string()),
            ResourceType::ByteArray(vec![]),
        ];

        for resource_type in &test_types {
            // Types with as_str() should also have index()
            if resource_type.as_str().is_some() {
                assert!(
                    resource_type.index().is_some(),
                    "Type {resource_type:?} has as_str() but no index()"
                );
            }

            // Types with index() should also have as_str()
            if resource_type.index().is_some() {
                assert!(
                    resource_type.as_str().is_some(),
                    "Type {resource_type:?} has index() but no as_str()"
                );
            }
        }
    }

    #[test]
    fn test_resource_type_data_size() {
        // Test data size calculations for all implemented types
        assert_eq!(ResourceType::Boolean(true).data_size(), Some(1));
        assert_eq!(ResourceType::Byte(255).data_size(), Some(1));
        assert_eq!(ResourceType::SByte(-1).data_size(), Some(1));
        assert_eq!(ResourceType::Char('A').data_size(), Some(2)); // UTF-16
        assert_eq!(ResourceType::Int16(42).data_size(), Some(2));
        assert_eq!(ResourceType::UInt16(42).data_size(), Some(2));
        assert_eq!(ResourceType::Int32(42).data_size(), Some(4));
        assert_eq!(ResourceType::UInt32(42).data_size(), Some(4));
        assert_eq!(ResourceType::Int64(42).data_size(), Some(8));
        assert_eq!(ResourceType::UInt64(42).data_size(), Some(8));
        assert_eq!(
            ResourceType::Single(std::f32::consts::PI).data_size(),
            Some(4)
        );
        assert_eq!(
            ResourceType::Double(std::f64::consts::PI).data_size(),
            Some(8)
        );

        // Test variable-length types
        assert_eq!(
            ResourceType::String("hello".to_string()).data_size(),
            Some(6)
        ); // 1 byte length prefix + 5 bytes UTF-8
        assert_eq!(ResourceType::String("".to_string()).data_size(), Some(1)); // 1 byte length + 0 bytes
        assert_eq!(ResourceType::ByteArray(vec![1, 2, 3]).data_size(), Some(7)); // 4 byte LE length + 3 bytes data
        assert_eq!(ResourceType::ByteArray(vec![]).data_size(), Some(4)); // 4 byte LE length + 0 bytes

        // Test implemented types (Decimal, DateTime, TimeSpan)
        assert_eq!(
            ResourceType::Decimal {
                lo: 0,
                mid: 0,
                hi: 0,
                flags: 0
            }
            .data_size(),
            Some(16) // 4 × i32 = 16 bytes
        );
        assert_eq!(ResourceType::DateTime(0).data_size(), Some(8)); // i64
        assert_eq!(ResourceType::TimeSpan(0).data_size(), Some(8)); // i64
        assert_eq!(ResourceType::Stream(vec![1, 2, 3]).data_size(), Some(7)); // Same as ByteArray

        // Test special types (without data)
        assert_eq!(ResourceType::Null.data_size(), None);
        assert_eq!(ResourceType::StartOfUserTypes.data_size(), None);
    }

    #[test]
    fn test_resource_type_full_consistency() {
        // Test that types with data_size() also have as_str() and index()
        let test_types = [
            ResourceType::Boolean(false),
            ResourceType::Byte(0),
            ResourceType::SByte(0),
            ResourceType::Char('A'),
            ResourceType::Int16(0),
            ResourceType::UInt16(0),
            ResourceType::Int32(0),
            ResourceType::UInt32(0),
            ResourceType::Int64(0),
            ResourceType::UInt64(0),
            ResourceType::Single(0.0),
            ResourceType::Double(0.0),
            ResourceType::String("test".to_string()),
            ResourceType::ByteArray(vec![1, 2, 3]),
        ];

        for resource_type in &test_types {
            // All implemented types should have all three methods
            assert!(
                resource_type.as_str().is_some(),
                "Type {resource_type:?} should have as_str()"
            );
            assert!(
                resource_type.index().is_some(),
                "Type {resource_type:?} should have index()"
            );
            assert!(
                resource_type.data_size().is_some(),
                "Type {resource_type:?} should have data_size()"
            );
        }
    }
}