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.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
CompileConfig
Configuration struct for compiling protobuf files.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 directory for generated Rust files.All generated Rust code will be written to this directory.
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-specific configuration options.Controls generation of API-related helper functions and utilities.
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.Generate name helper functions.When set to
true, generates functions for getting the name of messages and enums, useful for debugging and logging.Generate field name helper functions.When set to
true, generates functions for getting the names of fields within messages, useful for reflection and debugging.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.Generate oneof utility functions.When set to
true, generates utility functions for working with oneof fields, including getters and setters.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.
Generate serde serialization support.When set to
true, generates Serialize and Deserialize implementations for all protobuf messages and enums.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.The protobuf type path pattern to match (e.g.,
.google.protobuf.Timestamp)The corresponding Rust type or mapping (e.g.,
chrono::DateTime<chrono::Utc>)get_first
Gets the first matching value for a given protobuf path.The protobuf type path to match against
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:
get_first_field
Gets the first matching value for a specific field within a type.The protobuf type path
The field name within the type
Option<(&str, &str)> - A tuple of (matcher, value) if a match is found, or None otherwise.
Example:
Helper Generation
Thebomboni_prost crate generates various helper functions based on the ApiConfig settings:
Name Helpers
Whennames is enabled, generates functions for getting message and enum names.
Field Name Helpers
Whenfield_names is enabled, generates constants for field names (e.g., FIELD_NAME_FIELD_NAME).
Type URL Helpers
Whentype_url is enabled, generates functions for getting type URLs compatible with google.protobuf.Any.
Oneof Utilities
Whenoneof_utility is enabled, generates utility functions for working with oneof fields.
Serde Support
Whenserde is enabled, generates serde helper modules for enum serialization (e.g., enum_name_serde).