MCPcopy Create free account
hub / github.com/carllerche/assert-struct / process_tuple_elements

Function process_tuple_elements

assert-struct-macros/src/expand.rs:326–375  ·  view source on GitHub ↗

Helper function to process tuple elements and generate match patterns and assertions. This function handles the common pattern of iterating through elements and: - Creating `_` patterns for wildcards (which need no assertions) - Creating named bindings for other patterns and generating their assertions - Handling field operations like dereferencing # Parameters - `elements`: The tuple elements t

(
    elements: &[TupleElement],
    prefix: &str,
)

Source from the content-addressed store, hash-verified

324/// - `match_patterns`: TokenStreams for use in match arms or destructuring
325/// - `assertions`: TokenStreams for the generated assertion code
326fn process_tuple_elements(
327 elements: &[TupleElement],
328 prefix: &str,
329) -> (Vec<TokenStream>, Vec<TokenStream>) {
330 let mut match_patterns = Vec::new();
331 let mut assertions = Vec::new();
332
333 for (i, tuple_element) in elements.iter().enumerate() {
334 match tuple_element {
335 TupleElement::Positional(pattern) => {
336 match &**pattern {
337 Pattern::Wildcard(PatternWildcard { .. }) => {
338 // Wildcard patterns use `_` in the match pattern
339 match_patterns.push(quote! { _ });
340 }
341 _ => {
342 // Non-wildcard patterns need a binding and assertion
343 let name = quote::format_ident!("{}{}", prefix, i);
344 match_patterns.push(quote! { #name });
345
346 // Generate assertion with error collection
347 let assertion = expand_pattern_assertion(&quote! { #name }, pattern);
348 assertions.push(assertion);
349 }
350 }
351 }
352 TupleElement::Indexed(boxed_elem) => {
353 let pattern = &boxed_elem.pattern;
354
355 match pattern {
356 Pattern::Wildcard(PatternWildcard { .. }) => {
357 // Wildcard patterns use `_` in the match pattern
358 match_patterns.push(quote! { _ });
359 }
360 _ => {
361 // Non-wildcard patterns need a binding and assertion
362 let name = quote::format_ident!("{}{}", prefix, i);
363 match_patterns.push(quote! { #name });
364
365 // Expand the indexed FieldAssertion starting from the bound element name
366 let assertion = expand_field_assertion(&quote! { #name }, boxed_elem);
367 assertions.push(assertion);
368 }
369 }
370 }
371 }
372 }
373
374 (match_patterns, assertions)
375}
376
377/// Generate assertion for plain tuples with error collection.
378/// Uses match expressions for consistency with enum tuple handling.

Callers 2

expand_tuple_assertionFunction · 0.85
expand_enum_assertionFunction · 0.85

Calls 3

expand_pattern_assertionFunction · 0.85
expand_field_assertionFunction · 0.85
pushMethod · 0.80

Tested by

no test coverage detected