pretty_name/
lib.rs

1#![doc = include_str!("../README.md")]
2
3mod type_name;
4pub use type_name::type_name;
5pub use type_name::type_name_of_val;
6
7/// Get the name of the given identifier.
8/// 
9/// This macro can be used to get the name of local variables, contants and functions.
10/// 
11/// This macro checks that the identifier is valid in the current scope. If the identifier
12/// is renamed via refactoring tools, the macro call will be updated accordingly.
13/// 
14/// # Examples
15/// ```rust
16/// fn my_function() {}
17/// let my_variable = 42;
18/// assert_eq!(pretty_name::of!(my_function), "my_function");
19/// assert_eq!(pretty_name::of!(my_variable), "my_variable");
20/// ```
21#[macro_export]
22macro_rules! of {
23    ($ident:ident $(::<$($arg:ty),*>)?) => {{
24        let _ = &$ident;
25        stringify!($ident)
26    }};
27}
28
29/// Get the name of the given struct field.
30/// 
31/// This macro resolves `Self` to the appropriate type when used inside an `impl` block.
32/// 
33/// This macro checks that the field exists on the given type. If either the type or field
34/// is renamed via refactoring tools, the macro call will be updated accordingly.
35/// 
36/// # Examples
37/// ```rust
38/// struct MyStruct {
39///     my_field: i32,
40/// }
41/// assert_eq!(pretty_name::of_field!(MyStruct::my_field), "MyStruct::my_field");
42/// ```
43#[macro_export]
44macro_rules! of_field {
45    ($ty:ident $(::<$($arg:ty),*>)? :: $field:ident) => {{
46        let _ = |obj: $ty| {
47            let _ = &obj.$field;
48        };
49        format!("{}::{}", $crate::type_name::<$ty $(::<$($arg),*>)?>(), stringify!($field))
50    }};
51}
52
53/// Get the name of the given method.
54/// 
55/// This macro resolves `Self` to the appropriate type when used inside an `impl` block.
56/// 
57/// This macro checks that the method exists on the given type. If either the type or method
58/// is renamed via refactoring tools, the macro call will be updated accordingly.
59/// 
60/// # Examples
61/// ```rust
62/// struct MyStruct;
63/// impl MyStruct {
64///     fn my_method(&self) {}
65/// }
66/// assert_eq!(pretty_name::of_method!(MyStruct::my_method), "MyStruct::my_method");
67/// ```
68#[macro_export]
69macro_rules! of_method {
70    ($ty:ident $(::<$($arg:ty),*>)? :: $method:ident) => {{
71        let _ = &$ty::$method;
72        format!("{}::{}", $crate::type_name::<$ty $(::<$($arg),*>)?>(), stringify!($method))
73    }};
74}
75
76/// Get the name of the given enum variant.
77/// 
78/// This macro resolves `Self` to the appropriate type when used inside an `impl` block.
79/// 
80/// This macros supports both unit variants, tuple variants and struct variants. See
81/// examples for syntax for each variant type.
82/// 
83/// This macro checks that the variant exists on the given enum type. If either the type or
84/// variant is renamed via refactoring tools, the macro call will be updated accordingly.
85/// 
86/// # Examples
87/// ```rust
88/// enum MyEnum {
89///     UnitVariant,
90///     TupleVariant(i32, String),
91///     StructVariant { field: i32 },
92/// }
93/// assert_eq!(pretty_name::of_variant!(MyEnum::UnitVariant), "MyEnum::UnitVariant");
94/// assert_eq!(pretty_name::of_variant!(MyEnum::TupleVariant(..)), "MyEnum::TupleVariant");
95/// assert_eq!(pretty_name::of_variant!(MyEnum::StructVariant {..}), "MyEnum::StructVariant");
96/// ```
97#[macro_export]
98macro_rules! of_variant {
99    ($ty:ident $(::<$($arg:ty),*>)? :: $variant:ident) => {{
100        let _ = |obj| match obj { $ty::$variant => {}, _ => {} };
101        format!("{}::{}", $crate::type_name::<$ty $(::<$($arg),*>)?>(), stringify!($variant))
102    }};
103    ($ty:ident $(::<$($arg:ty),*>)? :: $variant:ident (..)) => {{
104        let _ = |obj| match obj { $ty::$variant(..) => {}, _ => {} };
105        format!("{}::{}", $crate::type_name::<$ty $(::<$($arg),*>)?>(), stringify!($variant))
106    }};
107    ($ty:ident $(::<$($arg:ty),*>)? :: $variant:ident {..}) => {{
108        let _ = |obj| match obj { $ty::$variant { .. } => {}, _ => {} };
109        format!("{}::{}", $crate::type_name::<$ty $(::<$($arg),*>)?>(), stringify!($variant))
110    }};
111}