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

Method tail_operations

assert-struct-macros/src/pattern/field.rs:239–277  ·  view source on GitHub ↗

Get operations after the root field access (tail operations) For NamedField/UnnamedField alone, returns None (no additional operations) For Chained, returns all operations except the first non-Deref field access Examples: - Chained([Deref, NamedField("x")]) → Some(Deref) - Chained([NamedField("x"), Method("len")]) → Some(Method("len")) - Chained([Deref, NamedField("x"), Method("len")]) → Some(Cha

(&self)

Source from the content-addressed store, hash-verified

237 /// - Chained([NamedField("x"), Method("len")]) → Some(Method("len"))
238 /// - Chained([Deref, NamedField("x"), Method("len")]) → Some(Chained([Deref, Method("len")]))
239 pub(crate) fn tail_operations(&self) -> Option<Self> {
240 match self {
241 FieldOperation::NamedField { .. } | FieldOperation::UnnamedField { .. } => {
242 // Just a field access, no additional operations
243 None
244 }
245 FieldOperation::Chained { operations, span } => {
246 // Find the index of the first non-Deref field access
247 let field_access_idx = operations
248 .iter()
249 .position(|op| {
250 matches!(
251 op,
252 FieldOperation::NamedField { .. } | FieldOperation::UnnamedField { .. }
253 )
254 })
255 .expect("Chained operation must have at least one field access");
256
257 // Collect all operations except the field access itself
258 let mut tail_ops: Vec<_> = operations[..field_access_idx].to_vec();
259 tail_ops.extend_from_slice(&operations[field_access_idx + 1..]);
260
261 if tail_ops.is_empty() {
262 None
263 } else if tail_ops.len() == 1 {
264 Some(tail_ops.into_iter().next().unwrap())
265 } else {
266 Some(FieldOperation::Chained {
267 operations: tail_ops,
268 span: *span,
269 })
270 }
271 }
272 // For other operation types (Method, Await, Index, Deref), they don't have a root field to strip
273 // These should not appear at the root level of a FieldAssertion, but if they do,
274 // return None to indicate no tail
275 _ => None,
276 }
277 }
278}
279
280impl FieldOperation {

Callers 2

expand_field_assertionFunction · 0.80

Calls 2

is_emptyMethod · 0.80
lenMethod · 0.80

Tested by

no test coverage detected