From 7054c75c9d67944277a812a6e691e5c6ab3c7515 Mon Sep 17 00:00:00 2001 From: Matthijs Brobbel Date: Wed, 14 Sep 2022 17:28:18 +0200 Subject: [PATCH] Escape keywords in TypeName --- pbjson-build/src/descriptor.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pbjson-build/src/descriptor.rs b/pbjson-build/src/descriptor.rs index 7ecdb37..2fb87f4 100644 --- a/pbjson-build/src/descriptor.rs +++ b/pbjson-build/src/descriptor.rs @@ -12,6 +12,8 @@ use prost_types::{ FileDescriptorProto, FileDescriptorSet, MessageOptions, OneofDescriptorProto, }; +use crate::escape::escape_ident; + #[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)] pub struct Package { path: Vec, @@ -63,12 +65,12 @@ impl TypeName { "type name cannot contain \'.\', got \"{}\"", s ); - Self(s) + Self(escape_ident(s)) } pub fn to_snake_case(&self) -> String { use heck::ToSnakeCase; - self.0.to_snake_case() + escape_ident(self.0.to_snake_case()) } pub fn to_upper_camel_case(&self) -> String { @@ -299,4 +301,10 @@ mod tests { String::from("foo_bar.baz.boo") ) } + + #[test] + fn escape_keywords() { + assert_eq!(TypeName::new("type").to_string(), "r#type"); + assert_eq!(TypeName::new("Type").to_snake_case(), "r#type"); + } }