mod_interface_meta 0.57.0

Protocol of modularity unifying interface of a module and introducing layers.
Documentation
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/// Define a private namespace for all its items.
mod private 
{

  use crate :: *;

  use macro_tools ::exposed :: *;
  use std ::collections ::HashMap;

  // = use

  // x
  // use private ::Type1;
  // use private :: { Type1, Type2 };
  // own use private ::Type1;
  // prelude use private ::Type1;

  // = ?

  // x
  // own own1;
  // orphan orphan1;
  // exposed exposed1;
  // prelude prelude1;
  // prelude { prelude1, prelude2 };

  // = macro module

  // x
  // macromod mod1;
  // macromod mod2;
  // macromod { mod1, mod2 };

  // - narrowing

  // x
  // orphan macromod mod_orphan1;
  // : own -> own
  // : orphan -> orphan
  // : exposed -> orphan
  // : prelude -> orphan

  // - extending

  // x
  // prelude exposed macromod mod_own1;
  // : own -> exposed
  // : orphan -> exposed
  // : exposed -> exposed
  // : prelude -> prelude

  // x
  // prelude own macromod mod_exposed1;
  // : own -> own
  // : orphan -> orphan
  // : exposed -> exposed
  // : prelude -> prelude

  // - selective

  // x
  // exposed exposed macromod mod_exposed1;
  // : own -> exposed
  // : orphan -> exposed
  // : exposed -> exposed
  // : prelude -> exposed

  // x
  // exposed orphan macromod mod_exposed1;
  // : own -> orphan
  // : orphan -> orphan
  // : exposed -> exposed
  // : prelude -> exposed

  // = micro module

  // x
  // mod mod1;
  // mod mod2;
  // mod { mod1, mod2 };

  // +
  // own mod mod_own1;
  // orphan mod mod_orphan1;
  // exposed mod mod_exposed1;
  // prelude mod mod_prelude1;

  // +
  // own mod { mod_own1, mod_own2 };
  // orphan mod { mod_orphan1, mod_orphan2 };
  // exposed mod { mod_exposed1, mod_exposed2 };
  // prelude mod { mod_prelude1, mod_prelude2 };

  // zzz: clause should not expect the first argument

  /// Context for handlign a record. Cotnains clauses map and debug attribute.
  #[ allow( dead_code ) ]
  pub struct RecordContext< 'clauses_map >
  {
  pub has_debug: bool,
  pub clauses_map: &'clauses_map mut HashMap< ClauseKind, Vec< proc_macro2 ::TokenStream >>,
 }

  ///
  /// Handle record "use" with implicit visibility.
  ///
  fn record_reuse_implicit(record: &Record, c: &'_ mut RecordContext< '_ >) -> syn ::Result< () > 
  {
  let attrs1 = &record.attrs;
  let path = record.use_elements.as_ref().unwrap();

  let path =  if let Some(rename) = &path.rename 
  {
   let pure_path = path.pure_without_super_path()?;
   c.clauses_map.get_mut(&ClauseImmediates ::Kind()).unwrap().push(qt! {
  pub use #pure_path as #rename;
 });
   parse_qt! { #rename }
 } else {
   path.clone()
 };

  let adjsuted_path = path.prefixed_with_all();

  c.clauses_map.get_mut(&ClauseKind::Own).unwrap().push(qt! {
   #[ doc( inline ) ]
   #[ allow( unused_imports ) ]
   #attrs1
   pub use #adjsuted_path ::own :: *;
 });

  c.clauses_map.get_mut(&ClauseKind::Orphan).unwrap().push(qt! {
   #[ doc( inline ) ]
   #[ allow( unused_imports ) ]
   #attrs1
   pub use #adjsuted_path ::orphan :: *;
 });

  c.clauses_map.get_mut(&ClauseKind::Exposed).unwrap().push(qt! {
   #[ doc( inline ) ]
   #[ allow( unused_imports ) ]
   #attrs1
   pub use #adjsuted_path ::exposed :: *;
 });

  c.clauses_map.get_mut(&ClauseKind::Prelude).unwrap().push(qt! {
   #[ doc( inline ) ]
   #[ allow( unused_imports ) ]
   #attrs1
   pub use #adjsuted_path ::prelude :: *;
 });

  Ok(())
 }

  ///
  /// Handle record "use" with implicit visibility.
  ///
  /// # Fix(issue-001): Bare use statement propagation
  ///
  /// Root cause: Previous implementation tried to use wildcard re-exports from `layer::*`
  /// which don't work because layers (`__all__::own`, etc.) are private modules.
  ///
  /// Pitfall: Wildcard re-exports only work when the source module is accessible.
  /// Private modules can't be re-exported with wildcards. Must add items directly
  /// to each layer using the adjusted path.
  ///
  fn record_use_implicit(record: &Record, c: &'_ mut RecordContext< '_ >)
  {
  let attrs1 = &record.attrs;
  let path = record.use_elements.as_ref().unwrap();
  let adjusted_path = path.prefixed_with_all();

  // Fix(issue-001): Add to all four layers directly
  // Can't use wildcard re-exports from private __all__ modules
  c.clauses_map.get_mut(&ClauseKind::Own).unwrap().push(qt! {
   #[ doc( inline ) ]
   #[ allow( unused_imports ) ]
   #attrs1
   pub use #adjusted_path;
 });

  c.clauses_map.get_mut(&ClauseKind::Orphan).unwrap().push(qt! {
   #[ doc( inline ) ]
   #[ allow( unused_imports ) ]
   #attrs1
   pub use #adjusted_path;
 });

  c.clauses_map.get_mut(&ClauseKind::Exposed).unwrap().push(qt! {
   #[ doc( inline ) ]
   #[ allow( unused_imports ) ]
   #attrs1
   pub use #adjusted_path;
 });

  c.clauses_map.get_mut(&ClauseKind::Prelude).unwrap().push(qt! {
   #[ doc( inline ) ]
   #[ allow( unused_imports ) ]
   #attrs1
   pub use #adjusted_path;
 });
 }

  ///
  /// Handle record "use" with explicit visibility.
  ///
  fn record_use_explicit(record: &Record, c: &'_ mut RecordContext< '_ >) -> syn ::Result< () > 
  {
  let attrs1 = &record.attrs;
  let path = record.use_elements.as_ref().unwrap();
  let vis = record.vis.clone();

  if !vis.valid_sub_namespace() 
  {
   return Err(syn_err!(
  record,
  "Use either {} visibility: \n  {}",
  VALID_VISIBILITY_LIST_STR,
  qt! { #record },
 ));
 }

  let adjsuted_path = path.prefixed_with_all();
  let vis2 =  if vis.restriction().is_some() 
  {
   qt! { pub( crate ) }
 } else {
   qt! { pub }
 };

  c.clauses_map.get_mut(&vis.kind()).unwrap().push(qt! {
   #[ doc( inline ) ]
   #[ allow( unused_imports ) ]
   #attrs1
   #vis2 use #adjsuted_path;
 });

  Ok(())
 }

  ///
  /// Handle record micro module.
  ///
  fn record_micro_module(
  record: &Record,
  element: &Pair< AttributesOuter, syn ::Path >,
  c: &'_ mut RecordContext< '_ >,
 ) -> syn ::Result< () > {
  let attrs1 = &record.attrs;
  let attrs2 = &element.0;
  let path = &element.1;

  c.clauses_map.get_mut(&ClauseImmediates ::Kind()).unwrap().push(qt! {
   #attrs1
   #attrs2
   pub mod #path;
 });

  if !record.vis.valid_sub_namespace() 
  {
   return Err(syn_err!(
  record,
  "To include a non-standard module use either { } visibility: \n  {}",
  VALID_VISIBILITY_LIST_STR,
  qt! { #record },
 ));
 }

  c.clauses_map.get_mut(&record.vis.kind()).unwrap().push(qt! {
   #[ doc( inline ) ]
   #[ allow( unused_imports ) ]
   #attrs1
   #attrs2
   pub use __all__ :: #path;
   // pub use super :: #path;
   // xxx: remove super?
 });

  Ok(())
 }

  ///
  /// Handle record micro module.
  ///
  #[ allow( dead_code ) ]
  fn record_layer(record: &Record, element: &Pair< AttributesOuter, syn ::Path >, c: &'_ mut RecordContext< '_ >) -> syn ::Result< () > 
  {
  let attrs1 = &record.attrs;
  let attrs2 = &element.0;
  let path = &element.1;

  if record.vis != Visibility ::Inherited 
  {
   return Err(syn_err!(
  record,
  "Layer should not have explicitly defined visibility because all its subnamespaces are used.\n  { }",
  qt! { #record },
 ));
 }

  c.clauses_map.get_mut(&ClauseImmediates ::Kind()).unwrap().push(qt! {
   #attrs1
   #attrs2
   pub mod #path;
 });

  c.clauses_map.get_mut(&ClauseKind::Own).unwrap().push(qt! {
   #[ doc( inline ) ]
   #[ allow( unused_imports ) ]
   #attrs1
   #attrs2
   pub use __all__ :: #path ::orphan :: *;
 });

  // export layer as own field of current layer
  // let prefixed_with_super_maybe = path.prefixed_with_super_maybe();
  c.clauses_map.get_mut(&ClauseKind::Own).unwrap().push(qt! {
   #[ doc( inline ) ]
   #[ allow( unused_imports ) ]
   #attrs1
   pub use super :: #path;
 });

  c.clauses_map.get_mut(&ClauseKind::Exposed).unwrap().push(qt! {
   #[ doc( inline ) ]
   #[ allow( unused_imports ) ]
   #attrs1
   #attrs2
   pub use __all__ :: #path ::exposed :: *;
 });

  c.clauses_map.get_mut(&ClauseKind::Prelude).unwrap().push(qt! {
   #[ doc( inline ) ]
   #[ allow( unused_imports ) ]
   #attrs1
   #attrs2
   pub use __all__ :: #path ::prelude :: *;
 });

  Ok(())
 }

  ///
  /// Protocol of modularity unifying interface of a module and introducing layers.
  ///
  #[ allow( dead_code, clippy ::too_many_lines ) ]
  pub fn mod_interface(input: proc_macro ::TokenStream) -> syn ::Result< proc_macro2 ::TokenStream >
  {
  #[ allow( clippy ::enum_glob_use ) ]
  use ElementType :: *;

  let original_input = input.clone();
  let document = syn ::parse :: < Thesis >(input)?;
  document.inner_attributes_validate()?;
  let has_debug = document.has_debug();

  // use inspect_type :: *;
  // inspect_type_of!( immediates );

  let mut clauses_map: HashMap< _, Vec< proc_macro2 ::TokenStream >> = HashMap ::new();
  clauses_map.insert(ClauseImmediates ::Kind(), Vec ::new());
  //clauses_map.insert( VisPrivate ::Kind(), Vec ::new() );
  clauses_map.insert(ClauseKind::Own, Vec ::new());
  clauses_map.insert(ClauseKind::Orphan, Vec ::new());
  clauses_map.insert(ClauseKind::Exposed, Vec ::new());
  clauses_map.insert(ClauseKind::Prelude, Vec ::new());

  // zzz: test case with several attrs

  let mut record_context = RecordContext :: < '_ > {
   has_debug,
   clauses_map: &mut clauses_map,
 };

  document.records.0.iter().try_for_each(|record| {
   match record.element_type 
   {
  Use(_) =>
  {
   let vis = &record.vis;
   if vis == &Visibility ::Inherited
   {
  record_use_implicit(record, &mut record_context);
 } else {
  record_use_explicit(record, &mut record_context)?;
 }
 }
  Reuse(_) =>
  {
   let vis = &record.vis;
   if vis == &Visibility ::Inherited 
   {
  record_reuse_implicit(record, &mut record_context)?;
 } else {
  return Err(syn_err!(
   record,
   "Using visibility usesd before `reuse` is illegal\n{}",
   qt! { #record },
 ));
 }
 }
  _ =>
  {
   record.elements.iter().try_for_each(|element| -> syn ::Result< () > {
  match record.element_type 
  {
   MicroModule(_) =>
  {
  record_micro_module(record, element, &mut record_context)?;
 }
   Layer(_) =>
  {
  record_layer(record, element, &mut record_context)?;
 }
   _ =>
  {
  panic!("Unexpected")
 }
 }
  syn ::Result ::Ok(())
 })?;
 }
 }

   syn ::Result ::Ok(())
 })?;

  let immediates_clause = clauses_map.get(&ClauseImmediates ::Kind()).unwrap();
  let own_clause = clauses_map.get(&ClauseKind::Own).unwrap();
  let orphan_clause = clauses_map.get(&ClauseKind::Orphan).unwrap();
  let exposed_clause = clauses_map.get(&ClauseKind::Exposed).unwrap();
  let prelude_clause = clauses_map.get(&ClauseKind::Prelude).unwrap();

  let result = qt! {

   #( #immediates_clause )*

   // use private as __private__; // this line is necessary for readable error in case private namespace is not present

   #[ doc( inline ) ]
   #[ allow( unused_imports ) ]
   pub use own :: *;

   /// Own namespace of the module.
   #[ allow( unused_imports ) ]
   pub mod own
   {
  // There must be internal private namespace
  // Because it's not possible to direcly make `use super :: *;`
  // Because then items from super can't be exposed publicly complaining :
  // `error[E0428] : the name `mod1` is defined multiple times`
  // use super :: *;
  use super ::private; // this line is necessary for readable error in case private namespace is not present
  mod __all__
  {
   pub use super ::super :: *;
   pub use super ::super ::private :: *;
 }
  #[ doc( inline ) ]
  pub use super ::orphan :: *;
  #( #own_clause )*
 }

   /// Orphan namespace of the module.
   #[ allow( unused_imports ) ]
   pub mod orphan
   {
  // use super :: *;
  mod __all__
  {
   pub use super ::super :: *;
   pub use super ::super ::private :: *;
 }
  #[ doc( inline ) ]
  pub use super ::exposed :: *;
  #( #orphan_clause )*
 }

   /// Exposed namespace of the module.
   #[ allow( unused_imports ) ]
   pub mod exposed
   {
  // use super :: *;
  mod __all__
  {
   pub use super ::super :: *;
   pub use super ::super ::private :: *;
 }
  #[ doc( inline ) ]
  pub use super ::prelude :: *;
  #( #exposed_clause )*
 }

   /// Prelude to use essentials: `use my_module ::prelude :: *`.
   #[ allow( unused_imports ) ]
   pub mod prelude
   {
  // use super :: *;
  mod __all__
  {
   pub use super ::super :: *;
   pub use super ::super ::private :: *;
 }
  #( #prelude_clause )*
 }

 };

  if has_debug 
  {
   let about = "derive: mod_interface";
   diag ::report_print(about, &original_input, &result);
 }

  // if has_debug
  // {
  //   diag ::report_print( "derive: mod_interface", original_input, &result );
  // }

  Ok(result)
 }
}

/// Own namespace of the module.
#[ allow( unused_imports ) ]
pub mod own 
{

  use super :: *;
  pub use orphan :: *;
}

pub use own :: *;

/// Parented namespace of the module.
#[ allow( unused_imports ) ]
pub mod orphan 
{

  use super :: *;
  pub use exposed :: *;
}

/// Exposed namespace of the module.
#[ allow( unused_imports ) ]
pub mod exposed 
{

  use super :: *;
  pub use prelude :: *;
  pub use private :: { };
}

/// Prelude to use essentials: `use my_module ::prelude :: *`.
#[ allow( unused_imports ) ]
pub mod prelude 
{

  use super :: *;
  pub use private :: { mod_interface };
}