nu_protocol/errors/
short_handler.rs

1use std::fmt;
2
3use miette::{Diagnostic, ReportHandler};
4
5/// A [`ReportHandler`] that renders errors as plain text without graphics.
6/// Designed for concise output that typically fits on a single line.
7#[derive(Debug, Clone)]
8pub struct ShortReportHandler {}
9
10impl ShortReportHandler {
11    pub const fn new() -> Self {
12        Self {}
13    }
14}
15
16impl Default for ShortReportHandler {
17    fn default() -> Self {
18        Self::new()
19    }
20}
21
22impl ShortReportHandler {
23    /// Render a [`Diagnostic`]. This function is meant to be called
24    /// by the toplevel [`ReportHandler`].
25    fn render_report(
26        &self,
27        f: &mut fmt::Formatter<'_>,
28        diagnostic: &dyn Diagnostic,
29    ) -> fmt::Result {
30        write!(f, "{}: ", diagnostic)?;
31
32        if let Some(labels) = diagnostic.labels() {
33            let mut labels = labels
34                .into_iter()
35                .filter_map(|span| span.label().map(String::from))
36                .peekable();
37
38            while let Some(label) = labels.next() {
39                let end_char = if labels.peek().is_some() { ", " } else { " " };
40                write!(f, "{}{}", label, end_char)?;
41            }
42        }
43
44        if let Some(help) = diagnostic.help() {
45            write!(f, "({})", help)?;
46        }
47
48        Ok(())
49    }
50}
51
52impl ReportHandler for ShortReportHandler {
53    fn debug(&self, diagnostic: &dyn Diagnostic, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        self.render_report(f, diagnostic)
55    }
56}