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

Function expand_string_assertion

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

Generate string literal assertion with error collection String literals always use .as_ref() to handle String/&str matching

(value_expr: &TokenStream, pattern: &PatternString)

Source from the content-addressed store, hash-verified

503/// Generate string literal assertion with error collection
504/// String literals always use .as_ref() to handle String/&str matching
505fn expand_string_assertion(value_expr: &TokenStream, pattern: &PatternString) -> TokenStream {
506 let lit = &pattern.lit;
507
508 // String patterns always use .as_ref() to handle String/&str matching
509 let span = lit.span();
510 let error_push = generate_error_push(
511 span,
512 quote!(format!("{:?}", actual)),
513 quote!(None),
514 pattern.node_id,
515 );
516
517 quote_spanned! {span=> {
518 // Take a reference to the expression result so that:
519 // 1. Temporaries (e.g. from method calls returning String) live for the
520 // entire block - fixes E0716 "temporary dropped while borrowed".
521 // 2. Reference-typed expressions (e.g. from index operations) are not
522 // moved - fixes E0507 "cannot move out of shared reference".
523 let __assert_struct_tmp = &#value_expr;
524 let actual = (*__assert_struct_tmp).as_ref();
525 if !matches!(actual, #lit) {
526 #error_push
527 }
528 }}
529}
530
531/// Generate simple assertion with error collection
532fn expand_simple_assertion(actual: &TokenStream, pattern: &PatternSimple) -> TokenStream {

Callers 1

expand_pattern_assertionFunction · 0.85

Calls 2

generate_error_pushFunction · 0.85
spanMethod · 0.45

Tested by

no test coverage detected