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