use std::fmt::{self, Display, Formatter};
#[cfg(feature = "location")]
use oi::Location;
#[cfg(feature = "location")]
use failure::Fail;
#[macro_export]
macro_rules! here {
() => {
$crate::Position::new(module_path!(), file!(), line!(), column!())
};
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Position {
module_path: &'static str,
file: &'static str,
line: u32,
column: u32,
}
impl Position {
pub fn new(module_path: &'static str, file: &'static str, line: u32, column: u32) -> Position {
Position {
module_path,
file,
line,
column,
}
}
pub fn file(self) -> &'static str {
self.file
}
pub fn module_path(self) -> &'static str {
self.module_path
}
pub fn line(self) -> u32 {
self.line
}
pub fn column(self) -> u32 {
self.column
}
}
impl Display for Position {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}:{}:{}", self.file, self.line, self.column)
}
}
#[cfg(feature = "location")]
impl Location for Position {
fn fmt_error(&self, f: &mut Formatter, error: &dyn Fail) -> fmt::Result {
write!(f, "{}: {}", self, error)
}
}