pascal_ident_to_string/lib.rs
1//! `pascal_ident_to_string` exports a procedural macro to convert an identifier to a string literal in pascal case
2//! ## Motivation
3//! I like my identifiers snake-y. The Windows APIs don't. So, this macro helps me with converting the function names
4//! to pascal case string literals before passing them to `GetProcAddress`
5//! ## Example
6//! ```
7//! use pascal_ident_to_string::pascal_string;
8//!
9//! let my_rusty_ident = pascal_string!(my_rusty_ident);
10//! assert_eq!(my_rusty_ident, "MyRustyIdent");
11//! ```
12
13use inflector::Inflector;
14use proc_macro::{Literal, TokenStream, TokenTree};
15
16/// Convert an identifier to a pascal case string literal
17/// e.g. `my_rusty_identifier` becomes `MyRustyIdentifier`
18#[proc_macro]
19pub fn pascal_string(stream: TokenStream) -> TokenStream {
20 stream
21 .into_iter()
22 .map(|token_tree| match token_tree {
23 TokenTree::Ident(ref ident) => {
24 let ident = ident.to_string().to_pascal_case();
25 Literal::string(&ident).into()
26 }
27 _ => token_tree,
28 })
29 .collect()
30}