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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//! # Description
//!
//! Macro to simplify using Types in the [`quote!`] macro.
//!
//! # Usage
//!
//! The [`quote_use!`] macro can be used just like [`quote!`], but with the
//! added functionality of adding use statements at the top:
//!
//! ```
//! # use quote_use::quote_use;
//! quote_use! {
//!     ## use std::fs::read;
//!
//!     read("src/main.rs")
//! }
//! # ;
//! ```
//!
//! This will expand to the equivalent statement using [`quote!`]:
//!
//! ```
//! # use quote::quote;
//! quote! {
//!     ::std::fs::read::read("src/main.rs")
//! }
//! # ;
//! ```
//!
//! ## Prelude
//!
//! This also allows using contents of the rust prelude directly:
//!
//! ```
//! # use quote_use::quote_use;
//! quote_use! {
//!     Some("src/main.rs")
//! }
//! # ;
//! ```
//! ### Overriding prelude
//! When you want to use your own type instead of the prelude type this can be
//! achieved by simply importing it like so
//!
//! ```
//! # use quote_use::quote_use;
//! quote_use! {
//!     ## use anyhow::Result;
//!
//!     Result
//! }
//! # ;
//! ```
//! ### Different preludes
//!
//! By default [`quote_use!`] uses the [core prelude](core::prelude), [std
//! prelude](std::prelude) and [2021 edition prelude](std::prelude::rust_2021).
//! Preferring `core` where available.
//!
//! All preludes can be disabled by adding `# use no_prelude;` at the top of the
//! macro input. The `std` prelude can be disabled with `# use no_std_prelude;`.
//!
//! ## Other quote macros
//!
//! There are also variants for other quote macros from [syn] and [mod@quote]:
//!
//! - [`quote_use!`] and [`quote_spanned_use!`] as replacement for [`quote!`]
//!   and
//! [`quote_spanned!`](quote::quote_spanned!) respectively
//! - [`parse_quote_use!`] and [`parse_quote_spanned_use!`] for
//!   [`parse_quote!`](syn::parse_quote!)
//! and [`parse_quote_spanned!`](syn::parse_quote_spanned!)
#[cfg(doc)]
use quote::quote;
// Reexport
pub use quote::{format_ident, IdentFragment, ToTokens, TokenStreamExt};

#[doc(hidden)]
pub mod __private {
    pub use quote;
    pub use quote_use_macros::quote_use_impl;
    #[cfg(feature = "syn")]
    pub use syn;
}

#[macro_export]
macro_rules! quote_use {
    ($($tokens:tt)*) => {
        $crate::__private::quote_use_impl!{($crate::__private::quote::quote) () ($($tokens)*)}
    };
}

#[macro_export]
macro_rules! quote_spanned_use {
    ($span:expr => $($tokens:tt)*) => {
        $crate::__private::quote_use_impl!{($crate::__private::quote::quote_spanned) ($span =>) ($($tokens)*)}
    };
}

#[cfg(feature = "syn")]
#[macro_export]
macro_rules! parse_quote_use {
    ($($tokens:tt)*) => {
        $crate::__private::quote_use_impl!{($crate::__private::syn::parse_quote) () ($($tokens)*)}
    };
}

#[cfg(feature = "syn")]
#[macro_export]
macro_rules! parse_quote_spanned_use {
    ($span:expr => $($tokens:tt)*) => {
        $crate::__private::quote_use_impl!{($crate::__private::syn::parse_quote_spanned) ($span =>) ($($tokens)*)}
    };
}

#[macro_export]
macro_rules! quote_use_no_prelude {
    ($($tokens:tt)*) => {
        $crate::__private::quote_use_impl!{($crate::__private::quote::quote) () (#use no_prelude; $($tokens)*)}
    };
}

#[macro_export]
macro_rules! quote_spanned_use_no_prelude {
    ($span:expr => $($tokens:tt)*) => {
        $crate::__private::quote_use_impl!{($crate::__private::quote::quote_spanned) ($span =>) (#use no_prelude; $($tokens)*)}
    };
}

#[cfg(feature = "syn")]
#[macro_export]
macro_rules! parse_quote_use_no_prelude {
    ($($tokens:tt)*) => {
        $crate::__private::quote_use_impl!{($crate::__private::syn::parse_quote) () (#use no_prelude; $($tokens)*)}
    };
}

#[cfg(feature = "syn")]
#[macro_export]
macro_rules! parse_quote_spanned_use_no_prelude {
    ($span:expr => $($tokens:tt)*) => {
        $crate::__private::quote_use_impl!{($crate::__private::syn::parse_quote_spanned) ($span =>) (#use no_prelude; $($tokens)*)}
    };
}