@@ -177,8 +177,6 @@ fn test_camel_to_snake_case() {
177
177
assert_eq ! ( camel_to_snake_case( "uint32Id" ) , "uint32_id" . to_string( ) ) ;
178
178
}
179
179
180
-
181
-
182
180
#[ derive( PartialEq , Clone ) ]
183
181
pub enum FormattedText {
184
182
Indent ( Box < FormattedText > ) ,
@@ -239,14 +237,25 @@ fn module_name(camel_case: &str) -> String {
239
237
240
238
const NAME_ANNOTATION_ID : u64 = 0xc2fe4c6d100166d0 ;
241
239
240
+ fn name_annotation_value ( annotation : schema_capnp:: annotation:: Reader ) -> capnp:: Result < & str > {
241
+ if let schema_capnp:: value:: Text ( t) = annotation. get_value ( ) ?. which ( ) ? {
242
+ let name = t?;
243
+ for c in name. chars ( ) {
244
+ if !( c == '_' || c. is_alphanumeric ( ) ) {
245
+ return Err ( capnp:: Error :: failed (
246
+ format ! ( "rust.name annotation value must only contain alphanumeric characters and '_'" ) ) )
247
+ }
248
+ }
249
+ Ok ( name)
250
+ } else {
251
+ Err ( capnp:: Error :: failed ( format ! ( "expected rust.name annotation value to be of type Text" ) ) )
252
+ }
253
+ }
254
+
242
255
fn get_field_name ( field : schema_capnp:: field:: Reader ) -> capnp:: Result < & str > {
243
256
for annotation in field. get_annotations ( ) ?. iter ( ) {
244
257
if annotation. get_id ( ) == NAME_ANNOTATION_ID {
245
- if let schema_capnp:: value:: Text ( t) = annotation. get_value ( ) ?. which ( ) ? {
246
- return t;
247
- } else {
248
- return Err ( capnp:: Error :: failed ( format ! ( "expected rust.name annotation value to be of type Text" ) ) ) ;
249
- }
258
+ return name_annotation_value ( annotation) ;
250
259
}
251
260
}
252
261
field. get_name ( )
@@ -255,10 +264,8 @@ fn get_field_name(field: schema_capnp::field::Reader) -> capnp::Result<&str> {
255
264
fn get_enumerant_name ( enumerant : schema_capnp:: enumerant:: Reader ) -> capnp:: Result < & str > {
256
265
for annotation in enumerant. get_annotations ( ) ?. iter ( ) {
257
266
if annotation. get_id ( ) == NAME_ANNOTATION_ID {
258
- if let schema_capnp:: value:: Text ( t) = annotation. get_value ( ) ?. which ( ) ? {
259
- return t;
260
- } else {
261
- return Err ( capnp:: Error :: failed ( format ! ( "expected rust.name annotation value to be of type Text" ) ) ) ;
267
+ if annotation. get_id ( ) == NAME_ANNOTATION_ID {
268
+ return name_annotation_value ( annotation) ;
262
269
}
263
270
}
264
271
}
@@ -291,11 +298,9 @@ fn populate_scope_map(node_map: &collections::hash_map::HashMap<u64, schema_capn
291
298
292
299
' annotations: for annotation in node_reader. get_annotations ( ) ?. iter ( ) {
293
300
if annotation. get_id ( ) == NAME_ANNOTATION_ID {
294
- if let schema_capnp :: value :: Text ( t ) = annotation . get_value ( ) ? . which ( ) ? {
295
- current_node_name = t ?. to_string ( ) ;
301
+ if annotation . get_id ( ) == NAME_ANNOTATION_ID {
302
+ current_node_name = name_annotation_value ( annotation ) ?. to_string ( ) ;
296
303
break ' annotations;
297
- } else {
298
- return Err ( capnp:: Error :: failed ( format ! ( "expected rust.name annotation value to be of type Text" ) ) ) ;
299
304
}
300
305
}
301
306
}
0 commit comments