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

Function expand

assert-struct-macros/src/expand.rs:18–73  ·  view source on GitHub ↗
(assert: &AssertStruct)

Source from the content-addressed store, hash-verified

16use nodes::{expand_pattern_node_ident, generate_pattern_nodes};
17
18pub fn expand(assert: &AssertStruct) -> TokenStream {
19 let value = &assert.value;
20 let pattern = &assert.pattern;
21
22 // Generate pattern nodes using the node IDs from the patterns
23 let mut node_defs = Vec::new();
24 let root_ref = generate_pattern_nodes(pattern, &mut node_defs, None);
25
26 // Generate static declarations for all nodes
27 let node_constants: Vec<TokenStream> = node_defs
28 .iter()
29 .map(|(id, def)| {
30 let ident = Ident::new(&format!("__PATTERN_NODE_{}", id), Span::call_site());
31 quote! {
32 static #ident: ::assert_struct::__macro_support::PatternNode = #def;
33 }
34 })
35 .collect();
36
37 let assertion = expand_pattern_assertion(&quote! { #value }, pattern);
38
39 // Wrap in a block to avoid variable name conflicts
40 quote! {
41 {
42 // Suppress clippy warnings that are expected in macro-generated code
43 #[allow(unused_assignments, clippy::neg_cmp_op_on_partial_ord, clippy::op_ref, clippy::zero_prefixed_literal, clippy::bool_comparison, clippy::redundant_pattern_matching, clippy::useless_asref)]
44 let __assert_struct_result = {
45 use std::convert::AsRef;
46
47 // Generate all node constants
48 #(#node_constants)*
49
50 // Store the pattern tree root
51 const __PATTERN_TREE: &::assert_struct::__macro_support::PatternNode = &#root_ref;
52
53 // Create error report. Both values are compile-time constants:
54 // - CARGO_MANIFEST_DIR: absolute path to this package's root
55 // - file!(): path relative to the workspace root
56 // Together they let us derive the absolute source path at runtime
57 // without relying on the working directory.
58 let mut __report = ::assert_struct::__macro_support::ErrorReport::new(
59 ::std::env!("CARGO_MANIFEST_DIR"),
60 ::std::file!(),
61 );
62
63 #assertion
64
65 // Check if any errors were collected
66 if !__report.is_empty() {
67 panic!("{}", __report);
68 }
69 };
70 __assert_struct_result
71 }
72 }
73}
74
75/// Generate assertion code with error collection instead of immediate panic.

Callers 1

assert_structFunction · 0.85

Calls 2

generate_pattern_nodesFunction · 0.85
expand_pattern_assertionFunction · 0.85

Tested by

no test coverage detected