macro_tools/
derive.rs

1//!
2//! Macro helpers around derive macro and structure [`syn ::DeriveInput`].
3//!
4
5/// Define a private namespace for all its items.
6mod private 
7{
8
9  use crate :: *;
10  use syn ::punctuated ::Punctuated;
11
12  ///
13  /// Extracts the named fields from a struct defined in a `syn ::DeriveInput`.
14  ///
15  /// This function specifically handles `syn ::DeriveInput` that represent structs
16  /// with named fields. It will return an error if the provided AST does not conform to these expectations.
17  ///
18  /// # Example
19  ///
20  /// ```rust, ignore
21  /// let ast = match syn ::parse :: < syn ::DeriveInput >( input )
22  /// {
23  ///   Ok( syntax_tree ) => syntax_tree,
24  ///   Err( err ) => return Err( err ),
25  /// };
26  /// let fields = derive.named_fields( &ast );
27  /// ```
28  /// # Errors
29  /// qqq: doc
30  pub fn named_fields(ast: &syn ::DeriveInput) -> crate ::Result< &Punctuated<syn ::Field, syn ::token ::Comma >>
31  {
32  let fields =  match ast.data 
33  {
34   syn ::Data ::Struct(ref data_struct) =>  match data_struct.fields 
35  {
36  syn ::Fields ::Named(ref fields_named) => &fields_named.named,
37  _ =>
38  {
39   return Err(syn_err!(
40  ast,
41  "Unknown format of data, expected syn ::Fields ::Named( ref fields_named )\n  {}",
42  qt! { #ast }
43 ))
44 }
45 },
46   _ =>
47  {
48  return Err(syn_err!(
49   ast,
50   "Unknown format of data, expected syn ::Data ::Struct( ref data_struct )\n  {}",
51   qt! { #ast }
52 ))
53 }
54 };
55
56  Ok(fields)
57 }
58}
59
60#[ doc( inline ) ]
61#[ allow( unused_imports ) ]
62pub use own :: *;
63
64/// Own namespace of the module.
65#[ allow( unused_imports ) ]
66pub mod own 
67{
68
69  use super :: *;
70  #[ doc( inline ) ]
71  pub use orphan :: *;
72
73  #[ doc( inline ) ]
74  pub use private :: { named_fields };
75}
76
77/// Parented namespace of the module.
78#[ allow( unused_imports ) ]
79pub mod orphan 
80{
81
82  use super :: *;
83  #[ doc( inline ) ]
84  pub use exposed :: *;
85}
86
87/// Exposed namespace of the module.
88#[ allow( unused_imports ) ]
89pub mod exposed 
90{
91
92  use super :: *;
93  pub use super ::super ::derive;
94
95  #[ doc( inline ) ]
96  pub use prelude :: *;
97
98  #[ doc( inline ) ]
99  pub use private :: { };
100}
101
102/// Prelude to use essentials: `use my_module ::prelude :: *`.
103#[ allow( unused_imports ) ]
104pub mod prelude 
105{
106
107  use super :: *;
108
109  #[ doc( inline ) ]
110  pub use private :: { };
111}