1use std::fmt;
2
3use crate::{encode::fast_serialize, Hex, UpperHex, LOWER, UPPER};
4
5macro_rules! impl_fmt {
6 ($Hex:ident, $Trait:ident, $case:ident, $FmtTrait:ident) => {
7 impl<T> fmt::$Trait for $Hex<T>
8 where
9 T: AsRef<[u8]> + ?Sized,
10 {
11 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
12 fast_serialize::<_, _, $case>(&self.0, |s| fmt::$FmtTrait::fmt(s, f))
13 }
14 }
15 };
16}
17
18impl_fmt!(Hex, Display, LOWER, Display);
19impl_fmt!(Hex, Debug, LOWER, Debug);
20impl_fmt!(Hex, LowerHex, LOWER, Display);
21impl_fmt!(Hex, UpperHex, UPPER, Display);
22
23impl_fmt!(UpperHex, Display, UPPER, Display);
24impl_fmt!(UpperHex, Debug, UPPER, Debug);
25impl_fmt!(UpperHex, LowerHex, LOWER, Display);
26impl_fmt!(UpperHex, UpperHex, UPPER, Display);
27
28#[test]
29fn test_lower() {
30 let hex = Hex([1_u8, 0x99, 0xff]);
31
32 assert_eq!(format!("{}", hex), "0199ff");
33 assert_eq!(format!("{:?}", hex), "\"0199ff\"");
34 assert_eq!(format!("{:x}", hex), "0199ff");
35 assert_eq!(format!("{:X}", hex), "0199FF");
36}
37
38#[test]
39fn test_upper() {
40 let hex = UpperHex([1_u8, 0x99, 0xff]);
41
42 assert_eq!(format!("{}", hex), "0199FF");
43 assert_eq!(format!("{:?}", hex), "\"0199FF\"");
44 assert_eq!(format!("{:x}", hex), "0199ff");
45 assert_eq!(format!("{:X}", hex), "0199FF");
46}