quote_use/lib.rs
1//! # Description
2//!
3//! Macro to simplify using Types in the [`quote!`] macro.
4//!
5//! # Usage
6//!
7//! The [`quote_use!`] macro can be used just like [`quote!`], but with the
8//! added functionality of adding use statements at the top:
9//!
10//! ```
11//! # use quote_use::quote_use;
12//! quote_use! {
13//! ## use std::fs::read;
14//!
15//! read("src/main.rs")
16//! }
17//! # ;
18//! ```
19//!
20//! This will expand to the equivalent statement using [`quote!`]:
21//!
22//! ```
23//! # use quote::quote;
24//! quote! {
25//! ::std::fs::read::read("src/main.rs")
26//! }
27//! # ;
28//! ```
29//!
30//! ## Prelude
31//!
32//! This also allows using contents of the rust prelude directly:
33//!
34//! ```
35//! # use quote_use::quote_use;
36//! quote_use! {
37//! Some("src/main.rs")
38//! }
39//! # ;
40//! ```
41//! ### Overriding prelude
42//! When you want to use your own type instead of the prelude type this can be
43//! achieved by simply importing it like so
44//!
45//! ```
46//! # use quote_use::quote_use;
47//! quote_use! {
48//! ## use anyhow::Result;
49//!
50//! Result
51//! }
52//! # ;
53//! ```
54//! ### Different preludes
55//!
56//! By default [`quote_use!`] uses the [core prelude](core::prelude), [std
57//! prelude](std::prelude) and [2021 edition prelude](std::prelude::rust_2021).
58//! Preferring `core` where available.
59//!
60//! All preludes can be disabled by adding `# use no_prelude;` at the top of the
61//! macro input. The `std` prelude can be disabled with `# use no_std_prelude;`.
62//!
63//! ## Other quote macros
64//!
65//! There are also variants for other quote macros from [syn] and [mod@quote]:
66//!
67//! - [`quote_use!`] and [`quote_spanned_use!`] as replacement for [`quote!`]
68//! and
69//! [`quote_spanned!`](quote::quote_spanned!) respectively
70//! - [`parse_quote_use!`] and [`parse_quote_spanned_use!`] for
71//! [`parse_quote!`](syn::parse_quote!)
72//! and [`parse_quote_spanned!`](syn::parse_quote_spanned!)
73#[cfg(doc)]
74use quote::quote;
75// Reexport
76pub use quote::{format_ident, IdentFragment, ToTokens, TokenStreamExt};
77
78#[doc(hidden)]
79pub mod __private {
80 pub use quote;
81 pub use quote_use_macros::quote_use_impl;
82 #[cfg(feature = "syn")]
83 pub use syn;
84}
85
86#[macro_export]
87macro_rules! quote_use {
88 ($($tokens:tt)*) => {
89 $crate::__private::quote_use_impl!{($crate::__private::quote::quote) () ($($tokens)*)}
90 };
91}
92
93#[macro_export]
94macro_rules! quote_spanned_use {
95 ($span:expr => $($tokens:tt)*) => {
96 $crate::__private::quote_use_impl!{($crate::__private::quote::quote_spanned) ($span =>) ($($tokens)*)}
97 };
98}
99
100#[cfg(feature = "syn")]
101#[macro_export]
102macro_rules! parse_quote_use {
103 ($($tokens:tt)*) => {
104 $crate::__private::quote_use_impl!{($crate::__private::syn::parse_quote) () ($($tokens)*)}
105 };
106}
107
108#[cfg(feature = "syn")]
109#[macro_export]
110macro_rules! parse_quote_spanned_use {
111 ($span:expr => $($tokens:tt)*) => {
112 $crate::__private::quote_use_impl!{($crate::__private::syn::parse_quote_spanned) ($span =>) ($($tokens)*)}
113 };
114}
115
116#[macro_export]
117macro_rules! quote_use_no_prelude {
118 ($($tokens:tt)*) => {
119 $crate::__private::quote_use_impl!{($crate::__private::quote::quote) () (#use no_prelude; $($tokens)*)}
120 };
121}
122
123#[macro_export]
124macro_rules! quote_spanned_use_no_prelude {
125 ($span:expr => $($tokens:tt)*) => {
126 $crate::__private::quote_use_impl!{($crate::__private::quote::quote_spanned) ($span =>) (#use no_prelude; $($tokens)*)}
127 };
128}
129
130#[cfg(feature = "syn")]
131#[macro_export]
132macro_rules! parse_quote_use_no_prelude {
133 ($($tokens:tt)*) => {
134 $crate::__private::quote_use_impl!{($crate::__private::syn::parse_quote) () (#use no_prelude; $($tokens)*)}
135 };
136}
137
138#[cfg(feature = "syn")]
139#[macro_export]
140macro_rules! parse_quote_spanned_use_no_prelude {
141 ($span:expr => $($tokens:tt)*) => {
142 $crate::__private::quote_use_impl!{($crate::__private::syn::parse_quote_spanned) ($span =>) (#use no_prelude; $($tokens)*)}
143 };
144}