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
//! # Nested Structs & Enums
//! This crate allows you to nest structs, enums, and impls, in a way that is similar to how it's done in Zig.
//!
//! # Examples
//!
//! ```rust
//!
//! nesting::nest! {
//! // #![] attributes apply to all structs and enums in the nest!{} block.
//! #![derive(Debug)]
//! // Becomes:
//! // #![structs(allow(dead_code))]
//! // #![enums(allow(dead_code))]
//!
//! // You can also scope the attributes like so:
//! #![all(allow(dead_code))] // using `all` means it will also apply to `impl`s!
//! // Which is the same as:
//! // #![structs(allow(dead_code))]
//! // #![enums(allow(dead_code))]
//! // #![impls(allow(dead_code))]
//!
//! pub struct MarkerStruct;
//!
//! struct TupleStruct(String, u32);
//!
//! struct FieldStruct {
//! field1: String,
//! field2: u32
//! child: ChildStruct,
//! my_enum: Enum
//!
//! // Loose functions get put into impl StructName {} blocks.
//! // i.e. this would be impl FieldStruct { pub fn my_fn() {} }
//! pub fn some_struct_fn(&self) {
//! println!("This function is nested inside the struct!");
//! }
//!
//! // You can add attributes to individual structs/enums/impls as usual
//! #[derive(Clone, Hash)]
//! struct ChildStruct {
//! field1: String,
//! field2: u32,
//! child: ChildChildStruct
//!
//! // You can nest infinitely...
//! #[derive(Clone, Hash)]
//! struct ChildChildStruct;
//! }
//!
//! impl ChildStruct {
//! pub fn some_impl_fn(&self) {
//! println!("This function is nested inside struct->impl");
//! }
//! }
//!
//! // We can impl Foo for Bar
//! impl std::fmt::Display for ChildStruct {
//! fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
//! write!(f, "{}: {}", &self.field1, self.field2)
//! }
//! }
//!
//! // We can make enums too
//! enum Enum {
//! A,
//! B(String),
//! C {
//! a: String,
//! b: u64,
//! }
//!
//! // Nesting works just fine in enums
//! #[derive(Default)]
//! struct StructInsideEnum(String);
//!
//! enum Enum2 { A, B, C }
//!
//! pub fn some_enum_fn(&self) {
//! println!("This is a function inside an enum!");
//! }
//! }
//! }
//! }
//! ```
use TokenStream;
use quote;
use parse_macro_input;
/// Create nested structs, enums, and impls.
///
/// # Examples
///
/// ```rust
/// nesting::nest! {
/// struct MyStruct {
/// child: MyChildStruct
///
/// struct MyChildStruct {
/// is_nested: bool,
///
/// impl Default for MyChildStruct {
/// fn default() -> Self {
/// Self { is_nested: true}
/// }
/// }
/// }
/// }
/// }
/// ```