Skip to main content
The bomboni_prost crate provides advanced protobuf code generation capabilities, building on top of prost with additional helper functions and utilities for working with protobuf messages and enums.

compile

Compiles protobuf files using the provided configuration. This function reads the file descriptor set, processes all protobuf files, and generates Rust code with additional helper functions and utilities.
config
CompileConfig
required
Configuration for the compilation process

Returns

  • Success: Ok(()) when compilation completes successfully
  • Error: Returns Box<dyn Error> if:
    • The file descriptor set cannot be read
    • Protobuf compilation fails
    • File I/O operations fail
    • Code generation encounters invalid protobuf definitions

Example

use bomboni_prost::{compile, config::CompileConfig, ApiConfig, path_map::PathMap};

let config = CompileConfig {
    file_descriptor_set_path: "descriptor.bin".into(),
    output_path: "src/generated".into(),
    format: true,
    api: ApiConfig::default(),
    external_paths: PathMap::default(),
};

compile(config)?;

CompileConfig

Configuration struct for compiling protobuf files.
file_descriptor_set_path
PathBuf
required
Path to the file descriptor set binary file.This file contains the compiled protobuf descriptors and is typically generated by protoc --descriptor_set_out.
output_path
PathBuf
required
Output directory for generated Rust files.All generated Rust code will be written to this directory.
format
bool
required
Whether to format the generated code.When set to true, the generated Rust code will be formatted using rustfmt before being written to files.
api
ApiConfig
required
API-specific configuration options.Controls generation of API-related helper functions and utilities.
external_paths
PathMap
required
External path mappings for custom type handling.Maps protobuf type paths to custom Rust types for integration with external libraries or custom implementations.

Default Configuration

The default configuration uses:
  • File descriptor set path: $OUT_DIR/fd.pb
  • Output path: $OUT_DIR
  • Format: true
  • API config: Default ApiConfig
  • External paths: Empty PathMap

ApiConfig

Configuration for API-specific code generation.
names
bool
default:"true"
Generate name helper functions.When set to true, generates functions for getting the name of messages and enums, useful for debugging and logging.
field_names
bool
default:"true"
Generate field name helper functions.When set to true, generates functions for getting the names of fields within messages, useful for reflection and debugging.
type_url
bool
default:"true"
Generate type URL helper functions.When set to true, generates functions for getting the type URL of messages, useful for Google APIs and Any type handling.
oneof_utility
bool
default:"true"
Generate oneof utility functions.When set to true, generates utility functions for working with oneof fields, including getters and setters.
domain
Option<String>
default:"None"
Optional domain prefix for generated types.When specified, adds the given domain prefix to generated type names, useful for avoiding name conflicts in multi-domain APIs.
serde
bool
default:"true"
Generate serde serialization support.When set to true, generates Serialize and Deserialize implementations for all protobuf messages and enums.
helpers_mod
Option<String>
default:"None"
Optional name for the helpers module.When specified, creates a module with the given name containing all generated helper functions. If None, helpers are placed in the same module as the message.

PathMap

Maps fully-qualified protobuf type names to Rust types or paths.

Methods

insert

Inserts a new matcher-value pair into the path map.
matcher
impl ToString
required
The protobuf type path pattern to match (e.g., .google.protobuf.Timestamp)
value
impl ToString
required
The corresponding Rust type or mapping (e.g., chrono::DateTime<chrono::Utc>)
Example:
use bomboni_prost::path_map::PathMap;

let mut map = PathMap::default();
map.insert(".google.protobuf.Timestamp", "chrono::DateTime<chrono::Utc>");
map.insert(".myapi.User", "crate::models::User");

get_first

Gets the first matching value for a given protobuf path.
path
&str
required
The protobuf type path to match against
Returns: Option<(&str, &str)> - A tuple of (matcher, value) if a match is found, or None otherwise. Searches for the most specific matcher that matches the given path. Exact matches take precedence over prefix matches. Example:
let map = PathMap::from([(".a", "A"), (".a.b", "B")]);
assert_eq!(map.get_first(".a").unwrap().1, "A");  // Exact match
assert_eq!(map.get_first(".a.b").unwrap().1, "B");  // More specific
assert_eq!(map.get_first(".a.c").unwrap().1, "A");  // Prefix match

get_first_field

Gets the first matching value for a specific field within a type.
path
&str
required
The protobuf type path
field
&str
required
The field name within the type
Returns: Option<(&str, &str)> - A tuple of (matcher, value) if a match is found, or None otherwise. Example:
let map = PathMap::from([(".myapi.User", "crate::User")]);
assert_eq!(map.get_first_field(".myapi.User", "name").unwrap().1, "crate::User");

Helper Generation

The bomboni_prost crate generates various helper functions based on the ApiConfig settings:

Name Helpers

When names is enabled, generates functions for getting message and enum names.

Field Name Helpers

When field_names is enabled, generates constants for field names (e.g., FIELD_NAME_FIELD_NAME).

Type URL Helpers

When type_url is enabled, generates functions for getting type URLs compatible with google.protobuf.Any.

Oneof Utilities

When oneof_utility is enabled, generates utility functions for working with oneof fields.

Serde Support

When serde is enabled, generates serde helper modules for enum serialization (e.g., enum_name_serde).

Build docs developers (and LLMs) love