1use std::fmt::{Debug, Formatter, Result};
7
8use crate::{Prefix, PrefixMap};
9
10impl<P: Prefix + Debug, T: Debug> Debug for PrefixMap<P, T> {
11 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
12 let entries: Vec<(P, &T)> = self.iter().collect();
13 DebugSubtree { entries: &entries }.fmt(f)
14 }
15}
16
17#[cfg(feature = "rkyv")]
18impl<P, T> Debug for crate::rkyv::ArchivedPrefixMap<P, T>
19where
20 P: Prefix + Debug,
21 T: rkyv::Archive,
22 T::Archived: Debug,
23{
24 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
25 let entries: Vec<(P, &T::Archived)> = self.iter().collect();
26 DebugSubtree { entries: &entries }.fmt(f)
27 }
28}
29
30struct DebugSubtree<'a, P, T> {
31 entries: &'a [(P, &'a T)],
32}
33
34struct DebugExtendedNode<'a, P, T> {
35 value: &'a T,
36 children: DebugSubtree<'a, P, T>,
37}
38
39impl<P: Prefix + Debug, T: Debug> Debug for DebugExtendedNode<'_, P, T> {
40 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
41 let mut d = f.debug_struct("");
42 d.field("value", self.value);
43 if !self.children.entries.is_empty() {
44 d.field("children", &self.children);
45 }
46 d.finish()
47 }
48}
49
50impl<P: Prefix + Debug, T: Debug> Debug for DebugSubtree<'_, P, T> {
51 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
52 let mut dm = f.debug_map();
53 let mut entries = self.entries;
54
55 while let Some(((prefix, value), rest)) = entries.split_first() {
56 let child_count = rest.iter().take_while(|(p, _)| prefix.contains(p)).count();
57 let (children, remaining) = rest.split_at(child_count);
58 entries = remaining;
59
60 dm.entry(
61 prefix,
62 &DebugExtendedNode {
63 value: *value,
64 children: DebugSubtree { entries: children },
65 },
66 );
67 }
68
69 dm.finish()
70 }
71}