@@ -11,7 +11,7 @@ use crate::{
1111 middle:: {
1212 context:: Mode ,
1313 ext:: {
14- FileExts , ModExts ,
14+ FileExts , ItemExts , ModExts ,
1515 pb:: { Extendee , ExtendeeKind , Extendees , FieldType } ,
1616 } ,
1717 ty:: { self } ,
@@ -422,7 +422,7 @@ impl CodegenBackend for ProtobufBackend {
422422 const PROTOCOL : & ' static str = "protobuf" ;
423423
424424 fn codegen_struct_impl ( & self , def_id : DefId , stream : & mut String , s : & rir:: Message ) {
425- let idl_name = s. name . sym . 0 . clone ( ) ;
425+ let idl_name = s. name . raw_str ( ) ;
426426 let name = self . cx . rust_name ( def_id) ;
427427 let mut encoded_len = s
428428 . fields
@@ -546,25 +546,45 @@ impl CodegenBackend for ProtobufBackend {
546546
547547 if self . cx . config . with_descriptor {
548548 let file_id = self . cx . node ( def_id) . unwrap ( ) . file_id ;
549- if self . cx . file_paths ( ) . get ( & file_id) . is_some ( ) {
550- let filename = self . file_name ( file_id) . unwrap ( ) . replace ( "." , "_" ) ;
551- let filename_lower = filename. to_lowercase ( ) ;
549+ let mut getter_impl = String :: new ( ) ;
550+ if let ItemExts :: Pb ( pb) = & s. item_exts {
551+ if let Some ( p) = & pb. parent {
552+ if self . cx . file_paths ( ) . get ( & file_id) . is_some ( ) {
553+ let path = self . cx . related_item_path ( def_id, p. did ) ;
554+ let name = self . cx . rust_name ( def_id) ;
555+ let idl_name = s. name . raw_str ( ) ;
556+ getter_impl = format ! (
557+ r#"
558+ impl {name} {{
559+ pub fn get_descriptor_proto() -> Option<&'static ::pilota::pb::descriptor::DescriptorProto> {{
560+ let message_descriptor = {path}::get_descriptor_proto()?;
561+ message_descriptor.get_message_descriptor_proto("{idl_name}")
562+ }}
563+ }}
564+ "#
565+ ) ;
566+ } ;
567+ } else if self . cx . file_paths ( ) . get ( & file_id) . is_some ( ) {
568+ let filename = self . file_name ( file_id) . unwrap ( ) . replace ( "." , "_" ) ;
569+ let filename_lower = filename. to_lowercase ( ) ;
552570
553- let file = & self . cx . files ( ) . get ( & file_id) . unwrap ( ) . package ;
554- let path = self . cx . item_path ( def_id) ;
555- let super_mods = "super::" . repeat ( path. len ( ) - file. len ( ) - 1 ) ;
571+ let file = & self . cx . files ( ) . get ( & file_id) . unwrap ( ) . package ;
572+ let path = self . cx . item_path ( def_id) ;
573+ let super_mods = "super::" . repeat ( path. len ( ) - file. len ( ) - 1 ) ;
556574
557- stream. push_str ( & format ! (
558- r#"
559- impl {name} {{
560- fn get_descriptor_proto() -> Option<&'static ::pilota::pb::descriptor::DescriptorProto> {{
561- let file_descriptor = {super_mods}file_descriptor_proto_{filename_lower}();
562- file_descriptor.get_message_descriptor_proto("{idl_name}")
563- }}
564- }}
565- "#
566- ) ) ;
575+ getter_impl = format ! (
576+ r#"
577+ impl {name} {{
578+ pub fn get_descriptor_proto() -> Option<&'static ::pilota::pb::descriptor::DescriptorProto> {{
579+ let file_descriptor = {super_mods}file_descriptor_proto_{filename_lower}();
580+ file_descriptor.get_message_descriptor_proto("{idl_name}")
581+ }}
582+ }}
583+ "#
584+ ) ;
585+ }
567586 }
587+ stream. push_str ( & getter_impl) ;
568588 }
569589
570590 stream. push_str ( & format ! (
@@ -612,6 +632,51 @@ impl CodegenBackend for ProtobufBackend {
612632 fn codegen_enum_impl ( & self , def_id : DefId , stream : & mut String , e : & rir:: Enum ) {
613633 let node = self . cx . node ( def_id) . unwrap ( ) ;
614634 if !self . cx . contains_tag :: < OneOf > ( node. tags ) {
635+ if self . cx . config . with_descriptor {
636+ let file_id = self . cx . node ( def_id) . unwrap ( ) . file_id ;
637+ let mut getter_impl = String :: new ( ) ;
638+
639+ if let ItemExts :: Pb ( pb) = & e. item_exts {
640+ if let Some ( p) = & pb. parent {
641+ if self . cx . file_paths ( ) . get ( & file_id) . is_some ( ) {
642+ let path = self . cx . related_item_path ( def_id, p. did ) ;
643+ let name = self . cx . rust_name ( def_id) ;
644+ let idl_name = e. name . raw_str ( ) ;
645+ getter_impl = format ! (
646+ r#"
647+ impl {name} {{
648+ pub fn get_descriptor_proto() -> Option<&'static ::pilota::pb::descriptor::EnumDescriptorProto> {{
649+ let message_descriptor = {path}::get_descriptor_proto()?;
650+ message_descriptor.get_enum_descriptor_proto("{idl_name}")
651+ }}
652+ }}
653+ "#
654+ ) ;
655+ } ;
656+ } else if self . cx . file_paths ( ) . get ( & file_id) . is_some ( ) {
657+ let name = self . cx . rust_name ( def_id) ;
658+ let idl_name = e. name . raw_str ( ) ;
659+ let filename = self . file_name ( file_id) . unwrap ( ) . replace ( "." , "_" ) ;
660+ let filename_lower = filename. to_lowercase ( ) ;
661+
662+ let file = & self . cx . files ( ) . get ( & file_id) . unwrap ( ) . package ;
663+ let path = self . cx . item_path ( def_id) ;
664+ let super_mods = "super::" . repeat ( path. len ( ) - file. len ( ) - 1 ) ;
665+
666+ getter_impl = format ! (
667+ r#"
668+ impl {name} {{
669+ pub fn get_descriptor_proto() -> Option<&'static ::pilota::pb::descriptor::EnumDescriptorProto> {{
670+ let file_descriptor = {super_mods}file_descriptor_proto_{filename_lower}();
671+ file_descriptor.get_enum_descriptor_proto("{idl_name}")
672+ }}
673+ }}
674+ "#
675+ ) ;
676+ }
677+ }
678+ stream. push_str ( & getter_impl) ;
679+ }
615680 return ;
616681 }
617682 let name = self . cx . rust_name ( def_id) ;
@@ -672,31 +737,25 @@ impl CodegenBackend for ProtobufBackend {
672737 } ) . join ( "" ) ;
673738
674739 let file_id = self . cx . node ( def_id) . unwrap ( ) . file_id ;
675- let mut getter_impl = match & e . oneof_parent {
676- Some ( ty ) => match & self . cx . file_paths ( ) . get ( & file_id ) {
677- Some ( _ ) => {
678- let path = self . codegen_item_ty ( ty . kind . clone ( ) ) ;
679- let path = format ! ( "{path}" ) ;
740+ let mut getter_impl = String :: new ( ) ;
741+ if let ItemExts :: Pb ( pb ) = & e . item_exts {
742+ if let Some ( p ) = & pb . parent {
743+ if self . cx . file_paths ( ) . get ( & file_id ) . is_some ( ) {
744+ let path = self . cx . related_item_path ( def_id , p . did ) ;
680745 let name = self . cx . rust_name ( def_id) ;
681- let idl_name = e. name . sym . 0 . clone ( ) ;
682- format ! (
746+ let idl_name = e. name . raw_str ( ) ;
747+ getter_impl = format ! (
683748 r#"
684749 impl {name} {{
685- fn get_descriptor_proto() -> Option<&'static ::pilota::pb::descriptor::OneofDescriptorProto> {{
750+ pub fn get_descriptor_proto() -> Option<&'static ::pilota::pb::descriptor::OneofDescriptorProto> {{
686751 let message_descriptor = {path}::get_descriptor_proto()?;
687752 message_descriptor.get_oneof_descriptor_proto("{idl_name}")
688753 }}
689754 }}
690755 "#
691- )
692- }
693- _ => "" . to_string ( ) ,
694- } ,
695- _ => "" . to_string ( ) ,
696- } ;
697-
698- if !self . config . with_descriptor {
699- getter_impl. clear ( ) ;
756+ ) ;
757+ } ;
758+ }
700759 }
701760
702761 stream. push_str ( & format ! {
@@ -736,47 +795,6 @@ impl CodegenBackend for ProtobufBackend {
736795 } ) ;
737796 }
738797
739- fn codegen_enum_descrptor_getter_impl (
740- & self ,
741- def_id : DefId ,
742- stream : & mut String ,
743- e : & rir:: Enum ,
744- ) {
745- if !self . cx . config . with_descriptor {
746- return ;
747- }
748-
749- let file_id = self . cx . node ( def_id) . unwrap ( ) . file_id ;
750-
751- let getter_impl = match self . cx . file_paths ( ) . get ( & file_id) {
752- Some ( _) => {
753- let name = self . cx . rust_name ( def_id) ;
754- let idl_name = e. name . sym . 0 . clone ( ) ;
755- let filename = self . file_name ( file_id) . unwrap ( ) . replace ( "." , "_" ) ;
756- let filename_lower = filename. to_lowercase ( ) ;
757-
758- let file = & self . cx . files ( ) . get ( & file_id) . unwrap ( ) . package ;
759- let path = self . cx . item_path ( def_id) ;
760- let super_mods = "super::" . repeat ( path. len ( ) - file. len ( ) - 1 ) ;
761-
762- format ! (
763- r#"
764-
765- impl {name} {{
766- fn get_descriptor_proto() -> Option<&'static ::pilota::pb::descriptor::EnumDescriptorProto> {{
767- let file_descriptor = {super_mods}file_descriptor_proto_{filename_lower}();
768- file_descriptor.get_enum_descriptor_proto("{idl_name}")
769- }}
770- }}
771-
772- "#
773- )
774- }
775- _ => "" . to_string ( ) ,
776- } ;
777- stream. push_str ( & getter_impl) ;
778- }
779-
780798 fn cx ( & self ) -> & Context {
781799 & self . cx
782800 }
0 commit comments