Skip to main content

prefix_trie/
fmt.rs

1//! Formatting implementation for the PrefixMap
2
3use std::fmt::{Debug, Formatter, Result};
4
5use super::*;
6
7impl<P: Debug, T: Debug> Debug for PrefixMap<P, T> {
8    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
9        DebugPrefixMap(self, 0).fmt(f)
10    }
11}
12
13struct DebugPrefixMap<'a, P, T>(&'a PrefixMap<P, T>, usize);
14
15impl<P: Debug, T: Debug> Debug for DebugPrefixMap<'_, P, T> {
16    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
17        let map = self.0;
18        let idx = self.1;
19        let node = &map.table[idx];
20        match (node.value.as_ref(), node.left, node.right) {
21            (None, None, None) => node.prefix.fmt(f),
22            (None, None, Some(child)) | (None, Some(child), None) => f
23                .debug_map()
24                .entry(&node.prefix, &DebugPrefixMap(map, child.get()))
25                .finish(),
26            (None, Some(left), Some(right)) => f
27                .debug_map()
28                .entry(
29                    &node.prefix,
30                    &(
31                        DebugPrefixMap(map, left.get()),
32                        DebugPrefixMap(map, right.get()),
33                    ),
34                )
35                .finish(),
36            (Some(v), None, None) => f.debug_map().entry(&node.prefix, v).finish(),
37            (Some(v), None, Some(child)) | (Some(v), Some(child), None) => f
38                .debug_map()
39                .entry(&node.prefix, &(v, DebugPrefixMap(map, child.get())))
40                .finish(),
41            (Some(v), Some(left), Some(right)) => f
42                .debug_map()
43                .entry(
44                    &node.prefix,
45                    &(
46                        v,
47                        DebugPrefixMap(map, left.get()),
48                        DebugPrefixMap(map, right.get()),
49                    ),
50                )
51                .finish(),
52        }
53    }
54}
55
56impl<P: Debug> Debug for PrefixSet<P> {
57    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
58        DebugPrefixMap(&self.0, 0).fmt(f)
59    }
60}