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  #[ allow( clippy::wildcard_imports ) ]
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
78#[ doc( inline ) ]
79#[ allow( unused_imports ) ]
80pub use own::*;
81
82/// Own namespace of the module.
83#[ allow( unused_imports ) ]
84pub mod own
85{
86  #[ allow( clippy::wildcard_imports ) ]
87  use super::*;
88  #[ doc( inline ) ]
89  pub use orphan::*;
90}
91
92/// Orphan namespace of the module.
93#[ allow( unused_imports ) ]
94pub mod orphan
95{
96  #[ allow( clippy::wildcard_imports ) ]
97  use super::*;
98  #[ doc( inline ) ]
99  pub use exposed::*;
100}
101
102/// Exposed namespace of the module.
103#[ allow( unused_imports ) ]
104pub mod exposed
105{
106  #[ allow( clippy::wildcard_imports ) ]
107  use super::*;
108
109  pub use super::super::tokens;
110  // pub use super::own as tokens;
111
112  #[ doc( inline ) ]
113  pub use prelude::*;
114  #[ doc( inline ) ]
115  pub use private::
116  {
117    Tokens,
118  };
119}
120
121/// Prelude to use essentials: `use my_module::prelude::*`.
122#[ allow( unused_imports ) ]
123pub mod prelude
124{
125  use super::*;
126}
127