|
1 | 1 | use crate::coding::{Decode, DecodeError, Encode, EncodeError}; |
2 | 2 | use std::collections::HashMap; |
| 3 | +use std::fmt; |
3 | 4 |
|
4 | | -#[derive(Debug, Clone, Eq, PartialEq)] |
| 5 | +#[derive(Clone, Eq, PartialEq)] |
5 | 6 | pub enum Value { |
6 | 7 | IntValue(u64), |
7 | 8 | BytesValue(Vec<u8>), |
8 | 9 | } |
9 | 10 |
|
10 | | -#[derive(Debug, Clone, Eq, PartialEq)] |
| 11 | +impl fmt::Debug for Value { |
| 12 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 13 | + match self { |
| 14 | + Value::IntValue(v) => write!(f, "{}", v), |
| 15 | + Value::BytesValue(bytes) => { |
| 16 | + // Show up to 16 bytes in hex for readability |
| 17 | + let preview: Vec<String> = bytes |
| 18 | + .iter() |
| 19 | + .take(16) |
| 20 | + .map(|b| format!("{:02X}", b)) |
| 21 | + .collect(); |
| 22 | + write!(f, "[{}]", preview.join(" ")) |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +#[derive(Clone, Eq, PartialEq)] |
11 | 29 | pub struct KeyValuePair { |
12 | 30 | pub key: u64, |
13 | 31 | pub value: Value, |
@@ -83,7 +101,13 @@ impl Encode for KeyValuePair { |
83 | 101 | } |
84 | 102 | } |
85 | 103 |
|
86 | | -#[derive(Default, Debug, Clone, Eq, PartialEq)] |
| 104 | +impl fmt::Debug for KeyValuePair { |
| 105 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 106 | + write!(f, "{{{}: {:?}}}", self.key, self.value) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +#[derive(Default, Clone, Eq, PartialEq)] |
87 | 111 | pub struct KeyValuePairs(pub HashMap<u64, KeyValuePair>); |
88 | 112 |
|
89 | 113 | impl KeyValuePairs { |
@@ -141,6 +165,20 @@ impl Encode for KeyValuePairs { |
141 | 165 | } |
142 | 166 | } |
143 | 167 |
|
| 168 | +impl fmt::Debug for KeyValuePairs { |
| 169 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 170 | + write!(f, "{{ ")?; |
| 171 | + let pairs: Vec<_> = self.0.iter().collect(); |
| 172 | + for (i, (_key, kv)) in pairs.iter().enumerate() { |
| 173 | + if i > 0 { |
| 174 | + write!(f, ", ")?; |
| 175 | + } |
| 176 | + write!(f, "{:?}", kv)?; |
| 177 | + } |
| 178 | + write!(f, " }}") |
| 179 | + } |
| 180 | +} |
| 181 | + |
144 | 182 | #[cfg(test)] |
145 | 183 | mod tests { |
146 | 184 | use super::*; |
|
0 commit comments