1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//! # code-path
//!
//! A code path macro
//!
//! ## Usage
//! ```rust
//! # use code_path::code_path;
//!
//! fn foo() {
//!     fn bar() {
//!         assert_eq!(
//!             code_path!(),
//!             "rust_out::main::_doctest_main_src_lib_rs_6_0::foo::bar, src/lib.rs:10:13".into(),
//!         );
//!     }
//!     bar()
//! }
//! foo()
//! ```

#![warn(clippy::all, missing_docs, nonstandard_style, future_incompatible)]
use std::fmt;
use std::ops::{Deref, DerefMut};

/// Represents path in the code
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CodePath(String);

impl fmt::Display for CodePath {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<&str> for CodePath {
    fn from(s: &str) -> Self {
        Self(s.into())
    }
}

impl From<String> for CodePath {
    fn from(s: String) -> Self {
        Self(s)
    }
}

impl From<CodePath> for String {
    fn from(val: CodePath) -> Self {
        val.0
    }
}

impl Deref for CodePath {
    type Target = String;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for CodePath {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

/// Returns the current code scope with location, e.g.
/// `code_path::tests::scope_path::foo::bar, src/lib.rs:80:17`
#[macro_export]
macro_rules! code_path {
    () => {
        $crate::CodePath::from(format!(
            "{}, {}",
            $crate::code_scope!(),
            $crate::code_loc!()
        ))
    };
}

/// Returns the current scope path, e.g. `my_crate::my_module::my_function`)
#[macro_export]
macro_rules! code_scope {
    () => {{
        fn f() {}
        fn type_name_of<T>(_: T) -> &'static str {
            ::std::any::type_name::<T>()
        }
        let mut name = type_name_of(f);
        name = &name[..name.len() - 3];
        while name.ends_with("::{{closure}}") {
            name = &name[..name.len() - 13];
        }
        name
    }};
}

/// Returns the code location: `file_name:line:column`
#[macro_export]
macro_rules! code_loc {
    () => {
        concat!(file!(), ":", line!(), ":", column!())
    };
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn nesting() {
        fn foo() -> &'static str {
            fn bar() -> &'static str {
                code_scope!()
            }
            bar()
        }

        assert_eq!(foo(), "code_path::tests::nesting::foo::bar");
    }

    #[test]
    fn ending_cloures() {
        fn foo() -> &'static str {
            #[allow(clippy::redundant_closure_call)]
            (|| (|| code_scope!())())()
        }
        assert_eq!(foo(), "code_path::tests::ending_cloures::foo");
    }
}