Skip to content

Commit 7e10172

Browse files
authored
Merge pull request #95 from sgodin/mlog-cleanup
2 parents f488b5c + 8a66be2 commit 7e10172

File tree

2 files changed

+292
-292
lines changed

2 files changed

+292
-292
lines changed

moq-transport/src/coding/kvp.rs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
use crate::coding::{Decode, DecodeError, Encode, EncodeError};
22
use std::collections::HashMap;
3+
use std::fmt;
34

4-
#[derive(Debug, Clone, Eq, PartialEq)]
5+
#[derive(Clone, Eq, PartialEq)]
56
pub enum Value {
67
IntValue(u64),
78
BytesValue(Vec<u8>),
89
}
910

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)]
1129
pub struct KeyValuePair {
1230
pub key: u64,
1331
pub value: Value,
@@ -83,7 +101,13 @@ impl Encode for KeyValuePair {
83101
}
84102
}
85103

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)]
87111
pub struct KeyValuePairs(pub HashMap<u64, KeyValuePair>);
88112

89113
impl KeyValuePairs {
@@ -141,6 +165,20 @@ impl Encode for KeyValuePairs {
141165
}
142166
}
143167

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+
144182
#[cfg(test)]
145183
mod tests {
146184
use super::*;

0 commit comments

Comments
 (0)