Line data Source code
1 : /// Useful type for asserting that expected bytes match reporting the bytes more readable
2 : /// array-syntax compatible hex bytes.
3 : ///
4 : /// # Usage
5 : ///
6 : /// ```
7 : /// use utils::Hex;
8 : ///
9 : /// let actual = serialize_something();
10 : /// let expected = [0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64];
11 : ///
12 : /// // the type implements PartialEq and on mismatch, both sides are printed in 16 wide multiline
13 : /// // output suffixed with an array style length for easier comparisons.
14 : /// assert_eq!(Hex(&actual), Hex(&expected));
15 : ///
16 : /// // with `let expected = [0x68];` the error would had been:
17 : /// // assertion `left == right` failed
18 : /// // left: [0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64; 11]
19 : /// // right: [0x68; 1]
20 : /// # fn serialize_something() -> Vec<u8> { "hello world".as_bytes().to_vec() }
21 : /// ```
22 20 : #[derive(PartialEq)]
23 : pub struct Hex<'a>(pub &'a [u8]);
24 :
25 : impl std::fmt::Debug for Hex<'_> {
26 0 : fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27 0 : write!(f, "[")?;
28 0 : for (i, c) in self.0.chunks(16).enumerate() {
29 0 : if i > 0 && !c.is_empty() {
30 0 : writeln!(f, ", ")?;
31 0 : }
32 0 : for (j, b) in c.iter().enumerate() {
33 0 : if j > 0 {
34 0 : write!(f, ", ")?;
35 0 : }
36 0 : write!(f, "0x{b:02x}")?;
37 : }
38 : }
39 0 : write!(f, "; {}]", self.0.len())
40 0 : }
41 : }
|