source_location/
lib.rs

1//!
2
3use std::fmt;
4use serde::{Deserialize, Serialize};
5
6#[macro_export]
7macro_rules! location {
8    () => {
9        $crate::Location {
10            file:   file!().to_string(),
11            line:   line!(),
12            column: column!(),
13        }
14    }
15}
16
17#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
18#[derive(Deserialize, Serialize)]
19pub struct Location {
20    pub file: String,
21    pub line: u32,
22    pub column: u32,
23}
24
25impl fmt::Display for Location {
26    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27        write!(f, "{}:{}:{}", self.file, self.line, self.column)
28    }
29}
30
31
32
33#[cfg(test)]
34mod tests {
35    use super::*;
36
37    #[test]
38    fn default_location() {
39        let Location { file, line, column } = Location::default();
40        assert_eq!(  file, "");
41        assert_eq!(  line,  0);
42        assert_eq!(column,  0);
43    }
44
45    #[test]
46    fn location_macro() {
47        let Location { file, line, column } = location!();
48        assert_eq!(  file, "src/lib.rs");
49        assert_eq!(  line,  47);
50        assert_eq!(column,  47);
51    }
52}