facet-macro-parse 0.46.0

Parser for facet derive macros - transforms TokenStreams into structured type representations
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
use crate::{GenericParam, GenericParams, LifetimeName, ToTokens, TokenStream};
use quote::quote;

/// The name of a generic parameter
#[derive(Clone)]
pub enum GenericParamName {
    /// "a" but formatted as "'a"
    Lifetime(LifetimeName),

    /// "T", formatted as "T"
    Type(TokenStream),

    /// "N", formatted as "N"
    Const(TokenStream),
}

/// The name of a generic parameter with bounds
#[derive(Clone)]
pub struct BoundedGenericParam {
    /// the parameter name
    pub param: GenericParamName,

    /// bounds like `'static`, or `Send + Sync`, etc. — None if no bounds
    pub bounds: Option<TokenStream>,
}

/// Stores different representations of generic parameters for implementing traits.
///
/// This structure separates generic parameters into different formats needed when
/// generating trait implementations.
#[derive(Clone)]
pub struct BoundedGenericParams {
    /// Collection of generic parameters with their bounds
    pub params: Vec<BoundedGenericParam>,
}

/// Display wrapper that shows generic parameters with their bounds
///
/// This is used to format generic parameters for display purposes,
/// including both the parameter names and their bounds (if any).
///
/// # Example
///
/// For a parameter like `T: Clone`, this will display `<T: Clone>`.
pub struct WithBounds<'a>(&'a BoundedGenericParams);

/// Display wrapper that shows generic parameters without their bounds
///
/// This is used to format just the parameter names for display purposes,
/// omitting any bounds information.
///
/// # Example
///
/// For a parameter like `T: Clone`, this will display just `<T>`.
pub struct WithoutBounds<'a>(&'a BoundedGenericParams);

/// Display wrapper that outputs generic parameters as a PhantomData
///
/// This is used to format generic parameters as a PhantomData type
/// for use in trait implementations.
///
/// # Example
///
/// For parameters `<'a, T, const N: usize>`, this will display
/// `::core::marker::PhantomData<(*mut &'__facet (), T, [u32; N])>`.
pub struct AsPhantomData<'a>(&'a BoundedGenericParams);

impl quote::ToTokens for AsPhantomData<'_> {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        // Optimization: if there's exactly one parameter and it's a lifetime,
        // use the short 𝟋Ph<'lifetime> alias from the prelude
        if self.0.params.len() == 1
            && let GenericParamName::Lifetime(name) = &self.0.params[0].param
        {
            tokens.extend(quote! { 𝟋Ph<#name> });
            return;
        }

        // General case: build PhantomData<(...)> with all parameters
        let mut temp = TokenStream::new();

        {
            #[expect(unused)]
            let tokens = ();

            // Track if we've written anything to handle commas correctly
            let mut first_param = true;

            // Generate all parameters in the tuple
            for param in &self.0.params {
                if !first_param {
                    temp.extend(quote! { , });
                }

                match &param.param {
                    GenericParamName::Lifetime(name) => {
                        temp.extend(quote! { *mut &#name () });
                    }
                    GenericParamName::Type(name) => {
                        temp.extend(quote! { #name });
                    }
                    GenericParamName::Const(name) => {
                        temp.extend(quote! { [u32; #name] });
                    }
                }

                first_param = false;
            }

            // If no parameters at all, add a unit type to make the PhantomData valid
            if first_param {
                temp.extend(quote! { () });
            }
        }
        tokens.extend(quote! {
            ::core::marker::PhantomData<(#temp)>
        })
    }
}

impl BoundedGenericParams {
    /// Returns a display wrapper that formats generic parameters as a PhantomData type
    pub const fn as_phantom_data(&self) -> AsPhantomData<'_> {
        AsPhantomData(self)
    }
}

impl quote::ToTokens for WithBounds<'_> {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        if self.0.params.is_empty() {
            return;
        }

        tokens.extend(quote! {
            <
        });

        for (i, param) in self.0.params.iter().enumerate() {
            if i > 0 {
                tokens.extend(quote! { , });
            }

            match &param.param {
                GenericParamName::Lifetime(name) => {
                    tokens.extend(quote! { #name });
                }
                GenericParamName::Type(name) => {
                    tokens.extend(quote! { #name });
                }
                GenericParamName::Const(name) => {
                    tokens.extend(quote! { const #name });
                }
            }

            // Add bounds if they exist
            if let Some(bounds) = &param.bounds {
                tokens.extend(quote! { : #bounds });
            }
        }

        tokens.extend(quote! {
            >
        });
    }
}

impl quote::ToTokens for WithoutBounds<'_> {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        if self.0.params.is_empty() {
            return;
        }

        tokens.extend(quote! {
            <
        });

        for (i, param) in self.0.params.iter().enumerate() {
            if i > 0 {
                tokens.extend(quote! { , });
            }

            match &param.param {
                GenericParamName::Lifetime(name) => {
                    tokens.extend(quote! { #name });
                }
                GenericParamName::Type(name) => {
                    tokens.extend(quote! { #name });
                }
                GenericParamName::Const(name) => {
                    tokens.extend(quote! { #name });
                }
            }
        }

        tokens.extend(quote! {
            >
        });
    }
}

impl BoundedGenericParams {
    /// Returns a display wrapper that formats generic parameters with their bounds
    pub const fn display_with_bounds(&self) -> WithBounds<'_> {
        WithBounds(self)
    }

    /// Returns a display wrapper that formats generic parameters without their bounds
    pub const fn display_without_bounds(&self) -> WithoutBounds<'_> {
        WithoutBounds(self)
    }

    /// Returns a display wrapper that formats generic parameters as a PhantomData
    ///
    /// This is a convenience method for generating PhantomData expressions
    /// for use in trait implementations.
    ///
    /// # Example
    ///
    /// For generic parameters `<'a, T, const N: usize>`, this returns a wrapper that
    /// when displayed produces:
    /// `::core::marker::PhantomData<(*mut &'a (), T, [u32; N])>`
    pub const fn display_as_phantom_data(&self) -> AsPhantomData<'_> {
        AsPhantomData(self)
    }

    /// Adds a new generic parameter in the correct position (lifetimes, then types, then consts)
    pub fn with(&self, param: BoundedGenericParam) -> Self {
        let mut params = self.params.clone();

        match &param.param {
            GenericParamName::Lifetime(_) => {
                // Find the position after the last lifetime parameter
                let insert_position = params
                    .iter()
                    .position(|p| !matches!(p.param, GenericParamName::Lifetime(_)))
                    .unwrap_or(params.len());

                params.insert(insert_position, param);
            }
            GenericParamName::Type(_) => {
                // Find the position after the last type parameter but before any const parameters
                let after_lifetimes = params
                    .iter()
                    .position(|p| !matches!(p.param, GenericParamName::Lifetime(_)))
                    .unwrap_or(params.len());

                let insert_position = params[after_lifetimes..]
                    .iter()
                    .position(|p| matches!(p.param, GenericParamName::Const(_)))
                    .map(|pos| pos + after_lifetimes)
                    .unwrap_or(params.len());

                params.insert(insert_position, param);
            }
            GenericParamName::Const(_) => {
                // Constants go at the end
                params.push(param);
            }
        }

        Self { params }
    }

    /// Adds a new lifetime parameter with the given name without bounds
    ///
    /// This is a convenience method for adding a lifetime parameter
    /// that's commonly used in trait implementations.
    pub fn with_lifetime(&self, name: LifetimeName) -> Self {
        self.with(BoundedGenericParam {
            param: GenericParamName::Lifetime(name),
            bounds: None,
        })
    }

    /// Adds a new type parameter with the given name without bounds
    ///
    /// This is a convenience method for adding a type parameter
    /// that's commonly used in trait implementations.
    pub fn with_type(&self, name: TokenStream) -> Self {
        self.with(BoundedGenericParam {
            param: GenericParamName::Type(name),
            bounds: None,
        })
    }
}

impl BoundedGenericParams {
    /// Parses generic parameters into separate components for implementing traits.
    ///
    /// This method takes a generic parameter declaration and populates the BoundedGenericParams struct
    /// with different representations of the generic parameters needed for code generation.
    ///
    /// # Examples
    ///
    /// For a type like `struct Example<T: Clone, 'a, const N: usize>`, this would populate:
    /// params with entries for each parameter and their bounds.
    pub fn parse(generics: Option<&GenericParams>) -> Self {
        let Some(generics) = generics else {
            return Self { params: Vec::new() };
        };

        let mut params = Vec::new();

        for param in generics.params.iter() {
            match &param.value {
                GenericParam::Type {
                    name,
                    bounds,
                    default: _,
                } => {
                    params.push(BoundedGenericParam {
                        param: GenericParamName::Type(name.to_token_stream()),
                        bounds: bounds
                            .as_ref()
                            .map(|bounds| bounds.second.to_token_stream()),
                    });
                }
                GenericParam::Lifetime { name, bounds } => {
                    params.push(BoundedGenericParam {
                        param: GenericParamName::Lifetime(LifetimeName(name.name.clone())),
                        bounds: bounds
                            .as_ref()
                            .map(|bounds| bounds.second.to_token_stream()),
                    });
                }
                GenericParam::Const {
                    _const: _,
                    name,
                    _colon: _,
                    typ,
                    default: _,
                } => {
                    params.push(BoundedGenericParam {
                        param: GenericParamName::Const(name.to_token_stream()),
                        bounds: Some(typ.to_token_stream()),
                    });
                }
            }
        }

        Self { params }
    }
}

#[cfg(test)]
mod tests {
    use super::{BoundedGenericParam, BoundedGenericParams, GenericParamName};
    use crate::LifetimeName;
    use quote::{ToTokens as _, quote};

    // Helper to render ToTokens implementors to string for comparison
    fn render_to_string<T: quote::ToTokens>(t: T) -> String {
        quote!(#t).to_string()
    }

    #[test]
    fn test_empty_generic_params() {
        let p = BoundedGenericParams { params: vec![] };
        assert_eq!(render_to_string(p.display_with_bounds()), "");
        assert_eq!(render_to_string(p.display_without_bounds()), "");
    }

    #[test]
    fn print_multiple_generic_params() {
        let p = BoundedGenericParams {
            params: vec![
                BoundedGenericParam {
                    bounds: Some(quote! { 'static }),
                    param: GenericParamName::Lifetime(LifetimeName(quote::format_ident!("a"))),
                },
                BoundedGenericParam {
                    bounds: Some(quote! { Clone + Debug }),
                    param: GenericParamName::Type(quote! { T }),
                },
                BoundedGenericParam {
                    bounds: None,
                    param: GenericParamName::Type(quote! { U }),
                },
                BoundedGenericParam {
                    bounds: Some(quote! { usize }), // Const params bounds are types
                    param: GenericParamName::Const(quote! { N }),
                },
            ],
        };
        // Check display with bounds
        let expected_with_bounds = quote! { <'a : 'static, T : Clone + Debug, U, const N : usize> };
        assert_eq!(
            p.display_with_bounds().to_token_stream().to_string(),
            expected_with_bounds.to_string()
        );

        // Check display without bounds
        let expected_without_bounds = quote! { <'a, T, U, N> }; // Note: const param N doesn't show `const` or type here
        assert_eq!(
            p.display_without_bounds().to_token_stream().to_string(),
            expected_without_bounds.to_string()
        );
    }

    #[test]
    fn test_add_mixed_parameters() {
        // Create a complex example with all parameter types
        let mut params = BoundedGenericParams { params: vec![] };

        // Add parameters in different order to test sorting
        params = params.with(BoundedGenericParam {
            bounds: None,
            param: GenericParamName::Type(quote! { T }),
        });

        params = params.with(BoundedGenericParam {
            bounds: Some(quote! { usize }), // Const bounds are types
            param: GenericParamName::Const(quote! { N }),
        });

        params = params.with(BoundedGenericParam {
            bounds: None,
            param: GenericParamName::Lifetime(LifetimeName(quote::format_ident!("a"))),
        });

        params = params.with(BoundedGenericParam {
            bounds: Some(quote! { Clone }),
            param: GenericParamName::Type(quote! { U }),
        });

        params = params.with(BoundedGenericParam {
            bounds: Some(quote! { 'static }),
            param: GenericParamName::Lifetime(LifetimeName(quote::format_ident!("b"))),
        });

        params = params.with(BoundedGenericParam {
            bounds: Some(quote! { u8 }), // Const bounds are types
            param: GenericParamName::Const(quote! { M }),
        });

        // Expected order: lifetimes first, then types, then consts
        let expected_without_bounds = quote! { <'a, 'b, T, U, N, M> };
        // Compare string representations for robust assertion
        assert_eq!(
            params
                .display_without_bounds()
                .to_token_stream()
                .to_string(),
            expected_without_bounds.to_string()
        );

        let expected_with_bounds =
            quote! { <'a, 'b : 'static, T, U : Clone, const N : usize, const M : u8> };
        // Compare string representations for robust assertion
        assert_eq!(
            params.display_with_bounds().to_token_stream().to_string(),
            expected_with_bounds.to_string()
        );
    }

    #[test]
    fn test_phantom_data_formatting() {
        // Empty params should have PhantomData with a unit type
        let empty = BoundedGenericParams { params: vec![] };
        assert_eq!(
            render_to_string(empty.display_as_phantom_data()),
            ":: core :: marker :: PhantomData < (()) >"
        );

        // Single lifetime - uses short 𝟋Ph alias
        let lifetime = BoundedGenericParams {
            params: vec![BoundedGenericParam {
                param: GenericParamName::Lifetime(LifetimeName(quote::format_ident!("a"))),
                bounds: None,
            }],
        };
        assert_eq!(
            render_to_string(lifetime.display_as_phantom_data()),
            "𝟋Ph < 'a >"
        );

        // Single type
        let type_param = BoundedGenericParams {
            params: vec![BoundedGenericParam {
                param: GenericParamName::Type(quote! { T }),
                bounds: None,
            }],
        };
        assert_eq!(
            render_to_string(type_param.display_as_phantom_data()),
            ":: core :: marker :: PhantomData < (T) >"
        );

        // Single const
        let const_param = BoundedGenericParams {
            params: vec![BoundedGenericParam {
                param: GenericParamName::Const(quote! { N }),
                bounds: None, // Bounds are irrelevant for PhantomData formatting
            }],
        };
        assert_eq!(
            render_to_string(const_param.display_as_phantom_data()),
            ":: core :: marker :: PhantomData < ([u32 ; N]) >"
        );

        // Complex mix of params
        let mixed = BoundedGenericParams {
            params: vec![
                BoundedGenericParam {
                    param: GenericParamName::Lifetime(LifetimeName(quote::format_ident!("a"))),
                    bounds: None,
                },
                BoundedGenericParam {
                    param: GenericParamName::Type(quote! { T }),
                    bounds: Some(quote! { Clone }), // Bounds irrelevant here
                },
                BoundedGenericParam {
                    param: GenericParamName::Const(quote! { N }),
                    bounds: Some(quote! { usize }), // Bounds irrelevant here
                },
            ],
        };
        let actual_tokens = mixed.display_as_phantom_data();
        let expected_tokens = quote! {
            ::core::marker::PhantomData<(*mut &'a (), T, [u32; N])>
        };
        assert_eq!(
            actual_tokens.to_token_stream().to_string(),
            expected_tokens.to_string()
        );
    }
}