@@ -886,48 +886,37 @@ impl Generator {
886886 self
887887 }
888888
889+ #[ cfg( feature = "config-file" ) ]
890+ pub ( crate ) fn parse_config_from_proto (
891+ & mut self ,
892+ proto_path : & Path ,
893+ pkg : Option < & String > ,
894+ ) -> Result < ( ) > {
895+ let toml_path = proto_path. with_extension ( "toml" ) ;
896+ let pkg_path = match pkg {
897+ Some ( pkg) => format ! ( ".{pkg}" ) ,
898+ None => "." . to_owned ( ) ,
899+ } ;
900+ match self . parse_config_file ( & toml_path, & pkg_path) {
901+ // If config file doesn't exist then just assume there's no configs
902+ Err ( Error :: Io ( err) ) if err. kind ( ) == std:: io:: ErrorKind :: NotFound => Ok ( ( ) ) ,
903+ Err ( err) => Err ( err) ,
904+ Ok ( ( ) ) => Ok ( ( ) ) ,
905+ }
906+ }
907+
889908 /// Compile `.proto` files and configuration files into a single Rust file.
890909 ///
891910 /// Configuration files are derived from the proto files by replacing the file extension with
892911 /// `.toml`. For example, if `server.proto` is passed in, the generator will look for the
893- /// `server.toml` config file and apply it to the `.server` package.
894- ///
895- /// <div class="warning">
896- /// The package name of each proto file must match the file name, because that's what the
897- /// generator assumes when applying the config files. For example, `client.rpc.proto` must
898- /// contain the specifier `package client.rpc;`.
899- /// </div>
912+ /// `server.toml` config file and apply it to the package specified in `server.proto`.
900913 #[ cfg( feature = "config-file" ) ]
901914 pub fn compile_protos_with_config_files (
902- mut self ,
915+ self ,
903916 protos : & [ impl AsRef < Path > ] ,
904917 out_filename : impl AsRef < Path > ,
905918 ) -> Result < ( ) > {
906- for proto_path in protos {
907- let proto_path = proto_path. as_ref ( ) ;
908- if let Some ( stem) = proto_path. file_stem ( ) . and_then ( OsStr :: to_str) {
909- let pkg = if stem. starts_with ( '.' ) {
910- stem. to_owned ( )
911- } else {
912- format ! ( ".{stem}" )
913- } ;
914- let toml_path = proto_path. with_extension ( "toml" ) ;
915-
916- match self . parse_config_file ( & toml_path, & pkg) {
917- // If config file doesn't exist then just assume there's no configs
918- Err ( Error :: Io ( err) ) if err. kind ( ) == std:: io:: ErrorKind :: NotFound => { }
919- Err ( err) => return Err ( err) ,
920- Ok ( ( ) ) => { }
921- }
922- } else {
923- ( self . warning_cb ) ( format_args ! (
924- "Couldn't derive config file path from {}" ,
925- proto_path. display( )
926- ) ) ;
927- }
928- }
929-
930- self . compile_protos ( protos, out_filename)
919+ self . compile_protos_inner ( protos, out_filename. as_ref ( ) , true )
931920 }
932921
933922 /// Compile `.proto` files into a single Rust file.
@@ -945,6 +934,15 @@ impl Generator {
945934 self ,
946935 protos : & [ impl AsRef < Path > ] ,
947936 out_filename : impl AsRef < Path > ,
937+ ) -> Result < ( ) > {
938+ self . compile_protos_inner ( protos, out_filename. as_ref ( ) , false )
939+ }
940+
941+ fn compile_protos_inner (
942+ self ,
943+ protos : & [ impl AsRef < Path > ] ,
944+ out_filename : & Path ,
945+ find_config_files : bool ,
948946 ) -> Result < ( ) > {
949947 let tmp;
950948 let fdset_file = if let Some ( fdset_path) = & self . fdset_path {
@@ -978,7 +976,7 @@ impl Generator {
978976 ) ) ;
979977 }
980978
981- self . compile_fdset_file ( fdset_file, out_filename)
979+ self . compile_fdset_file_inner ( fdset_file. as_path ( ) , out_filename, find_config_files )
982980 }
983981
984982 /// Compile a Protobuf file descriptor set into a Rust file.
@@ -989,6 +987,15 @@ impl Generator {
989987 self ,
990988 fdset_file : impl AsRef < Path > ,
991989 out_filename : impl AsRef < Path > ,
990+ ) -> crate :: Result < ( ) > {
991+ self . compile_fdset_file_inner ( fdset_file. as_ref ( ) , out_filename. as_ref ( ) , false )
992+ }
993+
994+ fn compile_fdset_file_inner (
995+ self ,
996+ fdset_file : & Path ,
997+ out_filename : & Path ,
998+ find_config_files : bool ,
992999 ) -> crate :: Result < ( ) > {
9931000 #[ allow( unused) ]
9941001 let format = self . format ;
@@ -999,7 +1006,7 @@ impl Generator {
9991006 fdset
10001007 . decode ( & mut decoder, bytes. len ( ) )
10011008 . expect ( "file descriptor set decode failed" ) ;
1002- let code = Context :: generate_fdset ( self , & fdset) ?;
1009+ let code = Context :: generate_fdset ( self , & fdset, find_config_files ) ?;
10031010
10041011 #[ cfg( feature = "format" ) ]
10051012 let output = if format {
0 commit comments