use std::path;
#[derive(Clone, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct SourceLocation {
module_path: &'static str,
file: &'static str,
line: u32,
column: u32,
}
impl SourceLocation {
#[doc(hidden)]
#[must_use]
pub fn __new(module_path: &'static str, file: &'static str, line: u32, column: u32) -> Self {
Self {
module_path,
file,
line,
column,
}
}
#[must_use]
pub fn module_path(&self) -> &'static str {
self.module_path
}
#[must_use]
pub fn file(&self) -> &'static str {
self.file
}
#[must_use]
pub fn file_name(&self) -> &'static str {
if let Some(index) = self.file.rfind(path::MAIN_SEPARATOR) {
&self.file[index + 1..]
} else {
self.file
}
}
#[must_use]
pub fn line(&self) -> u32 {
self.line
}
#[must_use]
pub fn column(&self) -> u32 {
self.column
}
#[cfg(feature = "log")]
#[must_use]
pub(crate) fn from_log_crate_record(record: &log::Record) -> Option<Self> {
let (module_path, file, line) = (
record.module_path_static(),
record.file_static(),
record.line(),
);
match (module_path, file, line) {
(None, None, None) => None,
_ => Some(Self {
module_path: module_path.unwrap_or(""),
file: file.unwrap_or(""),
line: line.unwrap_or(0),
column: 0,
}),
}
}
}
#[macro_export]
macro_rules! source_location_current {
() => {
$crate::__private_source_location_current_inner!()
};
}
#[macro_export]
#[doc(hidden)]
#[cfg(feature = "source-location")]
macro_rules! __private_source_location_current_inner {
() => {
Some($crate::SourceLocation::__new(
module_path!(),
file!(),
line!(),
column!(),
))
};
}
#[macro_export]
#[doc(hidden)]
#[cfg(not(feature = "source-location"))]
macro_rules! __private_source_location_current_inner {
() => {
Option::<$crate::SourceLocation>::None
};
}
#[cfg(test)]
mod tests {
#[test]
fn option_type_inference() {
_ = source_location_current!();
}
}