1- use  anyhow:: bail; 
2- 
31pub  const  MAX_IDENTIFIER_LEN :  usize  = 64 ; 
42
53pub  const  IDENTIFIER_REQUIREMENTS :  & str  =
@@ -24,33 +22,45 @@ pub const IDENTIFIER_REQUIREMENTS: &str =
2422/// [3]: <https://util.unicode.org/UnicodeJsps/list-unicodeset.jsp?a=%5B%3AXID_START%3A%5D&g=&i=> 
2523/// [4]: <https://util.unicode.org/UnicodeJsps/list-unicodeset.jsp?a=%5B%3AXID_CONTINUE%3A%5D&g=&i=> 
2624pub  fn  check_valid_identifier ( s :  & str )  -> anyhow:: Result < ( ) >  { 
25+  check_valid_identifier_inner ( s) . map_err ( |e| anyhow:: anyhow!( e) ) 
26+ } 
27+ 
28+ pub  fn  is_valid_identifier ( s :  & str )  -> bool  { 
29+  check_valid_identifier_inner ( s) . is_ok ( ) 
30+ } 
31+ 
32+ fn  check_valid_identifier_inner ( s :  & str )  -> Result < ( ) ,  String >  { 
2733 let  mut  chars = s. chars ( ) ; 
2834 match  chars. next ( )  { 
2935 Some ( c)  if  c. is_ascii_alphabetic ( )  => ( ) , 
3036 Some ( '_' )  => ( ) , 
31-  Some ( c)  => bail ! ( 
32-  "Invalid first character '{c}' in {s}: Identifiers must start with an alphabetic \  
33- 
34-  ) , 
35-  None  => bail ! ( "Identifier cannot be empty" ) , 
37+  Some ( c)  => { 
38+  return  Err ( format ! ( 
39+  "Invalid first character '{c}' in {s}: Identifiers must start with an alphabetic \  
40+ 
41+  ) ) 
42+  } , 
43+  None  => return  Err ( format ! ( "Identifier cannot be empty" ) ) , 
3644 } ; 
3745 for  c in  chars { 
3846 if  !c. is_ascii_alphanumeric ( )  && c != '_'  { 
39-  bail ! ( 
47+  return   Err ( format ! ( 
4048 "Identifier {s} has invalid character '{c}': Identifiers can only contain \  
4149
42-  ) ; 
50+  ) ) ; 
4351 } 
4452 } 
4553 if  s. len ( )  > MAX_IDENTIFIER_LEN  { 
46-  bail ! ( 
54+  return   Err ( format ! ( 
4755 "Identifier is too long ({} > maximum {})" , 
4856 s. len( ) , 
4957 MAX_IDENTIFIER_LEN 
50-  ) ; 
58+  ) ) ; 
5159 } 
5260 if  s. chars ( ) . all ( |c| c == '_' )  { 
53-  bail ! ( "Identifier {s} cannot have exclusively underscores" ) ; 
61+  return  Err ( format ! ( 
62+  "Identifier {s} cannot have exclusively underscores" 
63+  ) ) ; 
5464 } 
5565 Ok ( ( ) ) 
5666} 
@@ -64,33 +74,45 @@ pub const MAX_FIELD_NAME_LENGTH: usize = 1024;
6474/// Field names cannot start with '$', must contain only non-control ASCII 
6575/// characters, and must be at most 1024 characters long. 
6676pub  fn  check_valid_field_name ( s :  & str )  -> anyhow:: Result < ( ) >  { 
77+  check_valid_field_name_inner ( s) . map_err ( |e| anyhow:: anyhow!( e) ) 
78+ } 
79+ 
80+ pub  fn  is_valid_field_name ( s :  & str )  -> bool  { 
81+  check_valid_field_name_inner ( s) . is_ok ( ) 
82+ } 
83+ 
84+ fn  check_valid_field_name_inner ( s :  & str )  -> Result < ( ) ,  String >  { 
6785 let  mut  chars = s. chars ( ) ; 
6886 match  chars. next ( )  { 
69-  Some ( '$' )  => bail ! ( "Field name {s} starts with '$', which is reserved." ) , 
87+  Some ( '$' )  => { 
88+  return  Err ( format ! ( 
89+  "Field name {s} starts with '$', which is reserved." 
90+  ) ) 
91+  } , 
7092 Some ( c)  => { 
7193 if  !c. is_ascii ( )  || c. is_ascii_control ( )  { 
72-  bail ! ( 
94+  return   Err ( format ! ( 
7395 "Field name {s} has invalid character '{c}': Field names can only contain \  
7496
75-  ) ; 
97+  ) ) ; 
7698 } 
7799 } , 
78-  None  => bail ! ( "Field name cannot be empty" ) , 
100+  None  => return   Err ( format ! ( "Field name cannot be empty" ) ) , 
79101 } ; 
80102 for  c in  chars { 
81103 if  !c. is_ascii ( )  || c. is_ascii_control ( )  { 
82-  bail ! ( 
104+  return   Err ( format ! ( 
83105 "Field name {s} has invalid character '{c}': Field names can only contain \  
84106
85-  ) ; 
107+  ) ) ; 
86108 } 
87109 } 
88110 if  s. len ( )  > MAX_FIELD_NAME_LENGTH  { 
89-  bail ! ( 
111+  return   Err ( format ! ( 
90112 "Field name is too long ({} > maximum {})" , 
91113 s. len( ) , 
92114 MAX_FIELD_NAME_LENGTH 
93-  ) ; 
115+  ) ) ; 
94116 } 
95117 Ok ( ( ) ) 
96118} 
0 commit comments