/rust/registry/src/github.com-1ecc6299db9ec823/anyhow-1.0.71/src/fmt.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use crate::chain::Chain; |
2 | | use crate::error::ErrorImpl; |
3 | | use crate::ptr::Ref; |
4 | | use core::fmt::{self, Debug, Write}; |
5 | | |
6 | | impl ErrorImpl { |
7 | | pub(crate) unsafe fn display(this: Ref<Self>, f: &mut fmt::Formatter) -> fmt::Result { |
8 | 0 | write!(f, "{}", Self::error(this))?; |
9 | | |
10 | 0 | if f.alternate() { |
11 | 0 | for cause in Self::chain(this).skip(1) { |
12 | 0 | write!(f, ": {}", cause)?; |
13 | | } |
14 | 0 | } |
15 | | |
16 | 0 | Ok(()) |
17 | 0 | } |
18 | | |
19 | 0 | pub(crate) unsafe fn debug(this: Ref<Self>, f: &mut fmt::Formatter) -> fmt::Result { |
20 | 0 | let error = Self::error(this); |
21 | 0 |
|
22 | 0 | if f.alternate() { |
23 | 0 | return Debug::fmt(error, f); |
24 | 0 | } |
25 | 0 |
|
26 | 0 | write!(f, "{}", error)?; |
27 | | |
28 | 0 | if let Some(cause) = error.source() { |
29 | 0 | write!(f, "\n\nCaused by:")?; |
30 | 0 | let multiple = cause.source().is_some(); |
31 | 0 | for (n, error) in Chain::new(cause).enumerate() { |
32 | 0 | writeln!(f)?; |
33 | 0 | let mut indented = Indented { |
34 | 0 | inner: f, |
35 | 0 | number: if multiple { Some(n) } else { None }, |
36 | | started: false, |
37 | | }; |
38 | 0 | write!(indented, "{}", error)?; |
39 | | } |
40 | 0 | } |
41 | | |
42 | | #[cfg(any(backtrace, feature = "backtrace"))] |
43 | | { |
44 | | use crate::backtrace::BacktraceStatus; |
45 | | |
46 | 0 | let backtrace = Self::backtrace(this); |
47 | 0 | if let BacktraceStatus::Captured = backtrace.status() { |
48 | 0 | let mut backtrace = backtrace.to_string(); |
49 | 0 | write!(f, "\n\n")?; |
50 | 0 | if backtrace.starts_with("stack backtrace:") { |
51 | 0 | // Capitalize to match "Caused by:" |
52 | 0 | backtrace.replace_range(0..1, "S"); |
53 | 0 | } else { |
54 | | // "stack backtrace:" prefix was removed in |
55 | | // https://github.com/rust-lang/backtrace-rs/pull/286 |
56 | 0 | writeln!(f, "Stack backtrace:")?; |
57 | | } |
58 | 0 | backtrace.truncate(backtrace.trim_end().len()); |
59 | 0 | write!(f, "{}", backtrace)?; |
60 | 0 | } |
61 | | } |
62 | | |
63 | 0 | Ok(()) |
64 | 0 | } |
65 | | } |
66 | | |
67 | | struct Indented<'a, D> { |
68 | | inner: &'a mut D, |
69 | | number: Option<usize>, |
70 | | started: bool, |
71 | | } |
72 | | |
73 | | impl<T> Write for Indented<'_, T> |
74 | | where |
75 | | T: Write, |
76 | | { |
77 | 0 | fn write_str(&mut self, s: &str) -> fmt::Result { |
78 | 0 | for (i, line) in s.split('\n').enumerate() { |
79 | 0 | if !self.started { |
80 | 0 | self.started = true; |
81 | 0 | match self.number { |
82 | 0 | Some(number) => write!(self.inner, "{: >5}: ", number)?, |
83 | 0 | None => self.inner.write_str(" ")?, |
84 | | } |
85 | 0 | } else if i > 0 { |
86 | 0 | self.inner.write_char('\n')?; |
87 | 0 | if self.number.is_some() { |
88 | 0 | self.inner.write_str(" ")?; |
89 | | } else { |
90 | 0 | self.inner.write_str(" ")?; |
91 | | } |
92 | 0 | } |
93 | | |
94 | 0 | self.inner.write_str(line)?; |
95 | | } |
96 | | |
97 | 0 | Ok(()) |
98 | 0 | } |
99 | | } |
100 | | |
101 | | #[cfg(test)] |
102 | | mod tests { |
103 | | use super::*; |
104 | | |
105 | | #[test] |
106 | | fn one_digit() { |
107 | | let input = "verify\nthis"; |
108 | | let expected = " 2: verify\n this"; |
109 | | let mut output = String::new(); |
110 | | |
111 | | Indented { |
112 | | inner: &mut output, |
113 | | number: Some(2), |
114 | | started: false, |
115 | | } |
116 | | .write_str(input) |
117 | | .unwrap(); |
118 | | |
119 | | assert_eq!(expected, output); |
120 | | } |
121 | | |
122 | | #[test] |
123 | | fn two_digits() { |
124 | | let input = "verify\nthis"; |
125 | | let expected = " 12: verify\n this"; |
126 | | let mut output = String::new(); |
127 | | |
128 | | Indented { |
129 | | inner: &mut output, |
130 | | number: Some(12), |
131 | | started: false, |
132 | | } |
133 | | .write_str(input) |
134 | | .unwrap(); |
135 | | |
136 | | assert_eq!(expected, output); |
137 | | } |
138 | | |
139 | | #[test] |
140 | | fn no_digits() { |
141 | | let input = "verify\nthis"; |
142 | | let expected = " verify\n this"; |
143 | | let mut output = String::new(); |
144 | | |
145 | | Indented { |
146 | | inner: &mut output, |
147 | | number: None, |
148 | | started: false, |
149 | | } |
150 | | .write_str(input) |
151 | | .unwrap(); |
152 | | |
153 | | assert_eq!(expected, output); |
154 | | } |
155 | | } |