From 7ad21396c2ac5aa7ea4dfd250d1597c71ff8fea7 Mon Sep 17 00:00:00 2001 From: Amaan Qureshi Date: Mon, 24 Jul 2023 01:57:10 -0400 Subject: [PATCH] feat!: use `Option` for TSFieldIds --- lib/binding_rust/lib.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/binding_rust/lib.rs b/lib/binding_rust/lib.rs index 5bed0817..84c3d12e 100644 --- a/lib/binding_rust/lib.rs +++ b/lib/binding_rust/lib.rs @@ -10,6 +10,7 @@ use std::{ fmt, hash, iter, marker::PhantomData, mem::MaybeUninit, + num::NonZeroU16, ops, os::raw::{c_char, c_void}, ptr::{self, NonNull}, @@ -93,6 +94,8 @@ pub enum LogType { Lex, } +type FieldId = NonZeroU16; + /// A callback that receives log messages during parser. type Logger<'a> = Box; @@ -319,7 +322,7 @@ impl Language { /// Get the numerical id for the given field name. #[doc(alias = "ts_language_field_id_for_name")] - pub fn field_id_for_name(&self, field_name: impl AsRef<[u8]>) -> Option { + pub fn field_id_for_name(&self, field_name: impl AsRef<[u8]>) -> Option { let field_name = field_name.as_ref(); let id = unsafe { ffi::ts_language_field_id_for_name( @@ -331,7 +334,7 @@ impl Language { if id == 0 { None } else { - Some(id) + Some(FieldId::new(id).unwrap()) } } } @@ -1060,7 +1063,7 @@ 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.unwrap_or(0), cursor) + self.children_by_field_id(field_id, cursor) } /// Iterate over this node's children with a given field id. @@ -1068,7 +1071,7 @@ impl<'tree> Node<'tree> { /// See also [Node::children_by_field_name]. pub fn children_by_field_id<'a>( &self, - field_id: u16, + field_id: Option, cursor: &'a mut TreeCursor<'tree>, ) -> impl Iterator> + 'a { cursor.reset(*self); @@ -1076,7 +1079,7 @@ impl<'tree> Node<'tree> { let mut done = false; iter::from_fn(move || { while !done { - while cursor.field_id() != Some(field_id) { + while cursor.field_id() != field_id { if !cursor.goto_next_sibling() { return None; } @@ -1242,13 +1245,13 @@ impl<'a> TreeCursor<'a> { /// /// See also [field_name](TreeCursor::field_name). #[doc(alias = "ts_tree_cursor_current_field_id")] - pub fn field_id(&self) -> Option { + pub fn field_id(&self) -> Option { unsafe { let id = ffi::ts_tree_cursor_current_field_id(&self.0); if id == 0 { None } else { - Some(id) + Some(FieldId::new(id).unwrap()) } } }