async_snmp/format/hints.rs
1//! Pre-defined DISPLAY-HINT constants for common SNMP types.
2//!
3//! These constants can be used with [`Value::format_with_hint()`](crate::Value::format_with_hint)
4//! to format values according to their MIB definitions without looking up hints.
5//!
6//! # Example
7//!
8//! ```
9//! use async_snmp::format::hints;
10//! use async_snmp::Value;
11//! use bytes::Bytes;
12//!
13//! let mac = Value::OctetString(Bytes::from_static(&[0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5e]));
14//! assert_eq!(mac.format_with_hint(hints::MAC_ADDRESS), Some("00:1a:2b:3c:4d:5e".to_string()));
15//! ```
16
17/// MAC address: "1x:" - each byte as hex separated by colons.
18///
19/// Used by SNMPv2-TC::MacAddress and many physical address fields.
20pub const MAC_ADDRESS: &str = "1x:";
21
22/// Display string (UTF-8): "255a" - up to 255 ASCII/UTF-8 characters.
23///
24/// Used by SNMPv2-TC::DisplayString, SNMPv2-MIB::sysDescr, etc.
25pub const DISPLAY_STRING: &str = "255a";
26
27/// Date and time: "2d-1d-1d,1d:1d:1d.1d,1a1d:1d".
28///
29/// Used by SNMPv2-TC::DateAndTime (8 or 11 bytes).
30/// Format: YYYY-MM-DD,HH:MM:SS.d,+/-HH:MM
31pub const DATE_AND_TIME: &str = "2d-1d-1d,1d:1d:1d.1d,1a1d:1d";
32
33/// Hexadecimal string: "1x" - each byte as two hex digits.
34///
35/// Common format for binary data that should display as hex.
36pub const HEX_STRING: &str = "1x";
37
38/// Hexadecimal with spaces: "1x " - each byte as hex separated by spaces.
39///
40/// Alternative hex format sometimes used for readability.
41pub const HEX_STRING_SPACED: &str = "1x ";
42
43/// Dotted decimal: "1d." - each byte as decimal separated by dots.
44///
45/// Used for IP addresses and similar dotted notations.
46pub const DOTTED_DECIMAL: &str = "1d.";
47
48/// UTF-8 string: "255t" - up to 255 UTF-8 encoded characters.
49///
50/// For explicitly UTF-8 encoded strings.
51pub const UTF8_STRING: &str = "255t";
52
53/// Octet string as binary bits: "1b" - each byte as 8 binary digits.
54///
55/// Useful for displaying bitmasks and flags.
56pub const BINARY_STRING: &str = "1b";
57
58/// Integer as hex: "x" - integer value in lowercase hexadecimal.
59pub const INTEGER_HEX: &str = "x";
60
61/// Integer with 1 decimal place: "d-1".
62///
63/// Common for tenths (e.g., temperatures in 0.1 degree units).
64pub const DECIMAL_1: &str = "d-1";
65
66/// Integer with 2 decimal places: "d-2".
67///
68/// Common for hundredths (e.g., percentages as 0-10000).
69pub const DECIMAL_2: &str = "d-2";
70
71/// Integer with 3 decimal places: "d-3".
72///
73/// Common for thousandths (e.g., voltages in millivolts).
74pub const DECIMAL_3: &str = "d-3";
75
76#[cfg(test)]
77mod tests {
78 use super::*;
79 use crate::Value;
80 use bytes::Bytes;
81
82 #[test]
83 fn test_mac_address_hint() {
84 let mac = Value::OctetString(Bytes::from_static(&[0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5e]));
85 assert_eq!(
86 mac.format_with_hint(MAC_ADDRESS),
87 Some("00:1a:2b:3c:4d:5e".to_string())
88 );
89 }
90
91 #[test]
92 fn test_display_string_hint() {
93 let desc = Value::OctetString(Bytes::from_static(b"Hello World"));
94 assert_eq!(
95 desc.format_with_hint(DISPLAY_STRING),
96 Some("Hello World".to_string())
97 );
98 }
99
100 #[test]
101 fn test_hex_string_hint() {
102 let data = Value::OctetString(Bytes::from_static(&[0xDE, 0xAD, 0xBE, 0xEF]));
103 assert_eq!(
104 data.format_with_hint(HEX_STRING),
105 Some("deadbeef".to_string())
106 );
107 }
108
109 #[test]
110 fn test_dotted_decimal_hint() {
111 let ip = Value::OctetString(Bytes::from_static(&[192, 168, 1, 1]));
112 assert_eq!(
113 ip.format_with_hint(DOTTED_DECIMAL),
114 Some("192.168.1.1".to_string())
115 );
116 }
117
118 #[test]
119 fn test_integer_decimal_hints() {
120 assert_eq!(
121 Value::Integer(2350).format_with_hint(DECIMAL_2),
122 Some("23.50".to_string())
123 );
124 assert_eq!(
125 Value::Integer(1234).format_with_hint(DECIMAL_1),
126 Some("123.4".to_string())
127 );
128 assert_eq!(
129 Value::Integer(12500).format_with_hint(DECIMAL_3),
130 Some("12.500".to_string())
131 );
132 }
133
134 #[test]
135 fn test_integer_hex_hint() {
136 assert_eq!(
137 Value::Integer(255).format_with_hint(INTEGER_HEX),
138 Some("ff".to_string())
139 );
140 }
141}