error_stack/fmt/
location.rs

1use core::{fmt, panic::Location};
2
3use crate::fmt::color::{Color, ColorMode, DisplayStyle, Style};
4
5pub(super) struct LocationAttachment<'a, 'loc> {
6    location: &'a Location<'loc>,
7    mode: ColorMode,
8}
9
10impl<'a, 'loc> LocationAttachment<'a, 'loc> {
11    #[must_use]
12    pub(super) const fn new(location: &'a Location<'loc>, mode: ColorMode) -> Self {
13        Self { location, mode }
14    }
15}
16
17impl fmt::Display for LocationAttachment<'_, '_> {
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        let location = self.location;
20
21        let mut style = Style::new();
22
23        match self.mode {
24            ColorMode::None => {}
25            ColorMode::Color => style.set_foreground(Color::Black, true),
26            ColorMode::Emphasis => style.set_display(DisplayStyle::new().with_italic(true)),
27        };
28
29        f.write_fmt(format_args!("at {}", style.apply(&location)))?;
30
31        Ok(())
32    }
33}