From d489d716230f73b528d91ce9f7d3ecda4c52a4c4 Mon Sep 17 00:00:00 2001 From: Andrew Hlynskyi Date: Tue, 1 Aug 2023 12:21:13 +0300 Subject: [PATCH] fix: `children_by_field_id` API to receive non option field id --- lib/binding_rust/lib.rs | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/binding_rust/lib.rs b/lib/binding_rust/lib.rs index ae8e744d..2f8323df 100644 --- a/lib/binding_rust/lib.rs +++ b/lib/binding_rust/lib.rs @@ -1062,7 +1062,26 @@ impl<'tree> Node<'tree> { cursor: &'a mut TreeCursor<'tree>, ) -> impl Iterator> + 'a { let field_id = self.language().field_id_for_name(field_name); - self.children_by_field_id(field_id, cursor) + let mut done = field_id.is_none(); + if !done { + cursor.reset(*self); + cursor.goto_first_child(); + } + iter::from_fn(move || { + if !done { + while cursor.field_id() != field_id { + if !cursor.goto_next_sibling() { + return None; + } + } + let result = cursor.node(); + if !cursor.goto_next_sibling() { + done = true; + } + return Some(result); + } + None + }) } /// Iterate over this node's children with a given field id. @@ -1070,15 +1089,15 @@ impl<'tree> Node<'tree> { /// See also [Node::children_by_field_name]. pub fn children_by_field_id<'a>( &self, - field_id: Option, + field_id: FieldId, cursor: &'a mut TreeCursor<'tree>, ) -> impl Iterator> + 'a { cursor.reset(*self); cursor.goto_first_child(); let mut done = false; iter::from_fn(move || { - while !done { - while cursor.field_id() != field_id { + if !done { + while cursor.field_id() != Some(field_id) { if !cursor.goto_next_sibling() { return None; }