Skip to content

Commit e7f2785

Browse files
authored
Better implementation of Debug trait for TopicPartitionList (#613)
1 parent d3879c2 commit e7f2785

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

src/topic_partition_list.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ impl Offset {
7878
}
7979
}
8080

81-
// TODO: implement Debug
8281
/// One element of the topic partition list.
8382
pub struct TopicPartitionListElem<'a> {
8483
ptr: &'a mut RDKafkaTopicPartition,
@@ -165,6 +164,18 @@ impl<'a> PartialEq for TopicPartitionListElem<'a> {
165164
}
166165
}
167166

167+
impl fmt::Debug for TopicPartitionListElem<'_> {
168+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
169+
f.debug_struct("TopicPartitionListElem")
170+
.field("topic", &self.topic())
171+
.field("partition", &self.partition())
172+
.field("offset", &self.offset())
173+
.field("metadata", &self.metadata())
174+
.field("error", &self.error())
175+
.finish()
176+
}
177+
}
178+
168179
/// A structure to store and manipulate a list of topics and partitions with optional offsets.
169180
pub struct TopicPartitionList {
170181
ptr: NativePtr<RDKafkaTopicPartitionList>,
@@ -389,22 +400,7 @@ impl Default for TopicPartitionList {
389400

390401
impl fmt::Debug for TopicPartitionList {
391402
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
392-
write!(f, "TPL {{")?;
393-
for (i, elem) in self.elements().iter().enumerate() {
394-
if i > 0 {
395-
write!(f, "; ")?;
396-
}
397-
write!(
398-
f,
399-
"{}/{}: offset={:?} metadata={:?}, error={:?}",
400-
elem.topic(),
401-
elem.partition(),
402-
elem.offset(),
403-
elem.metadata(),
404-
elem.error(),
405-
)?;
406-
}
407-
write!(f, "}}")
403+
f.debug_list().entries(self.elements()).finish()
408404
}
409405
}
410406

tests/test_topic_partition_list.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use rdkafka::{Offset, TopicPartitionList};
2+
3+
/// Test topic partition list API and wrappers.
4+
5+
#[test]
6+
fn test_fmt_debug() {
7+
{
8+
let tpl = TopicPartitionList::new();
9+
assert_eq!(format!("{tpl:?}"), "[]");
10+
}
11+
12+
{
13+
let mut tpl = TopicPartitionList::new();
14+
tpl.add_topic_unassigned("foo");
15+
tpl.add_partition("bar", 8);
16+
tpl.add_partition_offset("bar", 7, Offset::Offset(42))
17+
.unwrap();
18+
assert_eq!(
19+
format!("{tpl:?}"),
20+
"[TopicPartitionListElem { topic: \"foo\", partition: -1, offset: Invalid, metadata: \"\", error: Ok(()) }, \
21+
TopicPartitionListElem { topic: \"bar\", partition: 8, offset: Invalid, metadata: \"\", error: Ok(()) }, \
22+
TopicPartitionListElem { topic: \"bar\", partition: 7, offset: Offset(42), metadata: \"\", error: Ok(()) }]");
23+
}
24+
}

0 commit comments

Comments
 (0)