use std::fmt::Display;
use std::panic::Location;
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct SourceLocation {
pub file: String,
pub line: u32,
pub column: u32,
}
impl SourceLocation {
#[track_caller]
#[must_use]
pub fn caller() -> Self {
Self::from_std(Location::caller())
}
pub(crate) fn from_std(location: &Location<'_>) -> Self {
Self {
file: location.file().to_owned(),
line: location.line(),
column: location.column(),
}
}
}
impl Display for SourceLocation {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}:{}", self.file, self.line, self.column)
}
}