macro_tools/
tokens.rs

1//!
2//! Attributes analyzys and manipulation.
3//!
4
5/// Define a private namespace for all its items.
6mod private 
7{
8
9  use crate :: *;
10  use core ::fmt;
11
12  /// `Tokens` is a wrapper around `proc_macro2 ::TokenStream`.
13  /// It is designed to facilitate the parsing and manipulation of token streams
14  /// within procedural macros.
15  ///
16  /// # Examples
17  ///
18  /// Creating a new `Tokens` instance from a token stream :
19  ///
20  /// ```rust
21  /// use macro_tools ::exposed :: *;
22  ///
23  /// let ts: proc_macro2 ::TokenStream = qt! { let x = 10; };
24  /// let tokens = tokens ::Tokens ::new( ts );
25  /// ```
26  #[ derive( Default ) ]
27  pub struct Tokens
28  {
29  /// `proc_macro2 ::TokenStream`
30  pub inner: proc_macro2 ::TokenStream,
31 }
32
33  impl Tokens 
34  {
35  /// Constructor from `proc_macro2 ::TokenStream`.
36  #[ must_use ]
37  pub fn new(inner: proc_macro2 ::TokenStream) -> Self
38  {
39   Tokens { inner }
40 }
41 }
42
43  impl syn ::parse ::Parse for Tokens 
44  {
45  fn parse(input: syn ::parse ::ParseStream< '_ >) -> syn ::Result< Self > 
46  {
47   let inner: proc_macro2 ::TokenStream = input.parse()?;
48   Ok(Tokens ::new(inner))
49 }
50 }
51
52  impl quote ::ToTokens for Tokens 
53  {
54  fn to_tokens(&self, tokens: &mut proc_macro2 ::TokenStream) 
55  {
56   self.inner.to_tokens(tokens);
57 }
58 }
59
60  impl fmt ::Debug for Tokens 
61  {
62  fn fmt(&self, f: &mut fmt ::Formatter< '_ >) -> fmt ::Result 
63  {
64   write!(f, "{}", self.inner)
65 }
66 }
67
68  impl core ::fmt ::Display for Tokens 
69  {
70  fn fmt(&self, f: &mut core ::fmt ::Formatter< '_ >) -> core ::fmt ::Result 
71  {
72   write!(f, "{}", self.inner)
73 }
74 }
75}
76
77#[ doc( inline ) ]
78#[ allow( unused_imports ) ]
79pub use own :: *;
80
81/// Own namespace of the module.
82#[ allow( unused_imports ) ]
83pub mod own 
84{
85
86  use super :: *;
87  #[ doc( inline ) ]
88  pub use orphan :: *;
89}
90
91/// Orphan namespace of the module.
92#[ allow( unused_imports ) ]
93pub mod orphan 
94{
95
96  use super :: *;
97  #[ doc( inline ) ]
98  pub use exposed :: *;
99}
100
101/// Exposed namespace of the module.
102#[ allow( unused_imports ) ]
103pub mod exposed 
104{
105
106  use super :: *;
107
108  pub use super ::super ::tokens;
109  // pub use super ::own as tokens;
110
111  #[ doc( inline ) ]
112  pub use prelude :: *;
113  #[ doc( inline ) ]
114  pub use private :: { Tokens };
115}
116
117/// Prelude to use essentials: `use my_module ::prelude :: *`.
118#[ allow( unused_imports ) ]
119pub mod prelude 
120{
121  use super :: *;
122}