use core::fmt;
use rootcause_internals::handlers::{AttachmentFormattingStyle, AttachmentHandler};
use crate::hooks::report_creation::AttachmentCollector;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Location {
pub file: &'static str,
pub line: u32,
}
impl Location {
#[track_caller]
pub const fn caller() -> Self {
let location = core::panic::Location::caller();
Location {
file: location.file(),
line: location.line(),
}
}
}
impl fmt::Display for Location {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}", self.file, self.line)
}
}
#[derive(Copy, Clone)]
pub struct LocationHandler;
impl AttachmentHandler<Location> for LocationHandler {
fn display(value: &Location, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
fmt::Display::fmt(value, formatter)
}
fn debug(value: &Location, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
fmt::Display::fmt(value, formatter)
}
fn preferred_formatting_style(
_value: &Location,
_report_formatting_function: rootcause_internals::handlers::FormattingFunction,
) -> AttachmentFormattingStyle {
AttachmentFormattingStyle {
placement: rootcause_internals::handlers::AttachmentFormattingPlacement::Inline,
priority: 20,
function: rootcause_internals::handlers::FormattingFunction::Display,
}
}
}
#[derive(Copy, Clone)]
pub struct LocationHook;
impl AttachmentCollector<Location> for LocationHook {
type Handler = LocationHandler;
fn collect(&self) -> Location {
Location::caller()
}
}