helpers/
truncated_display.rs1use std::fmt::{self, Debug, Display};
4
5pub trait TruncatedDisplay {
7 fn truncated_display(&self, max_length: usize) -> impl Debug + Display;
13}
14
15impl TruncatedDisplay for &str {
16 fn truncated_display(&self, max_length: usize) -> impl Debug + Display {
17 TruncatedDisplayStr {
18 string: self,
19 max_length,
20 }
21 }
22}
23
24impl TruncatedDisplay for String {
25 fn truncated_display(&self, max_length: usize) -> impl Debug + Display {
26 TruncatedDisplayStr {
27 string: self,
28 max_length,
29 }
30 }
31}
32
33struct TruncatedDisplayStr<'a> {
35 string: &'a str,
37 max_length: usize,
39}
40
41impl Debug for TruncatedDisplayStr<'_> {
42 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43 if self.max_length == 0 {
44 return Ok(());
45 }
46
47 let mut indices = self.string.char_indices().skip(self.max_length - 1);
48 if let Some(cutoff) = indices.next()
49 && indices.next().is_some()
50 {
51 write!(f, "{:?}…", &self.string[..cutoff.0])
52 } else {
53 write!(f, "{:?}", self.string)
54 }
55 }
56}
57
58impl Display for TruncatedDisplayStr<'_> {
59 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60 if self.max_length == 0 {
61 return Ok(());
62 }
63
64 let mut indices = self.string.char_indices().skip(self.max_length - 1);
65 if let Some(cutoff) = indices.next()
66 && indices.next().is_some()
67 {
68 write!(f, "{}…", &self.string[..cutoff.0])
69 } else {
70 write!(f, "{}", self.string)
71 }
72 }
73}