@@ -52,6 +52,8 @@ pub struct Config {
5252 pub ( crate ) skip_source_info : bool ,
5353 pub ( crate ) include_file : Option < PathBuf > ,
5454 pub ( crate ) prost_path : Option < String > ,
55+ pub ( crate ) type_name_prefixes : PathMap < String > ,
56+ pub ( crate ) type_name_suffixes : PathMap < String > ,
5557 #[ cfg( feature = "format" ) ]
5658 pub ( crate ) fmt : bool ,
5759}
@@ -353,6 +355,134 @@ impl Config {
353355 self
354356 }
355357
358+ /// Add a suffix to all generated type names.
359+ ///
360+ /// # Arguments
361+ ///
362+ /// **`suffix`** - an arbitrary string to be appended to all type names. For example,
363+ /// "Proto" would change `MyMessage` to `MyMessageProto`.
364+ ///
365+ /// # Examples
366+ ///
367+ /// ```rust
368+ /// # let mut config = prost_build::Config::new();
369+ /// // Add "Proto" suffix to all generated types
370+ /// config.type_name_suffix("Proto");
371+ /// ```
372+ pub fn type_name_suffix < S > ( & mut self , suffix : S ) -> & mut Self
373+ where
374+ S : AsRef < str > ,
375+ {
376+ self . type_name_suffixes
377+ . insert ( "." . to_string ( ) , suffix. as_ref ( ) . to_string ( ) ) ;
378+ self
379+ }
380+
381+ /// Add a prefix to all generated type names.
382+ ///
383+ /// # Arguments
384+ ///
385+ /// **`prefix`** - an arbitrary string to be prepended to all type names. For example,
386+ /// "Proto" would change `MyMessage` to `ProtoMyMessage`.
387+ ///
388+ /// # Examples
389+ ///
390+ /// ```rust
391+ /// # let mut config = prost_build::Config::new();
392+ /// // Add "Proto" prefix to all generated types
393+ /// config.type_name_prefix("Proto");
394+ /// ```
395+ pub fn type_name_prefix < P > ( & mut self , prefix : P ) -> & mut Self
396+ where
397+ P : AsRef < str > ,
398+ {
399+ self . type_name_prefixes
400+ . insert ( "." . to_string ( ) , prefix. as_ref ( ) . to_string ( ) ) ;
401+ self
402+ }
403+
404+ /// Configure package-specific type name suffixes.
405+ ///
406+ /// This allows different suffix settings for different proto packages,
407+ /// which is especially useful for cross-crate compatibility when importing
408+ /// proto files from external crates.
409+ ///
410+ /// # Arguments
411+ ///
412+ /// **`paths`** - package paths with their desired suffix. Paths starting with '.'
413+ /// are treated as fully-qualified package names. Paths without a leading '.' are
414+ /// treated as relative and suffix-matched.
415+ ///
416+ /// # Examples
417+ ///
418+ /// ```rust
419+ /// # let mut config = prost_build::Config::new();
420+ /// // Apply "Proto" suffix to a specific package
421+ /// config.package_type_name_suffix([(".my_package", "Proto")]);
422+ ///
423+ /// // Apply different suffixes to different packages
424+ /// config.package_type_name_suffix([
425+ /// (".external_api", "External"), // external API types
426+ /// (".internal", "Internal"), // internal types
427+ /// ]);
428+ ///
429+ /// // Apply suffix to all packages under a namespace
430+ /// config.package_type_name_suffix([("my_company", "Pb")]);
431+ /// ```
432+ pub fn package_type_name_suffix < I , P , S > ( & mut self , paths : I ) -> & mut Self
433+ where
434+ I : IntoIterator < Item = ( P , S ) > ,
435+ P : AsRef < str > ,
436+ S : AsRef < str > ,
437+ {
438+ for ( path, suffix) in paths {
439+ self . type_name_suffixes
440+ . insert ( path. as_ref ( ) . to_string ( ) , suffix. as_ref ( ) . to_string ( ) ) ;
441+ }
442+ self
443+ }
444+
445+ /// Configure package-specific type name prefixes.
446+ ///
447+ /// This allows different prefix settings for different proto packages,
448+ /// which is especially useful for cross-crate compatibility when importing
449+ /// proto files from external crates.
450+ ///
451+ /// # Arguments
452+ ///
453+ /// **`paths`** - package paths with their desired prefix. Paths starting with '.'
454+ /// are treated as fully-qualified package names. Paths without a leading '.' are
455+ /// treated as relative and suffix-matched.
456+ ///
457+ /// # Examples
458+ ///
459+ /// ```rust
460+ /// # let mut config = prost_build::Config::new();
461+ /// // Apply "Proto" prefix to a specific package
462+ /// config.package_type_name_prefix([(".my_package", "Proto")]);
463+ ///
464+ /// // Apply different prefixes to different packages
465+ /// config.package_type_name_prefix([
466+ /// (".external_api", "External"), // external API types
467+ /// (".internal", "Internal"), // internal types
468+ /// ]);
469+ ///
470+ /// // Apply prefix to all packages under a namespace
471+ /// config.package_type_name_prefix([("my_company", "Pb")]);
472+ /// ```
473+ pub fn package_type_name_prefix < I , P , S > ( & mut self , paths : I ) -> & mut Self
474+ where
475+ I : IntoIterator < Item = ( P , S ) > ,
476+ P : AsRef < str > ,
477+ S : AsRef < str > ,
478+ {
479+ for ( path, prefix) in paths {
480+ self . type_name_prefixes
481+ . insert ( path. as_ref ( ) . to_string ( ) , prefix. as_ref ( ) . to_string ( ) ) ;
482+ }
483+ self
484+ }
485+
356486 /// Wrap matched fields in a `Box`.
357487 ///
358488 /// # Arguments
@@ -1193,6 +1323,8 @@ impl default::Default for Config {
11931323 skip_source_info : false ,
11941324 include_file : None ,
11951325 prost_path : None ,
1326+ type_name_prefixes : PathMap :: default ( ) ,
1327+ type_name_suffixes : PathMap :: default ( ) ,
11961328 #[ cfg( feature = "format" ) ]
11971329 fmt : true ,
11981330 }
@@ -1219,6 +1351,8 @@ impl fmt::Debug for Config {
12191351 . field ( "disable_comments" , & self . disable_comments )
12201352 . field ( "skip_debug" , & self . skip_debug )
12211353 . field ( "prost_path" , & self . prost_path )
1354+ . field ( "type_name_prefixes" , & self . type_name_prefixes )
1355+ . field ( "type_name_suffixes" , & self . type_name_suffixes )
12221356 . finish ( )
12231357 }
12241358}
0 commit comments