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