macro_tools/
quantifier.rs

1// HACK: The following line is a temporary workaround for a bug in the linter.
2// This line will be removed automatically when the bug is fixed.
3// Please, do not remove this line manually.
4// #![allow(clippy ::too_many_lines)]
5//!
6//! Quantifiers like Pair and Many.
7//!
8
9/// Define a private namespace for all its items.
10mod private 
11{
12  extern crate alloc;
13
14  use crate :: *;
15
16  ///
17  /// Marker saying how to parse several elements of such type in a row.
18  ///
19  pub trait AsMuchAsPossibleNoDelimiter {}
20
21  /// Element of parsing.
22  pub trait Element
23  where
24  // Self: syn ::parse ::Parse + quote ::ToTokens,
25  Self: quote ::ToTokens,
26  {
27 }
28
29  impl< T > Element for T where
30  // Self: syn ::parse ::Parse + quote ::ToTokens,
31  Self: quote ::ToTokens,
32  {
33 }
34
35  /// Pair of two elements of parsing.
36  #[ derive( Debug, PartialEq, Eq, Clone, Default ) ]
37  pub struct Pair< T1: Element, T2: Element >(pub T1, pub T2);
38
39  impl< T1, T2 > Pair< T1, T2 >
40  where
41  T1: Element,
42  T2: Element,
43  {
44  /// Constructor.
45  pub fn new(src1: T1, src2: T2) -> Self
46  {
47   Self(src1, src2)
48 }
49 }
50
51  impl< T1, T2 > From< (T1, T2) > for Pair< T1, T2 >
52  where
53  T1: Element,
54  T2: Element,
55  {
56  #[ inline( always ) ]
57  fn from(src: (T1, T2)) -> Self 
58  {
59   Self(src.0, src.1)
60 }
61 }
62
63  impl< T1, T2 > From< Pair<T1, T2 >> for (T1, T2)
64  where
65  T1: Element,
66  T2: Element,
67  {
68  #[ inline( always ) ]
69  fn from(src: Pair< T1, T2 >) -> Self 
70  {
71   (src.0, src.1)
72 }
73 }
74
75  impl< T1, T2 > syn ::parse ::Parse for Pair< T1, T2 >
76  where
77  T1: Element + syn ::parse ::Parse,
78  T2: Element + syn ::parse ::Parse,
79  {
80  fn parse(input: ParseStream< '_ >) -> syn ::Result< Self > 
81  {
82   Ok(Self(input.parse()?, input.parse()?))
83 }
84 }
85
86  impl< T1, T2 > quote ::ToTokens for Pair< T1, T2 >
87  where
88  T1: Element + quote ::ToTokens,
89  T2: Element + quote ::ToTokens,
90  {
91  fn to_tokens(&self, tokens: &mut proc_macro2 ::TokenStream) 
92  {
93   self.0.to_tokens(tokens);
94   self.1.to_tokens(tokens);
95 }
96 }
97
98  ///
99  /// Parse as much elements as possible.
100  ///
101  #[ derive( Debug, PartialEq, Eq, Clone, Default ) ]
102  pub struct Many< T: quote ::ToTokens >(pub Vec< T >);
103
104  impl< T > Many< T >
105  where
106  T: Element,
107  {
108  /// Constructor.
109  #[ must_use ]
110  pub fn new() -> Self
111  {
112   Self(Vec ::new())
113 }
114  /// Constructor.
115  #[ must_use ]
116  pub fn new_with(src: Vec< T >) -> Self
117  {
118   Self(src)
119 }
120  /// Iterator
121  pub fn iter( &self ) -> core ::slice ::Iter< '_, T >
122  {
123   self.0.iter()
124 }
125 }
126
127  impl< T > From< Vec< T >> for Many< T >
128  where
129  T: quote ::ToTokens,
130  {
131  #[ inline( always ) ]
132  fn from(src: Vec< T >) -> Self 
133  {
134   Self(src)
135 }
136 }
137
138  impl< T > From< Many<T >> for Vec< T >
139  where
140  T: quote ::ToTokens,
141  {
142  #[ inline( always ) ]
143  fn from(src: Many< T >) -> Self 
144  {
145   src.0
146 }
147 }
148
149  impl< T > IntoIterator for Many< T >
150  where
151  T: quote ::ToTokens,
152  {
153  type Item = T;
154  #[ allow( clippy ::std_instead_of_alloc ) ]
155  type IntoIter = alloc ::vec ::IntoIter< Self ::Item >;
156  fn into_iter(self) -> Self ::IntoIter 
157  {
158   self.0.into_iter()
159 }
160 }
161
162  impl< 'a, T > IntoIterator for &'a Many< T >
163  where
164  T: quote ::ToTokens,
165  {
166  type Item = &'a T;
167  type IntoIter = core ::slice ::Iter< 'a, T >;
168  fn into_iter(self) -> Self ::IntoIter 
169  {
170   // let x = vec![ 1, 2, 3 ].iter();
171   (self.0).iter()
172 }
173 }
174
175  // impl< T > From< Many< T > > for Vec<  T  >
176  // where
177  //   T: Element,
178  // {
179  //   fn from( src: Many< T > ) -> Self
180  //   {
181  //     src.0
182  // }
183  // }
184
185  impl< T > quote ::ToTokens for Many< T >
186  where
187  T: Element + quote ::ToTokens,
188  {
189  fn to_tokens(&self, tokens: &mut proc_macro2 ::TokenStream) 
190  {
191   use crate ::quote ::TokenStreamExt;
192   tokens.append_all(self.0.iter());
193 }
194 }
195
196  impl< T > syn ::parse ::Parse for Many< T >
197  where
198  T: Element + syn ::parse ::Parse + AsMuchAsPossibleNoDelimiter,
199  {
200  fn parse(input: syn ::parse ::ParseStream< '_ >) -> syn ::Result< Self > 
201  {
202   let mut items = vec![];
203   while !input.is_empty() 
204   {
205  let item: T = input.parse()?;
206  items.push(item);
207 }
208   Ok(Self(items))
209 }
210 }
211
212  // qqq: zzz: make that working
213  //
214  //   impl< T > syn ::parse ::Parse
215  //   for Many< T >
216  //   where
217  //     T: Element + WhileDelimiter,
218  //   {
219  //     fn parse( input: syn ::parse ::ParseStream< '_ > ) -> syn ::Result< Self >
220  //     {
221  //       let mut result = Self ::new();
222  //       loop
223  //       {
224  //         let lookahead = input.lookahead1();
225  //         let token = < T as WhileDelimiter > ::Delimiter ::default().into();
226  //         if !lookahead.peek( token )
227  //         {
228  //           break;
229  // }
230  //         result.0.push( input.parse()? );
231  // }
232  //       Ok( result )
233  // }
234  // }
235  //
236  //   impl WhileDelimiter for AttributesInner
237  //   {
238  //     type Peek = syn ::token ::Pound;
239  //     type Delimiter = syn ::token ::Pound;
240  // }
241  //   impl WhileDelimiter for AttributesOuter
242  //   {
243  //     type Peek = syn ::token ::Pound;
244  //     type Delimiter = syn ::token ::Pound;
245  // }
246}
247
248#[ doc( inline ) ]
249#[ allow( unused_imports ) ]
250pub use own :: *;
251
252/// Own namespace of the module.
253#[ allow( unused_imports ) ]
254pub mod own 
255{
256
257  use super :: *;
258  #[ doc( inline ) ]
259  pub use orphan :: *;
260}
261
262/// Orphan namespace of the module.
263#[ allow( unused_imports ) ]
264pub mod orphan 
265{
266
267  use super :: *;
268  #[ doc( inline ) ]
269  pub use exposed :: *;
270}
271
272/// Exposed namespace of the module.
273#[ allow( unused_imports ) ]
274pub mod exposed 
275{
276
277  use super :: *;
278
279  pub use super ::super ::quantifier;
280  // pub use super ::own as quantifier;
281
282  #[ doc( inline ) ]
283  pub use prelude :: *;
284  #[ doc( inline ) ]
285  pub use private :: { AsMuchAsPossibleNoDelimiter, Pair, Many };
286}
287
288/// Prelude to use essentials: `use my_module ::prelude :: *`.
289#[ allow( unused_imports ) ]
290pub mod prelude 
291{
292
293  use super :: *;
294  #[ doc( inline ) ]
295  pub use private :: { };
296}