MCPcopy Index your code
hub / github.com/carllerche/assert-struct / parse

Method parse

assert-struct-macros/src/pattern/struct_pattern.rs:30–89  ·  view source on GitHub ↗

Parses a struct pattern with braces. # Example Input ```text User { name: "Alice", age: >= 18, .. } { name: "Alice" } // anonymous struct (.. implied, never required) ``` Handles both named structs (with a path) and wildcard structs (starting with `_`).

(input: syn::parse::ParseStream)

Source from the content-addressed store, hash-verified

28 ///
29 /// Handles both named structs (with a path) and wildcard structs (starting with `_`).
30 fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
31 let node_id = next_node_id();
32
33 // Check if this is a wildcard/anonymous struct pattern: _ { ... } or { ... }
34 let path = if input.peek(Token![_]) {
35 let _: Token![_] = input.parse()?;
36 None // wildcard has no path
37 } else if input.peek(syn::token::Brace) {
38 None // bare { ... } — anonymous struct
39 } else {
40 // Named struct pattern: TypeName { ... }
41 Some(input.parse::<syn::Path>()?)
42 };
43
44 // Parse the braced contents
45 let content;
46 syn::braced!(content in input);
47
48 // Parse comma-separated field assertions with optional rest pattern (..)
49 let mut fields = Punctuated::new();
50 let mut rest = false;
51
52 while !content.is_empty() {
53 // Check for rest pattern (..) which allows partial matching
54 if content.peek(Token![..]) {
55 let _: Token![..] = content.parse()?;
56 rest = true;
57 break;
58 }
59
60 fields.push_value(content.parse()?);
61
62 if content.is_empty() {
63 break;
64 }
65
66 let comma: Token![,] = content.parse()?;
67 fields.push_punct(comma);
68
69 // Rest pattern can appear after a comma
70 if content.peek(Token![..]) {
71 let _: Token![..] = content.parse()?;
72 rest = true;
73 break;
74 }
75 }
76
77 // Wildcard struct patterns always imply partial matching since the type
78 // is unknown, so .. is never required (but still accepted if written).
79 if path.is_none() {
80 rest = true;
81 }
82
83 Ok(PatternStruct {
84 node_id,
85 path,
86 fields,
87 rest,

Callers

nothing calls this directly

Calls 2

next_node_idFunction · 0.85
is_emptyMethod · 0.80

Tested by

no test coverage detected