async-snmp 0.12.0

Modern async-first SNMP client library for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
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
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
//! SNMP value types.
//!
//! The `Value` enum represents all SNMP data types including exceptions.

use crate::ber::{Decoder, EncodeBuf, tag};
use crate::error::internal::DecodeErrorKind;
use crate::error::{Error, Result, UNKNOWN_TARGET};
use crate::format::hex;
use crate::oid::Oid;
use bytes::Bytes;

/// RFC 2579 RowStatus textual convention.
///
/// Used by SNMP tables to control row creation, modification, and deletion.
/// The state machine for RowStatus is defined in RFC 2579 Section 7.1.
///
/// # State Transitions
///
/// | Current State | Set to | Result |
/// |--------------|--------|--------|
/// | (none) | createAndGo | row created in `active` state |
/// | (none) | createAndWait | row created in `notInService` or `notReady` |
/// | notInService | active | row becomes operational |
/// | notReady | active | error (row must first be notInService) |
/// | active | notInService | row becomes inactive |
/// | any | destroy | row is deleted |
///
/// # Example
///
/// ```
/// use async_snmp::{Value, RowStatus};
///
/// // Reading a RowStatus column
/// let value = Value::Integer(1);
/// assert_eq!(value.as_row_status(), Some(RowStatus::Active));
///
/// // Creating a value to write
/// let create: Value = RowStatus::CreateAndGo.into();
/// assert_eq!(create, Value::Integer(4));
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RowStatus {
    /// Row is operational and available for use.
    Active = 1,
    /// Row exists but is not operational (e.g., being modified).
    NotInService = 2,
    /// Row exists but required columns are missing or invalid.
    NotReady = 3,
    /// Request to create a new row that immediately becomes active.
    CreateAndGo = 4,
    /// Request to create a new row that starts in notInService/notReady.
    CreateAndWait = 5,
    /// Request to delete an existing row.
    Destroy = 6,
}

impl TryFrom<i32> for RowStatus {
    type Error = i32;

    fn try_from(value: i32) -> std::result::Result<Self, i32> {
        match value {
            1 => Ok(Self::Active),
            2 => Ok(Self::NotInService),
            3 => Ok(Self::NotReady),
            4 => Ok(Self::CreateAndGo),
            5 => Ok(Self::CreateAndWait),
            6 => Ok(Self::Destroy),
            _ => Err(value),
        }
    }
}

impl RowStatus {
    /// Create from raw SNMP integer value.
    ///
    /// Returns `None` for values outside the valid range (1-6).
    pub fn from_i32(value: i32) -> Option<Self> {
        Self::try_from(value).ok()
    }
}

impl From<RowStatus> for Value {
    fn from(status: RowStatus) -> Self {
        Value::Integer(status as i32)
    }
}

impl std::fmt::Display for RowStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Active => write!(f, "active"),
            Self::NotInService => write!(f, "notInService"),
            Self::NotReady => write!(f, "notReady"),
            Self::CreateAndGo => write!(f, "createAndGo"),
            Self::CreateAndWait => write!(f, "createAndWait"),
            Self::Destroy => write!(f, "destroy"),
        }
    }
}

/// RFC 2579 StorageType textual convention.
///
/// Describes how an SNMP row's data is stored and persisted.
///
/// # Persistence Levels
///
/// | Type | Survives Reboot | Writable |
/// |------|-----------------|----------|
/// | other | undefined | varies |
/// | volatile | no | yes |
/// | nonVolatile | yes | yes |
/// | permanent | yes | limited |
/// | readOnly | yes | no |
///
/// # Example
///
/// ```
/// use async_snmp::{Value, StorageType};
///
/// // Reading a StorageType column
/// let value = Value::Integer(3);
/// assert_eq!(value.as_storage_type(), Some(StorageType::NonVolatile));
///
/// // Creating a value to write
/// let storage: Value = StorageType::Volatile.into();
/// assert_eq!(storage, Value::Integer(2));
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum StorageType {
    /// Implementation-specific storage.
    Other = 1,
    /// Lost on reboot; can be modified.
    Volatile = 2,
    /// Survives reboot; can be modified.
    NonVolatile = 3,
    /// Survives reboot; limited modifications allowed.
    Permanent = 4,
    /// Survives reboot; cannot be modified.
    ReadOnly = 5,
}

impl TryFrom<i32> for StorageType {
    type Error = i32;

    fn try_from(value: i32) -> std::result::Result<Self, i32> {
        match value {
            1 => Ok(Self::Other),
            2 => Ok(Self::Volatile),
            3 => Ok(Self::NonVolatile),
            4 => Ok(Self::Permanent),
            5 => Ok(Self::ReadOnly),
            _ => Err(value),
        }
    }
}

impl StorageType {
    /// Create from raw SNMP integer value.
    ///
    /// Returns `None` for values outside the valid range (1-5).
    pub fn from_i32(value: i32) -> Option<Self> {
        Self::try_from(value).ok()
    }
}

impl From<StorageType> for Value {
    fn from(storage: StorageType) -> Self {
        Value::Integer(storage as i32)
    }
}

impl std::fmt::Display for StorageType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Other => write!(f, "other"),
            Self::Volatile => write!(f, "volatile"),
            Self::NonVolatile => write!(f, "nonVolatile"),
            Self::Permanent => write!(f, "permanent"),
            Self::ReadOnly => write!(f, "readOnly"),
        }
    }
}

/// SNMP value.
///
/// Represents all SNMP data types including SMIv2 types and exception values.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Value {
    /// INTEGER (ASN.1 primitive, signed 32-bit)
    Integer(i32),

    /// OCTET STRING (arbitrary bytes).
    ///
    /// Per RFC 2578 (SMIv2), OCTET STRING values have a maximum size of 65535 octets.
    /// This limit is **not enforced** during decoding to maintain permissive parsing
    /// behavior. Applications that require strict compliance should validate size
    /// after decoding.
    OctetString(Bytes),

    /// NULL
    Null,

    /// OBJECT IDENTIFIER
    ObjectIdentifier(Oid),

    /// IpAddress (4 bytes, big-endian)
    IpAddress([u8; 4]),

    /// Counter32 (unsigned 32-bit, wrapping)
    Counter32(u32),

    /// Gauge32 / Unsigned32 (unsigned 32-bit, non-wrapping)
    Gauge32(u32),

    /// TimeTicks (hundredths of seconds since epoch)
    TimeTicks(u32),

    /// Opaque (legacy, arbitrary bytes)
    Opaque(Bytes),

    /// Counter64 (unsigned 64-bit, wrapping).
    ///
    /// **SNMPv2c/v3 only.** Counter64 was introduced in SNMPv2 (RFC 2578) and is
    /// not supported in SNMPv1. When sending Counter64 values to an SNMPv1 agent,
    /// the value will be silently ignored or cause an error depending on the agent
    /// implementation.
    ///
    /// If your application needs to support SNMPv1, avoid using Counter64 or
    /// fall back to Counter32 (with potential overflow for high-bandwidth counters).
    Counter64(u64),

    /// noSuchObject exception - the requested OID exists in the MIB but has no value.
    ///
    /// This exception indicates that the agent recognizes the OID (it's a valid
    /// MIB object), but there is no instance available. This commonly occurs when
    /// requesting a table column OID without an index.
    ///
    /// # Example
    ///
    /// ```
    /// use async_snmp::Value;
    ///
    /// let response = Value::NoSuchObject;
    /// assert!(response.is_exception());
    ///
    /// // When handling responses, check for exceptions:
    /// match response {
    ///     Value::NoSuchObject => println!("OID exists but has no value"),
    ///     _ => {}
    /// }
    /// ```
    NoSuchObject,

    /// noSuchInstance exception - the specific instance does not exist.
    ///
    /// This exception indicates that while the MIB object exists, the specific
    /// instance (index) requested does not. This commonly occurs when querying
    /// a table row that doesn't exist.
    ///
    /// # Example
    ///
    /// ```
    /// use async_snmp::Value;
    ///
    /// let response = Value::NoSuchInstance;
    /// assert!(response.is_exception());
    /// ```
    NoSuchInstance,

    /// endOfMibView exception - end of the MIB has been reached.
    ///
    /// This exception is returned during GETNEXT/GETBULK operations when
    /// there are no more OIDs lexicographically greater than the requested OID.
    /// This is the normal termination condition for SNMP walks.
    ///
    /// # Example
    ///
    /// ```
    /// use async_snmp::Value;
    ///
    /// let response = Value::EndOfMibView;
    /// assert!(response.is_exception());
    ///
    /// // Commonly used to detect end of walk
    /// if matches!(response, Value::EndOfMibView) {
    ///     println!("Walk complete - reached end of MIB");
    /// }
    /// ```
    EndOfMibView,

    /// Unknown/unrecognized value type (for forward compatibility)
    Unknown { tag: u8, data: Bytes },
}

impl Value {
    /// Try to get as i32.
    ///
    /// Returns `Some(i32)` for [`Value::Integer`], `None` otherwise.
    ///
    /// # Examples
    ///
    /// ```
    /// use async_snmp::Value;
    ///
    /// let v = Value::Integer(42);
    /// assert_eq!(v.as_i32(), Some(42));
    ///
    /// let v = Value::Integer(-100);
    /// assert_eq!(v.as_i32(), Some(-100));
    ///
    /// // Counter32 is not an Integer
    /// let v = Value::Counter32(42);
    /// assert_eq!(v.as_i32(), None);
    /// ```
    pub fn as_i32(&self) -> Option<i32> {
        match self {
            Value::Integer(v) => Some(*v),
            _ => None,
        }
    }

    /// Try to get as u32.
    ///
    /// Returns `Some(u32)` for [`Value::Counter32`], [`Value::Gauge32`],
    /// [`Value::TimeTicks`], or non-negative [`Value::Integer`]. Returns `None` otherwise.
    ///
    /// # Examples
    ///
    /// ```
    /// use async_snmp::Value;
    ///
    /// // Works for Counter32, Gauge32, TimeTicks
    /// assert_eq!(Value::Counter32(100).as_u32(), Some(100));
    /// assert_eq!(Value::Gauge32(200).as_u32(), Some(200));
    /// assert_eq!(Value::TimeTicks(300).as_u32(), Some(300));
    ///
    /// // Works for non-negative integers
    /// assert_eq!(Value::Integer(50).as_u32(), Some(50));
    ///
    /// // Returns None for negative integers
    /// assert_eq!(Value::Integer(-1).as_u32(), None);
    ///
    /// // Counter64 returns None (use as_u64 instead)
    /// assert_eq!(Value::Counter64(100).as_u32(), None);
    /// ```
    pub fn as_u32(&self) -> Option<u32> {
        match self {
            Value::Counter32(v) | Value::Gauge32(v) | Value::TimeTicks(v) => Some(*v),
            Value::Integer(v) if *v >= 0 => Some(*v as u32),
            _ => None,
        }
    }

    /// Try to get as u64.
    ///
    /// Returns `Some(u64)` for [`Value::Counter64`], or any 32-bit unsigned type
    /// ([`Value::Counter32`], [`Value::Gauge32`], [`Value::TimeTicks`]), or
    /// non-negative [`Value::Integer`]. Returns `None` otherwise.
    ///
    /// # Examples
    ///
    /// ```
    /// use async_snmp::Value;
    ///
    /// // Counter64 is the primary use case
    /// assert_eq!(Value::Counter64(10_000_000_000).as_u64(), Some(10_000_000_000));
    ///
    /// // Also works for 32-bit unsigned types
    /// assert_eq!(Value::Counter32(100).as_u64(), Some(100));
    /// assert_eq!(Value::Gauge32(200).as_u64(), Some(200));
    ///
    /// // Non-negative integers work
    /// assert_eq!(Value::Integer(50).as_u64(), Some(50));
    ///
    /// // Negative integers return None
    /// assert_eq!(Value::Integer(-1).as_u64(), None);
    /// ```
    pub fn as_u64(&self) -> Option<u64> {
        match self {
            Value::Counter64(v) => Some(*v),
            Value::Counter32(v) | Value::Gauge32(v) | Value::TimeTicks(v) => Some(*v as u64),
            Value::Integer(v) if *v >= 0 => Some(*v as u64),
            _ => None,
        }
    }

    /// Try to get as bytes.
    ///
    /// Returns `Some(&[u8])` for [`Value::OctetString`] or [`Value::Opaque`].
    /// Returns `None` otherwise.
    ///
    /// # Examples
    ///
    /// ```
    /// use async_snmp::Value;
    /// use bytes::Bytes;
    ///
    /// let v = Value::OctetString(Bytes::from_static(b"hello"));
    /// assert_eq!(v.as_bytes(), Some(b"hello".as_slice()));
    ///
    /// // Works for Opaque too
    /// let v = Value::Opaque(Bytes::from_static(&[0xDE, 0xAD, 0xBE, 0xEF]));
    /// assert_eq!(v.as_bytes(), Some(&[0xDE, 0xAD, 0xBE, 0xEF][..]));
    ///
    /// // Other types return None
    /// assert_eq!(Value::Integer(42).as_bytes(), None);
    /// ```
    pub fn as_bytes(&self) -> Option<&[u8]> {
        match self {
            Value::OctetString(v) | Value::Opaque(v) => Some(v),
            _ => None,
        }
    }

    /// Try to get as string (UTF-8).
    ///
    /// Returns `Some(&str)` if the value is an [`Value::OctetString`] or [`Value::Opaque`]
    /// containing valid UTF-8. Returns `None` for other types or invalid UTF-8.
    ///
    /// # Examples
    ///
    /// ```
    /// use async_snmp::Value;
    /// use bytes::Bytes;
    ///
    /// let v = Value::OctetString(Bytes::from_static(b"Linux router1 5.4.0"));
    /// assert_eq!(v.as_str(), Some("Linux router1 5.4.0"));
    ///
    /// // Invalid UTF-8 returns None
    /// let v = Value::OctetString(Bytes::from_static(&[0xFF, 0xFE]));
    /// assert_eq!(v.as_str(), None);
    ///
    /// // Binary data with valid UTF-8 bytes still works, but use as_bytes() for clarity
    /// let binary = Value::OctetString(Bytes::from_static(&[0x80, 0x81, 0x82]));
    /// assert_eq!(binary.as_str(), None); // Invalid UTF-8 sequence
    /// assert!(binary.as_bytes().is_some());
    /// ```
    pub fn as_str(&self) -> Option<&str> {
        self.as_bytes().and_then(|b| std::str::from_utf8(b).ok())
    }

    /// Try to get as OID.
    ///
    /// Returns `Some(&Oid)` for [`Value::ObjectIdentifier`], `None` otherwise.
    ///
    /// # Examples
    ///
    /// ```
    /// use async_snmp::{Value, oid};
    ///
    /// let v = Value::ObjectIdentifier(oid!(1, 3, 6, 1, 2, 1, 1, 2, 0));
    /// let oid = v.as_oid().unwrap();
    /// assert_eq!(oid.to_string(), "1.3.6.1.2.1.1.2.0");
    ///
    /// // Other types return None
    /// assert_eq!(Value::Integer(42).as_oid(), None);
    /// ```
    pub fn as_oid(&self) -> Option<&Oid> {
        match self {
            Value::ObjectIdentifier(oid) => Some(oid),
            _ => None,
        }
    }

    /// Try to get as IP address.
    ///
    /// Returns `Some(Ipv4Addr)` for [`Value::IpAddress`], `None` otherwise.
    ///
    /// # Examples
    ///
    /// ```
    /// use async_snmp::Value;
    /// use std::net::Ipv4Addr;
    ///
    /// let v = Value::IpAddress([192, 168, 1, 1]);
    /// assert_eq!(v.as_ip(), Some(Ipv4Addr::new(192, 168, 1, 1)));
    ///
    /// // Other types return None
    /// assert_eq!(Value::Integer(42).as_ip(), None);
    /// ```
    pub fn as_ip(&self) -> Option<std::net::Ipv4Addr> {
        match self {
            Value::IpAddress(bytes) => Some(std::net::Ipv4Addr::from(*bytes)),
            _ => None,
        }
    }

    /// Extract any numeric value as f64.
    ///
    /// Useful for metrics systems and graphing where all values become f64.
    /// Counter64 values above 2^53 may lose precision.
    ///
    /// # Examples
    ///
    /// ```
    /// use async_snmp::Value;
    ///
    /// assert_eq!(Value::Integer(42).as_f64(), Some(42.0));
    /// assert_eq!(Value::Counter32(1000).as_f64(), Some(1000.0));
    /// assert_eq!(Value::Counter64(10_000_000_000).as_f64(), Some(10_000_000_000.0));
    /// assert_eq!(Value::Null.as_f64(), None);
    /// ```
    pub fn as_f64(&self) -> Option<f64> {
        match self {
            Value::Integer(v) => Some(*v as f64),
            Value::Counter32(v) | Value::Gauge32(v) | Value::TimeTicks(v) => Some(*v as f64),
            Value::Counter64(v) => Some(*v as f64),
            _ => None,
        }
    }

    /// Extract Counter64 as f64 with wrapping at 2^53.
    ///
    /// Prevents precision loss for large counters. IEEE 754 double-precision
    /// floats have a 53-bit mantissa, so Counter64 values above 2^53 lose
    /// precision when converted directly. This method wraps at the mantissa
    /// limit, preserving precision for rate calculations.
    ///
    /// Use when computing rates where precision matters more than absolute
    /// magnitude. For Counter32 and other types, behaves identically to `as_f64()`.
    ///
    /// # Examples
    ///
    /// ```
    /// use async_snmp::Value;
    ///
    /// // Small values behave the same as as_f64()
    /// assert_eq!(Value::Counter64(1000).as_f64_wrapped(), Some(1000.0));
    ///
    /// // Large Counter64 wraps at 2^53
    /// let large = 1u64 << 54; // 2^54
    /// let wrapped = Value::Counter64(large).as_f64_wrapped().unwrap();
    /// assert!(wrapped < large as f64); // Wrapped to smaller value
    /// ```
    pub fn as_f64_wrapped(&self) -> Option<f64> {
        const MANTISSA_LIMIT: u64 = 1 << 53;
        match self {
            Value::Counter64(v) => Some((*v % MANTISSA_LIMIT) as f64),
            _ => self.as_f64(),
        }
    }

    /// Extract integer with implied decimal places.
    ///
    /// Many SNMP sensors report fixed-point values as integers with an
    /// implied decimal point. This method applies the scaling directly,
    /// returning a usable f64 value.
    ///
    /// This complements `format_with_hint("d-2")` which returns a String
    /// for display. Use `as_decimal()` when you need the numeric value
    /// for computation or metrics.
    ///
    /// # Examples
    ///
    /// ```
    /// use async_snmp::Value;
    ///
    /// // Temperature 2350 with places=2 → 23.50
    /// assert_eq!(Value::Integer(2350).as_decimal(2), Some(23.50));
    ///
    /// // Percentage 9999 with places=2 → 99.99
    /// assert_eq!(Value::Integer(9999).as_decimal(2), Some(99.99));
    ///
    /// // Voltage 12500 with places=3 → 12.500
    /// assert_eq!(Value::Integer(12500).as_decimal(3), Some(12.5));
    ///
    /// // Non-numeric types return None
    /// assert_eq!(Value::Null.as_decimal(2), None);
    /// ```
    pub fn as_decimal(&self, places: u8) -> Option<f64> {
        let divisor = 10f64.powi(places as i32);
        self.as_f64().map(|v| v / divisor)
    }

    /// TimeTicks as Duration (hundredths of seconds).
    ///
    /// TimeTicks represents time in hundredths of a second. This method
    /// converts to `std::time::Duration` for idiomatic Rust time handling.
    ///
    /// Common use: sysUpTime, interface last-change timestamps.
    ///
    /// # Examples
    ///
    /// ```
    /// use async_snmp::Value;
    /// use std::time::Duration;
    ///
    /// // 360000 ticks = 3600 seconds = 1 hour
    /// let uptime = Value::TimeTicks(360000);
    /// assert_eq!(uptime.as_duration(), Some(Duration::from_secs(3600)));
    ///
    /// // Non-TimeTicks return None
    /// assert_eq!(Value::Integer(100).as_duration(), None);
    /// ```
    pub fn as_duration(&self) -> Option<std::time::Duration> {
        match self {
            Value::TimeTicks(v) => Some(std::time::Duration::from_millis(*v as u64 * 10)),
            _ => None,
        }
    }

    /// Extract IEEE 754 float from Opaque value (net-snmp extension).
    ///
    /// Decodes the two-layer ASN.1 structure used by net-snmp to encode floats
    /// inside Opaque values: extension tag (0x9f) + float type (0x78) + length (4)
    /// + 4 bytes IEEE 754 big-endian float.
    ///
    /// This is a non-standard extension supported by net-snmp for agents that
    /// need to report floating-point values. Standard SNMP uses implied decimal
    /// points via DISPLAY-HINT instead.
    ///
    /// # Examples
    ///
    /// ```
    /// use async_snmp::Value;
    /// use bytes::Bytes;
    ///
    /// // Opaque-encoded float for pi
    /// let data = Bytes::from_static(&[0x9f, 0x78, 0x04, 0x40, 0x49, 0x0f, 0xdb]);
    /// let value = Value::Opaque(data);
    /// let pi = value.as_opaque_float().unwrap();
    /// assert!((pi - std::f32::consts::PI).abs() < 0.0001);
    ///
    /// // Non-Opaque or wrong format returns None
    /// assert_eq!(Value::Integer(42).as_opaque_float(), None);
    /// ```
    pub fn as_opaque_float(&self) -> Option<f32> {
        match self {
            Value::Opaque(data)
                if data.len() >= 7
                && data[0] == 0x9f       // ASN_OPAQUE_TAG1 (extension)
                && data[1] == 0x78       // ASN_OPAQUE_FLOAT
                && data[2] == 0x04 =>
            {
                // length = 4
                let bytes: [u8; 4] = data[3..7].try_into().ok()?;
                Some(f32::from_be_bytes(bytes))
            }
            _ => None,
        }
    }

    /// Extract IEEE 754 double from Opaque value (net-snmp extension).
    ///
    /// Decodes the two-layer ASN.1 structure used by net-snmp to encode doubles
    /// inside Opaque values: extension tag (0x9f) + double type (0x79) + length (8)
    /// + 8 bytes IEEE 754 big-endian double.
    ///
    /// # Examples
    ///
    /// ```
    /// use async_snmp::Value;
    /// use bytes::Bytes;
    ///
    /// // Opaque-encoded double for pi ≈ 3.141592653589793
    /// let data = Bytes::from_static(&[
    ///     0x9f, 0x79, 0x08,  // extension tag, double type, length
    ///     0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18  // IEEE 754 double
    /// ]);
    /// let value = Value::Opaque(data);
    /// let pi = value.as_opaque_double().unwrap();
    /// assert!((pi - std::f64::consts::PI).abs() < 1e-10);
    /// ```
    pub fn as_opaque_double(&self) -> Option<f64> {
        match self {
            Value::Opaque(data)
                if data.len() >= 11
                && data[0] == 0x9f       // ASN_OPAQUE_TAG1 (extension)
                && data[1] == 0x79       // ASN_OPAQUE_DOUBLE
                && data[2] == 0x08 =>
            {
                // length = 8
                let bytes: [u8; 8] = data[3..11].try_into().ok()?;
                Some(f64::from_be_bytes(bytes))
            }
            _ => None,
        }
    }

    /// Extract Counter64 from Opaque value (net-snmp extension for SNMPv1).
    ///
    /// SNMPv1 doesn't support Counter64 natively. net-snmp encodes 64-bit
    /// counters inside Opaque for SNMPv1 compatibility using extension tag
    /// (0x9f) + counter64 type (0x76) + length + big-endian bytes.
    ///
    /// # Examples
    ///
    /// ```
    /// use async_snmp::Value;
    /// use bytes::Bytes;
    ///
    /// // Opaque-encoded Counter64 with value 0x0123456789ABCDEF
    /// let data = Bytes::from_static(&[
    ///     0x9f, 0x76, 0x08,  // extension tag, counter64 type, length
    ///     0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF
    /// ]);
    /// let value = Value::Opaque(data);
    /// assert_eq!(value.as_opaque_counter64(), Some(0x0123456789ABCDEF));
    /// ```
    pub fn as_opaque_counter64(&self) -> Option<u64> {
        self.as_opaque_unsigned(0x76)
    }

    /// Extract signed 64-bit integer from Opaque value (net-snmp extension).
    ///
    /// Uses extension tag (0x9f) + i64 type (0x7a) + length + big-endian bytes.
    ///
    /// # Examples
    ///
    /// ```
    /// use async_snmp::Value;
    /// use bytes::Bytes;
    ///
    /// // Opaque-encoded I64 with value -1 (0xFFFFFFFFFFFFFFFF)
    /// let data = Bytes::from_static(&[
    ///     0x9f, 0x7a, 0x08,
    ///     0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
    /// ]);
    /// let value = Value::Opaque(data);
    /// assert_eq!(value.as_opaque_i64(), Some(-1i64));
    /// ```
    pub fn as_opaque_i64(&self) -> Option<i64> {
        match self {
            Value::Opaque(data)
                if data.len() >= 4
                && data[0] == 0x9f       // ASN_OPAQUE_TAG1 (extension)
                && data[1] == 0x7a =>
            {
                // ASN_OPAQUE_I64
                let len = data[2] as usize;
                if data.len() < 3 + len || len == 0 || len > 8 {
                    return None;
                }
                let bytes = &data[3..3 + len];
                // Sign-extend from the actual length
                let is_negative = bytes[0] & 0x80 != 0;
                let mut value: i64 = if is_negative { -1 } else { 0 };
                for &byte in bytes {
                    value = (value << 8) | (byte as i64);
                }
                Some(value)
            }
            _ => None,
        }
    }

    /// Extract unsigned 64-bit integer from Opaque value (net-snmp extension).
    ///
    /// Uses extension tag (0x9f) + u64 type (0x7b) + length + big-endian bytes.
    ///
    /// # Examples
    ///
    /// ```
    /// use async_snmp::Value;
    /// use bytes::Bytes;
    ///
    /// // Opaque-encoded U64 with value 0x0123456789ABCDEF
    /// let data = Bytes::from_static(&[
    ///     0x9f, 0x7b, 0x08,
    ///     0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF
    /// ]);
    /// let value = Value::Opaque(data);
    /// assert_eq!(value.as_opaque_u64(), Some(0x0123456789ABCDEF));
    /// ```
    pub fn as_opaque_u64(&self) -> Option<u64> {
        self.as_opaque_unsigned(0x7b)
    }

    /// Helper for extracting unsigned 64-bit values from Opaque (Counter64, U64).
    fn as_opaque_unsigned(&self, expected_type: u8) -> Option<u64> {
        match self {
            Value::Opaque(data)
                if data.len() >= 4
                && data[0] == 0x9f           // ASN_OPAQUE_TAG1 (extension)
                && data[1] == expected_type =>
            {
                let len = data[2] as usize;
                if data.len() < 3 + len || len == 0 || len > 8 {
                    return None;
                }
                let bytes = &data[3..3 + len];
                let mut value: u64 = 0;
                for &byte in bytes {
                    value = (value << 8) | (byte as u64);
                }
                Some(value)
            }
            _ => None,
        }
    }

    /// Extract RFC 2579 TruthValue as bool.
    ///
    /// TruthValue is an INTEGER with: true(1), false(2).
    /// Returns `None` for non-Integer values or values outside {1, 2}.
    ///
    /// # Examples
    ///
    /// ```
    /// use async_snmp::Value;
    ///
    /// assert_eq!(Value::Integer(1).as_truth_value(), Some(true));
    /// assert_eq!(Value::Integer(2).as_truth_value(), Some(false));
    ///
    /// // Invalid values return None
    /// assert_eq!(Value::Integer(0).as_truth_value(), None);
    /// assert_eq!(Value::Integer(3).as_truth_value(), None);
    /// assert_eq!(Value::Null.as_truth_value(), None);
    /// ```
    pub fn as_truth_value(&self) -> Option<bool> {
        match self {
            Value::Integer(1) => Some(true),
            Value::Integer(2) => Some(false),
            _ => None,
        }
    }

    /// Extract RFC 2579 RowStatus.
    ///
    /// Returns `None` for non-Integer values or values outside {1-6}.
    ///
    /// # Examples
    ///
    /// ```
    /// use async_snmp::{Value, RowStatus};
    ///
    /// assert_eq!(Value::Integer(1).as_row_status(), Some(RowStatus::Active));
    /// assert_eq!(Value::Integer(6).as_row_status(), Some(RowStatus::Destroy));
    ///
    /// // Invalid values return None
    /// assert_eq!(Value::Integer(0).as_row_status(), None);
    /// assert_eq!(Value::Integer(7).as_row_status(), None);
    /// assert_eq!(Value::Null.as_row_status(), None);
    /// ```
    pub fn as_row_status(&self) -> Option<RowStatus> {
        match self {
            Value::Integer(v) => RowStatus::from_i32(*v),
            _ => None,
        }
    }

    /// Extract RFC 2579 StorageType.
    ///
    /// Returns `None` for non-Integer values or values outside {1-5}.
    ///
    /// # Examples
    ///
    /// ```
    /// use async_snmp::{Value, StorageType};
    ///
    /// assert_eq!(Value::Integer(2).as_storage_type(), Some(StorageType::Volatile));
    /// assert_eq!(Value::Integer(3).as_storage_type(), Some(StorageType::NonVolatile));
    ///
    /// // Invalid values return None
    /// assert_eq!(Value::Integer(0).as_storage_type(), None);
    /// assert_eq!(Value::Integer(6).as_storage_type(), None);
    /// assert_eq!(Value::Null.as_storage_type(), None);
    /// ```
    pub fn as_storage_type(&self) -> Option<StorageType> {
        match self {
            Value::Integer(v) => StorageType::from_i32(*v),
            _ => None,
        }
    }

    /// Check if this is an exception value.
    pub fn is_exception(&self) -> bool {
        matches!(
            self,
            Value::NoSuchObject | Value::NoSuchInstance | Value::EndOfMibView
        )
    }

    /// Returns the total BER-encoded size (tag + length + content).
    pub(crate) fn ber_encoded_size(&self) -> usize {
        use crate::ber::{
            integer_content_len, length_encoded_len, unsigned32_content_len, unsigned64_content_len,
        };

        match self {
            Value::Integer(v) => {
                let content_len = integer_content_len(*v);
                1 + length_encoded_len(content_len) + content_len
            }
            Value::OctetString(data) => {
                let content_len = data.len();
                1 + length_encoded_len(content_len) + content_len
            }
            Value::Null => 2, // tag + length(0)
            Value::ObjectIdentifier(oid) => oid.ber_encoded_size(),
            Value::IpAddress(_) => 6, // tag + length(4) + 4 bytes
            Value::Counter32(v) | Value::Gauge32(v) | Value::TimeTicks(v) => {
                let content_len = unsigned32_content_len(*v);
                1 + length_encoded_len(content_len) + content_len
            }
            Value::Opaque(data) => {
                let content_len = data.len();
                1 + length_encoded_len(content_len) + content_len
            }
            Value::Counter64(v) => {
                let content_len = unsigned64_content_len(*v);
                1 + length_encoded_len(content_len) + content_len
            }
            Value::NoSuchObject | Value::NoSuchInstance | Value::EndOfMibView => 2, // tag + length(0)
            Value::Unknown { data, .. } => {
                let content_len = data.len();
                1 + length_encoded_len(content_len) + content_len
            }
        }
    }

    /// Format an OctetString, Opaque, or Integer value using RFC 2579 DISPLAY-HINT.
    ///
    /// For OctetString and Opaque, uses the OCTET STRING hint format (e.g., "1x:").
    /// For Integer, uses the INTEGER hint format (e.g., "d-2" for decimal places).
    ///
    /// Returns `None` for other value types or invalid hint syntax.
    /// On invalid OCTET STRING hint syntax, falls back to hex encoding.
    ///
    /// # Example
    ///
    /// ```
    /// use async_snmp::Value;
    /// use bytes::Bytes;
    ///
    /// // OctetString: MAC address
    /// let mac = Value::OctetString(Bytes::from_static(&[0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5e]));
    /// assert_eq!(mac.format_with_hint("1x:"), Some("00:1a:2b:3c:4d:5e".into()));
    ///
    /// // Integer: Temperature with 2 decimal places
    /// let temp = Value::Integer(2350);
    /// assert_eq!(temp.format_with_hint("d-2"), Some("23.50".into()));
    ///
    /// // Integer: Hex format
    /// let int = Value::Integer(255);
    /// assert_eq!(int.format_with_hint("x"), Some("ff".into()));
    /// ```
    pub fn format_with_hint(&self, hint: &str) -> Option<String> {
        match self {
            Value::OctetString(bytes) => Some(crate::format::display_hint::apply(hint, bytes)),
            Value::Opaque(bytes) => Some(crate::format::display_hint::apply(hint, bytes)),
            Value::Integer(v) => crate::format::display_hint::apply_integer(hint, *v),
            _ => None,
        }
    }

    /// Encode to BER.
    pub fn encode(&self, buf: &mut EncodeBuf) {
        match self {
            Value::Integer(v) => buf.push_integer(*v),
            Value::OctetString(data) => buf.push_octet_string(data),
            Value::Null => buf.push_null(),
            Value::ObjectIdentifier(oid) => buf.push_oid(oid),
            Value::IpAddress(addr) => buf.push_ip_address(*addr),
            Value::Counter32(v) => buf.push_unsigned32(tag::application::COUNTER32, *v),
            Value::Gauge32(v) => buf.push_unsigned32(tag::application::GAUGE32, *v),
            Value::TimeTicks(v) => buf.push_unsigned32(tag::application::TIMETICKS, *v),
            Value::Opaque(data) => {
                buf.push_bytes(data);
                buf.push_length(data.len());
                buf.push_tag(tag::application::OPAQUE);
            }
            Value::Counter64(v) => buf.push_integer64(*v),
            Value::NoSuchObject => {
                buf.push_length(0);
                buf.push_tag(tag::context::NO_SUCH_OBJECT);
            }
            Value::NoSuchInstance => {
                buf.push_length(0);
                buf.push_tag(tag::context::NO_SUCH_INSTANCE);
            }
            Value::EndOfMibView => {
                buf.push_length(0);
                buf.push_tag(tag::context::END_OF_MIB_VIEW);
            }
            Value::Unknown { tag: t, data } => {
                buf.push_bytes(data);
                buf.push_length(data.len());
                buf.push_tag(*t);
            }
        }
    }

    /// Decode from BER.
    pub fn decode(decoder: &mut Decoder) -> Result<Self> {
        let tag = decoder.read_tag()?;
        let len = decoder.read_length()?;

        match tag {
            tag::universal::INTEGER => {
                let value = decoder.read_integer_value(len)?;
                Ok(Value::Integer(value))
            }
            tag::universal::OCTET_STRING => {
                let available = decoder.remaining();
                let len = if len > available {
                    // Some devices (notably MikroTik) send OctetString values
                    // where the declared length exceeds the enclosing varbind
                    // SEQUENCE boundary by 1-2 bytes (firmware bug). Trust the
                    // SEQUENCE boundary over the value length field and clamp.
                    tracing::warn!(
                        target: "async_snmp::value",
                        { snmp.offset = %decoder.offset(), declared = len, available },
                        "OctetString length exceeds varbind SEQUENCE boundary, clamping to available bytes"
                    );
                    available
                } else {
                    len
                };
                let data = decoder.read_bytes(len)?;
                Ok(Value::OctetString(data))
            }
            tag::universal::NULL => {
                if len != 0 {
                    tracing::debug!(target: "async_snmp::value", { offset = decoder.offset(), kind = %DecodeErrorKind::InvalidNull }, "decode error");
                    return Err(Error::MalformedResponse {
                        target: UNKNOWN_TARGET,
                    }
                    .boxed());
                }
                Ok(Value::Null)
            }
            tag::universal::OBJECT_IDENTIFIER => {
                let oid = decoder.read_oid_value(len)?;
                Ok(Value::ObjectIdentifier(oid))
            }
            tag::application::IP_ADDRESS => {
                if len != 4 {
                    tracing::debug!(target: "async_snmp::value", { offset = decoder.offset(), length = len, kind = %DecodeErrorKind::InvalidIpAddressLength { length: len } }, "decode error");
                    return Err(Error::MalformedResponse {
                        target: UNKNOWN_TARGET,
                    }
                    .boxed());
                }
                let data = decoder.read_bytes(4)?;
                Ok(Value::IpAddress([data[0], data[1], data[2], data[3]]))
            }
            tag::application::COUNTER32 => {
                let value = decoder.read_unsigned32_value(len)?;
                Ok(Value::Counter32(value))
            }
            tag::application::GAUGE32 => {
                let value = decoder.read_unsigned32_value(len)?;
                Ok(Value::Gauge32(value))
            }
            tag::application::TIMETICKS => {
                let value = decoder.read_unsigned32_value(len)?;
                Ok(Value::TimeTicks(value))
            }
            tag::application::OPAQUE => {
                let available = decoder.remaining();
                let len = if len > available {
                    tracing::warn!(
                        target: "async_snmp::value",
                        { snmp.offset = %decoder.offset(), declared = len, available },
                        "Opaque length exceeds varbind SEQUENCE boundary, clamping to available bytes"
                    );
                    available
                } else {
                    len
                };
                let data = decoder.read_bytes(len)?;
                Ok(Value::Opaque(data))
            }
            tag::application::NSAP => {
                let data = decoder.read_bytes(len)?;
                Ok(Value::OctetString(data))
            }
            tag::application::COUNTER64 => {
                let value = decoder.read_integer64_value(len)?;
                Ok(Value::Counter64(value))
            }
            tag::application::UINTEGER32 => {
                let value = decoder.read_unsigned32_value(len)?;
                Ok(Value::Gauge32(value))
            }
            tag::context::NO_SUCH_OBJECT => {
                if len != 0 {
                    let _ = decoder.read_bytes(len)?;
                }
                Ok(Value::NoSuchObject)
            }
            tag::context::NO_SUCH_INSTANCE => {
                if len != 0 {
                    let _ = decoder.read_bytes(len)?;
                }
                Ok(Value::NoSuchInstance)
            }
            tag::context::END_OF_MIB_VIEW => {
                if len != 0 {
                    let _ = decoder.read_bytes(len)?;
                }
                Ok(Value::EndOfMibView)
            }
            // Reject constructed OCTET STRING (0x24).
            // Net-snmp documents but does not parse constructed form; we follow suit.
            tag::universal::OCTET_STRING_CONSTRUCTED => {
                tracing::debug!(target: "async_snmp::value", { offset = decoder.offset(), kind = %DecodeErrorKind::ConstructedOctetString }, "decode error");
                Err(Error::MalformedResponse {
                    target: UNKNOWN_TARGET,
                }
                .boxed())
            }
            _ => {
                // Unknown tag - preserve for forward compatibility
                let data = decoder.read_bytes(len)?;
                Ok(Value::Unknown { tag, data })
            }
        }
    }
}

impl std::fmt::Display for Value {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Value::Integer(v) => write!(f, "{}", v),
            Value::OctetString(data) => {
                // Try to display as string if it's valid UTF-8
                if let Ok(s) = std::str::from_utf8(data) {
                    write!(f, "{}", s)
                } else {
                    write!(f, "0x{}", hex::encode(data))
                }
            }
            Value::Null => write!(f, "NULL"),
            Value::ObjectIdentifier(oid) => write!(f, "{}", oid),
            Value::IpAddress(addr) => {
                write!(f, "{}.{}.{}.{}", addr[0], addr[1], addr[2], addr[3])
            }
            Value::Counter32(v) => write!(f, "{}", v),
            Value::Gauge32(v) => write!(f, "{}", v),
            Value::TimeTicks(v) => {
                write!(f, "{}", crate::format::format_timeticks(*v))
            }
            Value::Opaque(data) => write!(f, "Opaque(0x{})", hex::encode(data)),
            Value::Counter64(v) => write!(f, "{}", v),
            Value::NoSuchObject => write!(f, "noSuchObject"),
            Value::NoSuchInstance => write!(f, "noSuchInstance"),
            Value::EndOfMibView => write!(f, "endOfMibView"),
            Value::Unknown { tag, data } => {
                write!(
                    f,
                    "Unknown(tag=0x{:02X}, data=0x{})",
                    tag,
                    hex::encode(data)
                )
            }
        }
    }
}

/// Convenience conversions for creating [`Value`] from common Rust types.
///
/// # Examples
///
/// ```
/// use async_snmp::Value;
/// use bytes::Bytes;
///
/// // From integers
/// let v: Value = 42i32.into();
/// assert_eq!(v.as_i32(), Some(42));
///
/// // From strings (creates OctetString)
/// let v: Value = "hello".into();
/// assert_eq!(v.as_str(), Some("hello"));
///
/// // From String
/// let v: Value = String::from("world").into();
/// assert_eq!(v.as_str(), Some("world"));
///
/// // From byte slices
/// let v: Value = (&[1u8, 2, 3][..]).into();
/// assert_eq!(v.as_bytes(), Some(&[1, 2, 3][..]));
///
/// // From Bytes
/// let v: Value = Bytes::from_static(b"data").into();
/// assert_eq!(v.as_bytes(), Some(b"data".as_slice()));
///
/// // From u64 (creates Counter64)
/// let v: Value = 10_000_000_000u64.into();
/// assert_eq!(v.as_u64(), Some(10_000_000_000));
///
/// // From Ipv4Addr
/// use std::net::Ipv4Addr;
/// let v: Value = Ipv4Addr::new(10, 0, 0, 1).into();
/// assert_eq!(v.as_ip(), Some(Ipv4Addr::new(10, 0, 0, 1)));
///
/// // From [u8; 4] (creates IpAddress)
/// let v: Value = [192u8, 168, 1, 1].into();
/// assert!(matches!(v, Value::IpAddress([192, 168, 1, 1])));
/// ```
impl From<i32> for Value {
    fn from(v: i32) -> Self {
        Value::Integer(v)
    }
}

impl From<&str> for Value {
    fn from(s: &str) -> Self {
        Value::OctetString(Bytes::copy_from_slice(s.as_bytes()))
    }
}

impl From<String> for Value {
    fn from(s: String) -> Self {
        Value::OctetString(Bytes::from(s))
    }
}

impl From<&[u8]> for Value {
    fn from(data: &[u8]) -> Self {
        Value::OctetString(Bytes::copy_from_slice(data))
    }
}

impl From<Oid> for Value {
    fn from(oid: Oid) -> Self {
        Value::ObjectIdentifier(oid)
    }
}

impl From<std::net::Ipv4Addr> for Value {
    fn from(addr: std::net::Ipv4Addr) -> Self {
        Value::IpAddress(addr.octets())
    }
}

impl From<Bytes> for Value {
    fn from(data: Bytes) -> Self {
        Value::OctetString(data)
    }
}

impl From<u64> for Value {
    fn from(v: u64) -> Self {
        Value::Counter64(v)
    }
}

/// Converts a 4-byte array into [`Value::IpAddress`].
///
/// The bytes are interpreted as a big-endian IPv4 address, matching the IpAddress
/// SNMP type (RFC 2578 Section 7.1.5). Use this when you already have an address
/// in `[u8; 4]` form (e.g., from `Ipv4Addr::octets()`).
impl From<[u8; 4]> for Value {
    fn from(addr: [u8; 4]) -> Self {
        Value::IpAddress(addr)
    }
}

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

    // AUDIT-003: Test that constructed OCTET STRING (0x24) is explicitly rejected.
    // Net-snmp documents but does not parse constructed form; we reject it.
    #[test]
    fn test_reject_constructed_octet_string() {
        // Constructed OCTET STRING has tag 0x24 (0x04 | 0x20)
        // Create a fake BER-encoded constructed OCTET STRING: 0x24 0x03 0x04 0x01 0x41
        // (constructed OCTET STRING containing primitive OCTET STRING "A")
        let data = bytes::Bytes::from_static(&[0x24, 0x03, 0x04, 0x01, 0x41]);
        let mut decoder = Decoder::new(data);
        let result = Value::decode(&mut decoder);

        assert!(
            result.is_err(),
            "constructed OCTET STRING (0x24) should be rejected"
        );
        // Verify error is MalformedResponse (detailed error kind is logged via tracing)
        let err = result.unwrap_err();
        assert!(
            matches!(&*err, crate::Error::MalformedResponse { .. }),
            "expected MalformedResponse error, got: {:?}",
            err
        );
    }

    #[test]
    fn test_primitive_octet_string_accepted() {
        // Primitive OCTET STRING (0x04) should be accepted
        let data = bytes::Bytes::from_static(&[0x04, 0x03, 0x41, 0x42, 0x43]); // "ABC"
        let mut decoder = Decoder::new(data);
        let result = Value::decode(&mut decoder);

        assert!(result.is_ok(), "primitive OCTET STRING should be accepted");
        let value = result.unwrap();
        assert_eq!(value.as_bytes(), Some(&b"ABC"[..]));
    }

    // ========================================================================
    // Value Type Encoding/Decoding Tests
    // ========================================================================

    fn roundtrip(value: Value) -> Value {
        let mut buf = EncodeBuf::new();
        value.encode(&mut buf);
        let data = buf.finish();
        let mut decoder = Decoder::new(data);
        Value::decode(&mut decoder).unwrap()
    }

    #[test]
    fn test_integer_positive() {
        let value = Value::Integer(42);
        assert_eq!(roundtrip(value.clone()), value);
    }

    #[test]
    fn test_integer_negative() {
        let value = Value::Integer(-42);
        assert_eq!(roundtrip(value.clone()), value);
    }

    #[test]
    fn test_integer_zero() {
        let value = Value::Integer(0);
        assert_eq!(roundtrip(value.clone()), value);
    }

    #[test]
    fn test_integer_min() {
        let value = Value::Integer(i32::MIN);
        assert_eq!(roundtrip(value.clone()), value);
    }

    #[test]
    fn test_integer_max() {
        let value = Value::Integer(i32::MAX);
        assert_eq!(roundtrip(value.clone()), value);
    }

    #[test]
    fn test_octet_string_ascii() {
        let value = Value::OctetString(Bytes::from_static(b"hello world"));
        assert_eq!(roundtrip(value.clone()), value);
    }

    #[test]
    fn test_octet_string_binary() {
        let value = Value::OctetString(Bytes::from_static(&[0x00, 0xFF, 0x80, 0x7F]));
        assert_eq!(roundtrip(value.clone()), value);
    }

    #[test]
    fn test_octet_string_empty() {
        let value = Value::OctetString(Bytes::new());
        assert_eq!(roundtrip(value.clone()), value);
    }

    #[test]
    fn test_null() {
        let value = Value::Null;
        assert_eq!(roundtrip(value.clone()), value);
    }

    #[test]
    fn test_object_identifier() {
        let value = Value::ObjectIdentifier(crate::oid!(1, 3, 6, 1, 2, 1, 1, 1, 0));
        assert_eq!(roundtrip(value.clone()), value);
    }

    #[test]
    fn test_ip_address() {
        let value = Value::IpAddress([192, 168, 1, 1]);
        assert_eq!(roundtrip(value.clone()), value);
    }

    #[test]
    fn test_ip_address_zero() {
        let value = Value::IpAddress([0, 0, 0, 0]);
        assert_eq!(roundtrip(value.clone()), value);
    }

    #[test]
    fn test_ip_address_broadcast() {
        let value = Value::IpAddress([255, 255, 255, 255]);
        assert_eq!(roundtrip(value.clone()), value);
    }

    #[test]
    fn test_counter32() {
        let value = Value::Counter32(999999);
        assert_eq!(roundtrip(value.clone()), value);
    }

    #[test]
    fn test_counter32_zero() {
        let value = Value::Counter32(0);
        assert_eq!(roundtrip(value.clone()), value);
    }

    #[test]
    fn test_counter32_max() {
        let value = Value::Counter32(u32::MAX);
        assert_eq!(roundtrip(value.clone()), value);
    }

    #[test]
    fn test_gauge32() {
        let value = Value::Gauge32(1000000000);
        assert_eq!(roundtrip(value.clone()), value);
    }

    #[test]
    fn test_gauge32_max() {
        let value = Value::Gauge32(u32::MAX);
        assert_eq!(roundtrip(value.clone()), value);
    }

    #[test]
    fn test_timeticks() {
        let value = Value::TimeTicks(123456);
        assert_eq!(roundtrip(value.clone()), value);
    }

    #[test]
    fn test_timeticks_max() {
        let value = Value::TimeTicks(u32::MAX);
        assert_eq!(roundtrip(value.clone()), value);
    }

    #[test]
    fn test_opaque() {
        let value = Value::Opaque(Bytes::from_static(&[0xDE, 0xAD, 0xBE, 0xEF]));
        assert_eq!(roundtrip(value.clone()), value);
    }

    #[test]
    fn test_opaque_empty() {
        let value = Value::Opaque(Bytes::new());
        assert_eq!(roundtrip(value.clone()), value);
    }

    #[test]
    fn test_counter64() {
        let value = Value::Counter64(123456789012345);
        assert_eq!(roundtrip(value.clone()), value);
    }

    #[test]
    fn test_counter64_zero() {
        let value = Value::Counter64(0);
        assert_eq!(roundtrip(value.clone()), value);
    }

    #[test]
    fn test_counter64_max() {
        let value = Value::Counter64(u64::MAX);
        assert_eq!(roundtrip(value.clone()), value);
    }

    #[test]
    fn test_no_such_object() {
        let value = Value::NoSuchObject;
        assert_eq!(roundtrip(value.clone()), value);
    }

    #[test]
    fn test_no_such_instance() {
        let value = Value::NoSuchInstance;
        assert_eq!(roundtrip(value.clone()), value);
    }

    #[test]
    fn test_end_of_mib_view() {
        let value = Value::EndOfMibView;
        assert_eq!(roundtrip(value.clone()), value);
    }

    #[test]
    fn test_unknown_tag_preserved() {
        // Tag 0x48 is application class but not a standard SNMP type
        let data = Bytes::from_static(&[0x48, 0x03, 0x01, 0x02, 0x03]);
        let mut decoder = Decoder::new(data);
        let value = Value::decode(&mut decoder).unwrap();

        match value {
            Value::Unknown { tag, ref data } => {
                assert_eq!(tag, 0x48);
                assert_eq!(data.as_ref(), &[0x01, 0x02, 0x03]);
            }
            _ => panic!("expected Unknown variant"),
        }

        // Roundtrip should preserve
        assert_eq!(roundtrip(value.clone()), value);
    }

    #[test]
    fn test_nsap_decodes_as_octet_string() {
        // Historic NSAP type (0x45) decodes as OctetString
        let data = Bytes::from_static(&[0x45, 0x03, 0x01, 0x02, 0x03]);
        let mut decoder = Decoder::new(data);
        let value = Value::decode(&mut decoder).unwrap();
        assert_eq!(
            value,
            Value::OctetString(Bytes::from_static(&[0x01, 0x02, 0x03]))
        );
    }

    #[test]
    fn test_uinteger32_decodes_as_gauge32() {
        // Historic UInteger32 type (0x47) decodes as Gauge32
        let data = Bytes::from_static(&[0x47, 0x01, 0x2a]);
        let mut decoder = Decoder::new(data);
        let value = Value::decode(&mut decoder).unwrap();
        assert_eq!(value, Value::Gauge32(42));
    }

    // ========================================================================
    // Accessor Method Tests
    // ========================================================================

    #[test]
    fn test_as_i32() {
        assert_eq!(Value::Integer(42).as_i32(), Some(42));
        assert_eq!(Value::Integer(-42).as_i32(), Some(-42));
        assert_eq!(Value::Counter32(100).as_i32(), None);
        assert_eq!(Value::Null.as_i32(), None);
    }

    #[test]
    fn test_as_u32() {
        assert_eq!(Value::Counter32(100).as_u32(), Some(100));
        assert_eq!(Value::Gauge32(200).as_u32(), Some(200));
        assert_eq!(Value::TimeTicks(300).as_u32(), Some(300));
        assert_eq!(Value::Integer(50).as_u32(), Some(50));
        assert_eq!(Value::Integer(-1).as_u32(), None);
        assert_eq!(Value::Counter64(100).as_u32(), None);
    }

    #[test]
    fn test_as_u64() {
        assert_eq!(Value::Counter64(100).as_u64(), Some(100));
        assert_eq!(Value::Counter32(100).as_u64(), Some(100));
        assert_eq!(Value::Gauge32(200).as_u64(), Some(200));
        assert_eq!(Value::TimeTicks(300).as_u64(), Some(300));
        assert_eq!(Value::Integer(50).as_u64(), Some(50));
        assert_eq!(Value::Integer(-1).as_u64(), None);
    }

    #[test]
    fn test_as_bytes() {
        let s = Value::OctetString(Bytes::from_static(b"test"));
        assert_eq!(s.as_bytes(), Some(b"test".as_slice()));

        let o = Value::Opaque(Bytes::from_static(b"data"));
        assert_eq!(o.as_bytes(), Some(b"data".as_slice()));

        assert_eq!(Value::Integer(1).as_bytes(), None);
    }

    #[test]
    fn test_as_str() {
        let s = Value::OctetString(Bytes::from_static(b"hello"));
        assert_eq!(s.as_str(), Some("hello"));

        // Invalid UTF-8 returns None
        let invalid = Value::OctetString(Bytes::from_static(&[0xFF, 0xFE]));
        assert_eq!(invalid.as_str(), None);

        assert_eq!(Value::Integer(1).as_str(), None);
    }

    #[test]
    fn test_as_oid() {
        let oid = crate::oid!(1, 3, 6, 1);
        let v = Value::ObjectIdentifier(oid.clone());
        assert_eq!(v.as_oid(), Some(&oid));

        assert_eq!(Value::Integer(1).as_oid(), None);
    }

    #[test]
    fn test_as_ip() {
        let v = Value::IpAddress([192, 168, 1, 1]);
        assert_eq!(v.as_ip(), Some(std::net::Ipv4Addr::new(192, 168, 1, 1)));

        assert_eq!(Value::Integer(1).as_ip(), None);
    }

    // ========================================================================
    // is_exception() Tests
    // ========================================================================

    #[test]
    fn test_is_exception() {
        assert!(Value::NoSuchObject.is_exception());
        assert!(Value::NoSuchInstance.is_exception());
        assert!(Value::EndOfMibView.is_exception());

        assert!(!Value::Integer(1).is_exception());
        assert!(!Value::Null.is_exception());
        assert!(!Value::OctetString(Bytes::new()).is_exception());
    }

    // ========================================================================
    // Display Trait Tests
    // ========================================================================

    #[test]
    fn test_display_integer() {
        assert_eq!(format!("{}", Value::Integer(42)), "42");
        assert_eq!(format!("{}", Value::Integer(-42)), "-42");
    }

    #[test]
    fn test_display_octet_string_utf8() {
        let v = Value::OctetString(Bytes::from_static(b"hello"));
        assert_eq!(format!("{}", v), "hello");
    }

    #[test]
    fn test_display_octet_string_binary() {
        // Use bytes that are not valid UTF-8 (0xFF is never valid in UTF-8)
        let v = Value::OctetString(Bytes::from_static(&[0xFF, 0xFE]));
        assert_eq!(format!("{}", v), "0xfffe");
    }

    #[test]
    fn test_display_null() {
        assert_eq!(format!("{}", Value::Null), "NULL");
    }

    #[test]
    fn test_display_ip_address() {
        let v = Value::IpAddress([192, 168, 1, 1]);
        assert_eq!(format!("{}", v), "192.168.1.1");
    }

    #[test]
    fn test_display_counter32() {
        assert_eq!(format!("{}", Value::Counter32(999)), "999");
    }

    #[test]
    fn test_display_gauge32() {
        assert_eq!(format!("{}", Value::Gauge32(1000)), "1000");
    }

    #[test]
    fn test_display_timeticks() {
        // 123456 hundredths = 1234.56 seconds = 20m 34.56s
        let v = Value::TimeTicks(123456);
        assert_eq!(format!("{}", v), "00:20:34.56");
    }

    #[test]
    fn test_display_opaque() {
        let v = Value::Opaque(Bytes::from_static(&[0xBE, 0xEF]));
        assert_eq!(format!("{}", v), "Opaque(0xbeef)");
    }

    #[test]
    fn test_display_counter64() {
        assert_eq!(format!("{}", Value::Counter64(12345678)), "12345678");
    }

    #[test]
    fn test_display_exceptions() {
        assert_eq!(format!("{}", Value::NoSuchObject), "noSuchObject");
        assert_eq!(format!("{}", Value::NoSuchInstance), "noSuchInstance");
        assert_eq!(format!("{}", Value::EndOfMibView), "endOfMibView");
    }

    #[test]
    fn test_display_unknown() {
        let v = Value::Unknown {
            tag: 0x99,
            data: Bytes::from_static(&[0x01, 0x02]),
        };
        assert_eq!(format!("{}", v), "Unknown(tag=0x99, data=0x0102)");
    }

    // ========================================================================
    // From Conversion Tests
    // ========================================================================

    #[test]
    fn test_from_i32() {
        let v: Value = 42i32.into();
        assert_eq!(v, Value::Integer(42));
    }

    #[test]
    fn test_from_str() {
        let v: Value = "hello".into();
        assert_eq!(v.as_str(), Some("hello"));
    }

    #[test]
    fn test_from_string() {
        let v: Value = String::from("hello").into();
        assert_eq!(v.as_str(), Some("hello"));
    }

    #[test]
    fn test_from_bytes_slice() {
        let v: Value = (&[1u8, 2, 3][..]).into();
        assert_eq!(v.as_bytes(), Some(&[1u8, 2, 3][..]));
    }

    #[test]
    fn test_from_oid() {
        let oid = crate::oid!(1, 3, 6, 1);
        let v: Value = oid.clone().into();
        assert_eq!(v.as_oid(), Some(&oid));
    }

    #[test]
    fn test_from_ipv4addr() {
        let addr = std::net::Ipv4Addr::new(10, 0, 0, 1);
        let v: Value = addr.into();
        assert_eq!(v, Value::IpAddress([10, 0, 0, 1]));
    }

    #[test]
    fn test_from_bytes() {
        let data = Bytes::from_static(b"hello");
        let v: Value = data.into();
        assert_eq!(v.as_bytes(), Some(b"hello".as_slice()));
    }

    #[test]
    fn test_from_u64() {
        let v: Value = 12345678901234u64.into();
        assert_eq!(v, Value::Counter64(12345678901234));
    }

    #[test]
    fn test_from_ip_array() {
        let v: Value = [192u8, 168, 1, 1].into();
        assert_eq!(v, Value::IpAddress([192, 168, 1, 1]));
    }

    // ========================================================================
    // Eq and Hash Tests
    // ========================================================================

    #[test]
    fn test_value_eq_and_hash() {
        use std::collections::HashSet;

        let mut set = HashSet::new();
        set.insert(Value::Integer(42));
        set.insert(Value::Integer(42)); // Duplicate
        set.insert(Value::Integer(100));

        assert_eq!(set.len(), 2);
        assert!(set.contains(&Value::Integer(42)));
        assert!(set.contains(&Value::Integer(100)));
    }

    // ========================================================================
    // Decode Error Tests
    // ========================================================================

    #[test]
    fn test_decode_invalid_null_length() {
        // NULL must have length 0
        let data = Bytes::from_static(&[0x05, 0x01, 0x00]); // NULL with length 1
        let mut decoder = Decoder::new(data);
        let result = Value::decode(&mut decoder);
        assert!(result.is_err());
    }

    #[test]
    fn test_decode_invalid_ip_address_length() {
        // IpAddress must have length 4
        let data = Bytes::from_static(&[0x40, 0x03, 0x01, 0x02, 0x03]); // Only 3 bytes
        let mut decoder = Decoder::new(data);
        let result = Value::decode(&mut decoder);
        assert!(result.is_err());
    }

    #[test]
    fn test_decode_exception_with_content_accepted() {
        // Per implementation, exceptions with non-zero length have content skipped
        let data = Bytes::from_static(&[0x80, 0x01, 0xFF]); // NoSuchObject with 1 byte
        let mut decoder = Decoder::new(data);
        let result = Value::decode(&mut decoder);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), Value::NoSuchObject);
    }

    // ========================================================================
    // Numeric Extraction Method Tests
    // ========================================================================

    #[test]
    fn test_as_f64() {
        assert_eq!(Value::Integer(42).as_f64(), Some(42.0));
        assert_eq!(Value::Integer(-42).as_f64(), Some(-42.0));
        assert_eq!(Value::Counter32(1000).as_f64(), Some(1000.0));
        assert_eq!(Value::Gauge32(2000).as_f64(), Some(2000.0));
        assert_eq!(Value::TimeTicks(3000).as_f64(), Some(3000.0));
        assert_eq!(
            Value::Counter64(10_000_000_000).as_f64(),
            Some(10_000_000_000.0)
        );
        assert_eq!(Value::Null.as_f64(), None);
        assert_eq!(
            Value::OctetString(Bytes::from_static(b"test")).as_f64(),
            None
        );
    }

    #[test]
    fn test_as_f64_wrapped() {
        // Small values behave same as as_f64()
        assert_eq!(Value::Counter64(1000).as_f64_wrapped(), Some(1000.0));
        assert_eq!(Value::Counter32(1000).as_f64_wrapped(), Some(1000.0));
        assert_eq!(Value::Integer(42).as_f64_wrapped(), Some(42.0));

        // Large Counter64 wraps at 2^53
        let mantissa_limit = 1u64 << 53;
        assert_eq!(Value::Counter64(mantissa_limit).as_f64_wrapped(), Some(0.0));
        assert_eq!(
            Value::Counter64(mantissa_limit + 1).as_f64_wrapped(),
            Some(1.0)
        );
    }

    #[test]
    fn test_as_decimal() {
        assert_eq!(Value::Integer(2350).as_decimal(2), Some(23.50));
        assert_eq!(Value::Integer(9999).as_decimal(2), Some(99.99));
        assert_eq!(Value::Integer(12500).as_decimal(3), Some(12.5));
        assert_eq!(Value::Integer(-500).as_decimal(2), Some(-5.0));
        assert_eq!(Value::Counter32(1000).as_decimal(1), Some(100.0));
        assert_eq!(Value::Null.as_decimal(2), None);
    }

    #[test]
    fn test_as_duration() {
        use std::time::Duration;

        // 100 ticks = 1 second
        assert_eq!(
            Value::TimeTicks(100).as_duration(),
            Some(Duration::from_secs(1))
        );
        // 360000 ticks = 3600 seconds = 1 hour
        assert_eq!(
            Value::TimeTicks(360000).as_duration(),
            Some(Duration::from_secs(3600))
        );
        // 1 tick = 10 milliseconds
        assert_eq!(
            Value::TimeTicks(1).as_duration(),
            Some(Duration::from_millis(10))
        );

        // Non-TimeTicks return None
        assert_eq!(Value::Integer(100).as_duration(), None);
        assert_eq!(Value::Counter32(100).as_duration(), None);
    }

    // ========================================================================
    // Opaque Sub-type Extraction Tests
    // ========================================================================

    #[test]
    fn test_as_opaque_float() {
        // Opaque-encoded float for pi ≈ 3.14159
        // 0x40490fdb is IEEE 754 single-precision for ~3.14159
        let data = Bytes::from_static(&[0x9f, 0x78, 0x04, 0x40, 0x49, 0x0f, 0xdb]);
        let value = Value::Opaque(data);
        let pi = value.as_opaque_float().unwrap();
        assert!((pi - std::f32::consts::PI).abs() < 0.0001);

        // Non-Opaque returns None
        assert_eq!(Value::Integer(42).as_opaque_float(), None);

        // Wrong subtype returns None
        let wrong_type = Bytes::from_static(&[0x9f, 0x79, 0x04, 0x40, 0x49, 0x0f, 0xdb]);
        assert_eq!(Value::Opaque(wrong_type).as_opaque_float(), None);

        // Too short returns None
        let short = Bytes::from_static(&[0x9f, 0x78, 0x04, 0x40, 0x49]);
        assert_eq!(Value::Opaque(short).as_opaque_float(), None);
    }

    #[test]
    fn test_as_opaque_double() {
        // Opaque-encoded double for pi
        // 0x400921fb54442d18 is IEEE 754 double-precision for pi
        let data = Bytes::from_static(&[
            0x9f, 0x79, 0x08, 0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18,
        ]);
        let value = Value::Opaque(data);
        let pi = value.as_opaque_double().unwrap();
        assert!((pi - std::f64::consts::PI).abs() < 1e-10);

        // Non-Opaque returns None
        assert_eq!(Value::Integer(42).as_opaque_double(), None);
    }

    #[test]
    fn test_as_opaque_counter64() {
        // 8-byte Counter64
        let data = Bytes::from_static(&[
            0x9f, 0x76, 0x08, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
        ]);
        let value = Value::Opaque(data);
        assert_eq!(value.as_opaque_counter64(), Some(0x0123456789ABCDEF));

        // Shorter encoding (e.g., small value)
        let small = Bytes::from_static(&[0x9f, 0x76, 0x01, 0x42]);
        assert_eq!(Value::Opaque(small).as_opaque_counter64(), Some(0x42));

        // Zero
        let zero = Bytes::from_static(&[0x9f, 0x76, 0x01, 0x00]);
        assert_eq!(Value::Opaque(zero).as_opaque_counter64(), Some(0));
    }

    #[test]
    fn test_as_opaque_i64() {
        // Positive value
        let positive = Bytes::from_static(&[0x9f, 0x7a, 0x02, 0x01, 0x00]);
        assert_eq!(Value::Opaque(positive).as_opaque_i64(), Some(256));

        // Negative value (-1)
        let minus_one = Bytes::from_static(&[0x9f, 0x7a, 0x01, 0xFF]);
        assert_eq!(Value::Opaque(minus_one).as_opaque_i64(), Some(-1));

        // Full 8-byte negative
        let full_neg = Bytes::from_static(&[
            0x9f, 0x7a, 0x08, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        ]);
        assert_eq!(Value::Opaque(full_neg).as_opaque_i64(), Some(-1));

        // i64::MIN
        let min = Bytes::from_static(&[
            0x9f, 0x7a, 0x08, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        ]);
        assert_eq!(Value::Opaque(min).as_opaque_i64(), Some(i64::MIN));
    }

    #[test]
    fn test_as_opaque_u64() {
        // 8-byte U64
        let data = Bytes::from_static(&[
            0x9f, 0x7b, 0x08, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
        ]);
        let value = Value::Opaque(data);
        assert_eq!(value.as_opaque_u64(), Some(0x0123456789ABCDEF));

        // u64::MAX
        let max = Bytes::from_static(&[
            0x9f, 0x7b, 0x08, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        ]);
        assert_eq!(Value::Opaque(max).as_opaque_u64(), Some(u64::MAX));
    }

    #[test]
    fn test_format_with_hint_integer() {
        // Decimal places
        assert_eq!(
            Value::Integer(2350).format_with_hint("d-2"),
            Some("23.50".into())
        );
        assert_eq!(
            Value::Integer(-500).format_with_hint("d-2"),
            Some("-5.00".into())
        );

        // Basic formats
        assert_eq!(Value::Integer(255).format_with_hint("x"), Some("ff".into()));
        assert_eq!(Value::Integer(8).format_with_hint("o"), Some("10".into()));
        assert_eq!(Value::Integer(5).format_with_hint("b"), Some("101".into()));
        assert_eq!(Value::Integer(42).format_with_hint("d"), Some("42".into()));

        // Invalid hint
        assert_eq!(Value::Integer(42).format_with_hint("invalid"), None);

        // Counter32 etc. still return None (only Integer supported for INTEGER hints)
        assert_eq!(Value::Counter32(42).format_with_hint("d-2"), None);
    }

    #[test]
    fn test_as_truth_value() {
        // Valid TruthValue
        assert_eq!(Value::Integer(1).as_truth_value(), Some(true));
        assert_eq!(Value::Integer(2).as_truth_value(), Some(false));

        // Invalid integers
        assert_eq!(Value::Integer(0).as_truth_value(), None);
        assert_eq!(Value::Integer(3).as_truth_value(), None);
        assert_eq!(Value::Integer(-1).as_truth_value(), None);

        // Non-Integer types
        assert_eq!(Value::Null.as_truth_value(), None);
        assert_eq!(Value::Counter32(1).as_truth_value(), None);
        assert_eq!(Value::Gauge32(1).as_truth_value(), None);
    }

    // ========================================================================
    // RowStatus Tests
    // ========================================================================

    #[test]
    fn test_row_status_from_i32() {
        assert_eq!(RowStatus::from_i32(1), Some(RowStatus::Active));
        assert_eq!(RowStatus::from_i32(2), Some(RowStatus::NotInService));
        assert_eq!(RowStatus::from_i32(3), Some(RowStatus::NotReady));
        assert_eq!(RowStatus::from_i32(4), Some(RowStatus::CreateAndGo));
        assert_eq!(RowStatus::from_i32(5), Some(RowStatus::CreateAndWait));
        assert_eq!(RowStatus::from_i32(6), Some(RowStatus::Destroy));

        // Invalid values
        assert_eq!(RowStatus::from_i32(0), None);
        assert_eq!(RowStatus::from_i32(7), None);
        assert_eq!(RowStatus::from_i32(-1), None);
    }

    #[test]
    fn test_row_status_try_from() {
        assert_eq!(RowStatus::try_from(1), Ok(RowStatus::Active));
        assert_eq!(RowStatus::try_from(6), Ok(RowStatus::Destroy));
        assert_eq!(RowStatus::try_from(0), Err(0));
        assert_eq!(RowStatus::try_from(7), Err(7));
        assert_eq!(RowStatus::try_from(-1), Err(-1));
    }

    #[test]
    fn test_row_status_into_value() {
        let v: Value = RowStatus::Active.into();
        assert_eq!(v, Value::Integer(1));

        let v: Value = RowStatus::Destroy.into();
        assert_eq!(v, Value::Integer(6));
    }

    #[test]
    fn test_row_status_display() {
        assert_eq!(format!("{}", RowStatus::Active), "active");
        assert_eq!(format!("{}", RowStatus::NotInService), "notInService");
        assert_eq!(format!("{}", RowStatus::NotReady), "notReady");
        assert_eq!(format!("{}", RowStatus::CreateAndGo), "createAndGo");
        assert_eq!(format!("{}", RowStatus::CreateAndWait), "createAndWait");
        assert_eq!(format!("{}", RowStatus::Destroy), "destroy");
    }

    #[test]
    fn test_as_row_status() {
        // Valid RowStatus values
        assert_eq!(Value::Integer(1).as_row_status(), Some(RowStatus::Active));
        assert_eq!(Value::Integer(6).as_row_status(), Some(RowStatus::Destroy));

        // Invalid integers
        assert_eq!(Value::Integer(0).as_row_status(), None);
        assert_eq!(Value::Integer(7).as_row_status(), None);

        // Non-Integer types
        assert_eq!(Value::Null.as_row_status(), None);
        assert_eq!(Value::Counter32(1).as_row_status(), None);
    }

    // ========================================================================
    // StorageType Tests
    // ========================================================================

    #[test]
    fn test_storage_type_from_i32() {
        assert_eq!(StorageType::from_i32(1), Some(StorageType::Other));
        assert_eq!(StorageType::from_i32(2), Some(StorageType::Volatile));
        assert_eq!(StorageType::from_i32(3), Some(StorageType::NonVolatile));
        assert_eq!(StorageType::from_i32(4), Some(StorageType::Permanent));
        assert_eq!(StorageType::from_i32(5), Some(StorageType::ReadOnly));

        // Invalid values
        assert_eq!(StorageType::from_i32(0), None);
        assert_eq!(StorageType::from_i32(6), None);
        assert_eq!(StorageType::from_i32(-1), None);
    }

    #[test]
    fn test_storage_type_try_from() {
        assert_eq!(StorageType::try_from(1), Ok(StorageType::Other));
        assert_eq!(StorageType::try_from(5), Ok(StorageType::ReadOnly));
        assert_eq!(StorageType::try_from(0), Err(0));
        assert_eq!(StorageType::try_from(6), Err(6));
        assert_eq!(StorageType::try_from(-1), Err(-1));
    }

    #[test]
    fn test_storage_type_into_value() {
        let v: Value = StorageType::Volatile.into();
        assert_eq!(v, Value::Integer(2));

        let v: Value = StorageType::NonVolatile.into();
        assert_eq!(v, Value::Integer(3));
    }

    #[test]
    fn test_storage_type_display() {
        assert_eq!(format!("{}", StorageType::Other), "other");
        assert_eq!(format!("{}", StorageType::Volatile), "volatile");
        assert_eq!(format!("{}", StorageType::NonVolatile), "nonVolatile");
        assert_eq!(format!("{}", StorageType::Permanent), "permanent");
        assert_eq!(format!("{}", StorageType::ReadOnly), "readOnly");
    }

    #[test]
    fn test_as_storage_type() {
        // Valid StorageType values
        assert_eq!(
            Value::Integer(2).as_storage_type(),
            Some(StorageType::Volatile)
        );
        assert_eq!(
            Value::Integer(3).as_storage_type(),
            Some(StorageType::NonVolatile)
        );

        // Invalid integers
        assert_eq!(Value::Integer(0).as_storage_type(), None);
        assert_eq!(Value::Integer(6).as_storage_type(), None);

        // Non-Integer types
        assert_eq!(Value::Null.as_storage_type(), None);
        assert_eq!(Value::Counter32(1).as_storage_type(), None);
    }
}