Skip to content

Commit 05ca544

Browse files
committed
add validation of name annotation value
1 parent e546bc2 commit 05ca544

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

capnpc/src/codegen.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,6 @@ fn test_camel_to_snake_case() {
177177
assert_eq!(camel_to_snake_case("uint32Id"), "uint32_id".to_string());
178178
}
179179

180-
181-
182180
#[derive(PartialEq, Clone)]
183181
pub enum FormattedText {
184182
Indent(Box<FormattedText>),
@@ -239,14 +237,25 @@ fn module_name(camel_case: &str) -> String {
239237

240238
const NAME_ANNOTATION_ID: u64 = 0xc2fe4c6d100166d0;
241239

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+
242255
fn get_field_name(field: schema_capnp::field::Reader) -> capnp::Result<&str> {
243256
for annotation in field.get_annotations()?.iter() {
244257
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);
250259
}
251260
}
252261
field.get_name()
@@ -255,10 +264,8 @@ fn get_field_name(field: schema_capnp::field::Reader) -> capnp::Result<&str> {
255264
fn get_enumerant_name(enumerant: schema_capnp::enumerant::Reader) -> capnp::Result<&str> {
256265
for annotation in enumerant.get_annotations()?.iter() {
257266
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);
262269
}
263270
}
264271
}
@@ -291,11 +298,9 @@ fn populate_scope_map(node_map: &collections::hash_map::HashMap<u64, schema_capn
291298

292299
'annotations: for annotation in node_reader.get_annotations()?.iter() {
293300
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();
296303
break 'annotations;
297-
} else {
298-
return Err(capnp::Error::failed(format!("expected rust.name annotation value to be of type Text")));
299304
}
300305
}
301306
}

0 commit comments

Comments
 (0)