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
//! # Infiltrait
//!
//! A procedural macro that automatically generates trait definitions from implementation blocks.
//!
//! The `#[infiltrait]` attribute macro allows you to define a trait and its implementation
//! simultaneously by writing only the impl block. This eliminates the need to separately
//! define the trait interface, reducing code duplication and keeping related code together.
//!
//! ## Usage
//!
//! ```rust
//! use infiltrait::infiltrait;
//!
//! struct MyStruct;
//!
//! #[infiltrait]
//! impl MyTrait for MyStruct {
//! const MAGIC_NUMBER: i32 = 42;
//! type Output = String;
//!
//! fn hello(&self) -> Output {
//! format!("Constant magic number: {}", MAGIC_NUMBER)
//! }
//! }
//!
//! // Generated code:
//! // trait MyTrait {
//! // type Output;
//! // const MAGIC_NUMBER: i32;
//! // fn hello(&self) -> String;
//! // }
//! //
//! // impl MyTrait for MyStruct {
//! // const MAGIC_NUMBER: i32 = 42;
//! // type Output = String;
//!
//! // fn hello(&self) -> Output {
//! // format!("Constant magic number: {}", MAGIC_NUMBER)
//! // }
//! // }
//! ```
//!
//! ## Features
//!
//! - Automatically generates trait definitions from impl blocks
//! - Supports methods, associated constants, and associated types
//! - Preserves visibility modifiers and unsafe markers
//! - Provides clear compile-time error messages
//! - Zero runtime overhead
//!
//! ## Limitations
//!
//! - Trait names cannot contain lifetimes or generic parameters
//! - Only supports trait implementations (not inherent impls)
//!
//! ## Examples
//!
//! ### Basic Usage
//!
//! ```rust
//! # use infiltrait::infiltrait;
//! struct Calculator;
//!
//! #[infiltrait]
//! impl Arithmetic for Calculator {
//! fn add(&self, a: i32, b: i32) -> i32 {
//! a + b
//! }
//!
//! fn multiply(&self, a: i32, b: i32) -> i32 {
//! a * b
//! }
//! }
//! ```
//!
//! ### With Associated Types and Constants
//!
//! ```rust
//! # use infiltrait::infiltrait;
//! struct Database;
//!
//! #[infiltrait]
//! impl Storage for Database {
//! type Item = String;
//! const MAX_ITEMS: usize = 1000;
//!
//! fn store(&mut self, item: Self::Item) -> Result<(), &'static str> {
//! Ok(()) // Implementation details...
//! }
//! }
//! ```
//!
//! ### Public Traits
//!
//! ```rust
//! # use infiltrait::infiltrait;
//! struct Service;
//!
//! #[infiltrait]
//! pub impl PublicApi for Service {
//! fn process(&self, data: &str) -> String {
//! data.to_uppercase()
//! }
//! }
//! ```
extern crate proc_macro;
use TokenStream;
/// The main procedural macro that generates trait definitions from implementation blocks.
///
/// This attribute macro takes an `impl TraitName for Type` block and automatically
/// generates the corresponding trait definition, then outputs both the trait and
/// the implementation.
///
/// # Arguments
///
/// * `_attr` - Attribute arguments (currently unused)
/// * `item` - The implementation block to process
///
/// # Returns
///
/// A `TokenStream` containing both the generated trait definition and the original
/// implementation block.
///
/// # Errors
///
/// This macro will produce compile-time errors if:
/// - The input is not a trait implementation (missing trait name)
/// - The trait name contains lifetimes or generic parameters
/// - The input cannot be parsed as a valid implementation block
///
/// # Examples
///
/// ```rust
/// # use infiltrait::infiltrait;
/// struct MyStruct;
///
/// #[infiltrait]
/// impl MyTrait for MyStruct {
/// fn greet(&self) -> &'static str {
/// "Hello!"
/// }
/// }
///
/// // Now you can use MyTrait as a regular trait:
/// fn use_trait<T: MyTrait>(obj: &T) {
/// println!("{}", obj.greet());
/// }
/// ```
/// Creates a trait definition from an implementation block.
///
/// This function extracts the trait name from the impl block and converts all
/// impl items (methods, constants, types) into their corresponding trait items
/// by removing method bodies and implementation details.
///
/// # Errors
///
/// Returns an error if:
/// - No trait name is specified in the impl block
/// - The trait name contains lifetimes or generic parameters
/// Converts an implementation item into its corresponding trait item.
///
/// This function transforms impl block items (which have implementations)
/// into trait items (which are just signatures). Method bodies are removed,
/// constant values become defaults set to `None`, and type implementations
/// become associated type declarations.
///
/// # Arguments
///
/// * `impl_item` - The implementation item to convert
///
/// # Returns
///
/// An `Option<syn::TraitItem>` containing the converted trait item, or `None`
/// if the impl item type is not supported (e.g., macros, verbatim items).
///
/// # Supported Item Types
///
/// - **Constants**: `const FOO: i32 = 42;` becomes `const FOO: i32;`
/// - **Functions**: Method implementations become method signatures
/// - **Types**: `type Foo = Bar;` becomes `type Foo;`
/// A parsed representation of an implementation block with its visibility modifier.
///
/// This struct combines the visibility modifier (pub, pub(crate), etc.) with
/// the implementation block itself. It's used as an intermediate representation
/// during parsing to ensure we capture both the visibility that should be applied
/// to the generated trait and the implementation details.
///
/// # Fields
///
/// * `visibility` - The visibility modifier applied to the impl block
/// * `impl_block` - The parsed implementation block
/// Parser implementation for `DefinableImplementation`.
///
/// This implementation allows `DefinableImplementation` to be parsed directly
/// from a `TokenStream` by first parsing the visibility modifier, then the
/// implementation block.