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

Method parse_comma_separated

assert-struct-macros/src/pattern/tuple.rs:73–127  ·  view source on GitHub ↗

Parse a comma-separated list of tuple elements, supporting both positional and indexed syntax. Used inside tuple patterns to handle mixed syntax like ("foo", *1: "bar", "baz")

(input: ParseStream)

Source from the content-addressed store, hash-verified

71 /// Parse a comma-separated list of tuple elements, supporting both positional and indexed syntax.
72 /// Used inside tuple patterns to handle mixed syntax like ("foo", *1: "bar", "baz")
73 pub(crate) fn parse_comma_separated(input: ParseStream) -> syn::Result<Vec<Self>> {
74 let mut elements = Vec::new();
75 let mut position = 0;
76
77 while !input.is_empty() {
78 // Try to parse as indexed element by attempting FieldOperation parse
79 let fork = input.fork();
80
81 if fork.parse::<Pattern>().is_ok() && !fork.peek(Token![:]) {
82 // Parse as positional pattern
83 let pattern = input.parse()?;
84 elements.push(TupleElement::Positional(Box::new(pattern)));
85 } else {
86 // Parse as indexed element
87 let operations: FieldOperation = input.parse()?;
88 let root_field = operations.root_field_name();
89
90 // Validate that the index matches the current position
91 match root_field {
92 FieldName::Index(index) if index == position => {
93 // Valid indexed element
94 let _: Token![:] = input.parse()?;
95 let pattern = input.parse()?;
96
97 elements.push(TupleElement::Indexed(Box::new(FieldAssertion {
98 operations,
99 pattern,
100 })));
101 }
102 FieldName::Index(index) => {
103 // Index doesn't match position
104 return Err(syn::Error::new(
105 input.span(),
106 format!("Index {} must match position {} in tuple", index, position),
107 ));
108 }
109 FieldName::Ident(_) => {
110 // Index doesn't match position
111 return Err(syn::Error::new(
112 input.span(),
113 "Operations like * can only be used with indexed elements (e.g., *0:, *1:)",
114 ));
115 }
116 }
117 }
118
119 position += 1;
120
121 if !input.is_empty() {
122 let _: Token![,] = input.parse()?;
123 }
124 }
125
126 Ok(elements)
127 }
128}

Callers

nothing calls this directly

Calls 5

is_emptyMethod · 0.80
pushMethod · 0.80
root_field_nameMethod · 0.80
parseMethod · 0.45
spanMethod · 0.45

Tested by

no test coverage detected