@@ -10,6 +10,7 @@ use std::{
1010 fmt, hash, iter,
1111 marker:: PhantomData ,
1212 mem:: MaybeUninit ,
13+ num:: NonZeroU16 ,
1314 ops,
1415 os:: raw:: { c_char, c_void} ,
1516 ptr:: { self , NonNull } ,
@@ -93,6 +94,8 @@ pub enum LogType {
9394 Lex ,
9495}
9596
97+ type FieldId = NonZeroU16 ;
98+
9699/// A callback that receives log messages during parser.
97100type Logger < ' a > = Box < dyn FnMut ( LogType , & str ) + ' a > ;
98101
@@ -319,7 +322,7 @@ impl Language {
319322
320323 /// Get the numerical id for the given field name.
321324 #[ doc( alias = "ts_language_field_id_for_name" ) ]
322- pub fn field_id_for_name ( & self , field_name : impl AsRef < [ u8 ] > ) -> Option < u16 > {
325+ pub fn field_id_for_name ( & self , field_name : impl AsRef < [ u8 ] > ) -> Option < FieldId > {
323326 let field_name = field_name. as_ref ( ) ;
324327 let id = unsafe {
325328 ffi:: ts_language_field_id_for_name (
@@ -331,7 +334,7 @@ impl Language {
331334 if id == 0 {
332335 None
333336 } else {
334- Some ( id )
337+ Some ( FieldId :: new ( id ) . unwrap ( ) )
335338 }
336339 }
337340}
@@ -1060,23 +1063,23 @@ impl<'tree> Node<'tree> {
10601063 cursor : & ' a mut TreeCursor < ' tree > ,
10611064 ) -> impl Iterator < Item = Node < ' tree > > + ' a {
10621065 let field_id = self . language ( ) . field_id_for_name ( field_name) ;
1063- self . children_by_field_id ( field_id. unwrap_or ( 0 ) , cursor)
1066+ self . children_by_field_id ( field_id, cursor)
10641067 }
10651068
10661069 /// Iterate over this node's children with a given field id.
10671070 ///
10681071 /// See also [Node::children_by_field_name].
10691072 pub fn children_by_field_id < ' a > (
10701073 & self ,
1071- field_id : u16 ,
1074+ field_id : Option < FieldId > ,
10721075 cursor : & ' a mut TreeCursor < ' tree > ,
10731076 ) -> impl Iterator < Item = Node < ' tree > > + ' a {
10741077 cursor. reset ( * self ) ;
10751078 cursor. goto_first_child ( ) ;
10761079 let mut done = false ;
10771080 iter:: from_fn ( move || {
10781081 while !done {
1079- while cursor. field_id ( ) != Some ( field_id) {
1082+ while cursor. field_id ( ) != field_id {
10801083 if !cursor. goto_next_sibling ( ) {
10811084 return None ;
10821085 }
@@ -1242,13 +1245,13 @@ impl<'a> TreeCursor<'a> {
12421245 ///
12431246 /// See also [field_name](TreeCursor::field_name).
12441247 #[ doc( alias = "ts_tree_cursor_current_field_id" ) ]
1245- pub fn field_id ( & self ) -> Option < u16 > {
1248+ pub fn field_id ( & self ) -> Option < FieldId > {
12461249 unsafe {
12471250 let id = ffi:: ts_tree_cursor_current_field_id ( & self . 0 ) ;
12481251 if id == 0 {
12491252 None
12501253 } else {
1251- Some ( id )
1254+ Some ( FieldId :: new ( id ) . unwrap ( ) )
12521255 }
12531256 }
12541257 }
0 commit comments