@@ -91,6 +91,21 @@ impl Display for Field {
9191#[ cfg_attr( feature = "serde" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
9292pub struct FieldPath ( Vec < Field > ) ;
9393
94+ /// A helpful constructor for creating `FieldPath`s to nested
95+ /// struct fields of the format `field_path!(x.y.z)`
96+ #[ macro_export]
97+ macro_rules! field_path {
98+ ( $front: ident) => { {
99+ $crate:: FieldPath :: from_name( stringify!( $front) )
100+ } } ;
101+ ( $front: ident $( . $rest: ident) +) => { {
102+ $crate:: FieldPath :: from_iter( [
103+ $crate:: Field :: from( stringify!( $front) ) ,
104+ $( $crate:: Field :: from( stringify!( $rest) ) ) ,+
105+ ] )
106+ } } ;
107+ }
108+
94109impl FieldPath {
95110 /// The selector for the root (i.e., the top-level struct itself)
96111 pub fn root ( ) -> Self {
@@ -350,7 +365,7 @@ mod tests {
350365 #[ test]
351366 fn nested_field_not_found ( ) {
352367 let dtype = DType :: struct_ ( [ ( "a" , DType :: Bool ( NonNullable ) ) ] , NonNullable ) ;
353- let path = FieldPath :: from_name ( "b" ) ;
368+ let path = field_path ! ( b ) ;
354369 assert ! ( path. resolve( dtype. clone( ) ) . is_none( ) ) ;
355370 assert ! ( !path. exists_in( dtype. clone( ) ) ) ;
356371
@@ -365,7 +380,7 @@ mod tests {
365380 [ ( "a" , DType :: Primitive ( PType :: I32 , NonNullable ) ) ] ,
366381 NonNullable ,
367382 ) ;
368- let path = FieldPath :: from_name ( "a" ) . push ( "b" ) ;
383+ let path = field_path ! ( a . b ) ;
369384 assert ! ( path. resolve( dtype. clone( ) ) . is_none( ) ) ;
370385 assert ! ( !path. exists_in( dtype. clone( ) ) ) ;
371386
0 commit comments