pub struct CodeGenContext<'a> {
pub files: &'a [FileDescriptorProto],
pub config: &'a CodeGenConfig,
pub type_map: HashMap<String, String>,
/* private fields */
}Expand description
Shared context for a code generation run.
Holds the full set of file descriptors and a mapping from fully-qualified
protobuf type names to their Rust type paths. This is needed because a
field in one .proto file may reference a message defined in another.
Fields§
§files: &'a [FileDescriptorProto]All file descriptors (both requested and dependencies).
config: &'a CodeGenConfigCode generation configuration.
type_map: HashMap<String, String>Map from fully-qualified protobuf name (e.g., “.my.package.MyMessage”) to Rust type path (e.g., “my::package::MyMessage”).
Nested types use module-qualified paths: “.pkg.Outer.Inner” → “pkg::outer::Inner” (not “pkg::OuterInner”).
Implementations§
Source§impl<'a> CodeGenContext<'a>
impl<'a> CodeGenContext<'a>
Sourcepub fn new(
files: &'a [FileDescriptorProto],
config: &'a CodeGenConfig,
effective_extern_paths: &[(String, String)],
) -> Self
pub fn new( files: &'a [FileDescriptorProto], config: &'a CodeGenConfig, effective_extern_paths: &[(String, String)], ) -> Self
Build a context from file descriptors, populating the type map.
effective_extern_paths includes both user-provided mappings and any
auto-injected defaults (e.g., the WKT mapping). These are computed by
crate::effective_extern_paths before calling this constructor.
Sourcepub fn for_generate(
files: &'a [FileDescriptorProto],
files_to_generate: &[String],
config: &'a CodeGenConfig,
) -> Self
pub fn for_generate( files: &'a [FileDescriptorProto], files_to_generate: &[String], config: &'a CodeGenConfig, ) -> Self
Build a context matching what generate() uses
internally.
Computes effective extern paths (user-provided + auto-injected WKT
mapping to buffa-types) and builds the type map from them.
Convenience for downstream generators (e.g. connectrpc-codegen)
that emit code alongside buffa’s message types and need identical
type-path resolution. Using this instead of new() +
manual extern-path computation ensures zero drift with buffa’s own
generation.
Sourcepub fn rust_type(&self, proto_fqn: &str) -> Option<&str>
pub fn rust_type(&self, proto_fqn: &str) -> Option<&str>
Look up the Rust type path for a fully-qualified protobuf type name.
Sourcepub fn is_enum_closed(&self, proto_fqn: &str) -> Option<bool>
pub fn is_enum_closed(&self, proto_fqn: &str) -> Option<bool>
Look up whether an enum (by fully-qualified proto name) is closed.
Returns None if the enum is not in this compilation set (e.g., an
extern_path type), in which case callers should fall back to the
referencing field’s feature chain (correct for proto2/proto3 where
enum_type is file-level anyway).
Sourcepub fn rust_type_relative(
&self,
proto_fqn: &str,
current_package: &str,
nesting: usize,
) -> Option<String>
pub fn rust_type_relative( &self, proto_fqn: &str, current_package: &str, nesting: usize, ) -> Option<String>
Look up the Rust type path relative to the current code generation scope.
current_package is the proto package (e.g., "google.protobuf").
nesting is the number of message module levels the generated code
sits inside (0 for struct fields and impls at the package level,
1 for oneof enums inside a message module, etc.).
- Same package: strips the package prefix and prepends
super::for each nesting level. - Cross package (local): navigates via
super::to the common ancestor, then descends into the target package. This works regardless of where the module tree is placed in the user’s crate. - Cross package (extern): returns the absolute extern path as-is.
Sourcepub fn use_bytes_type(&self, field_fqn: &str) -> bool
pub fn use_bytes_type(&self, field_fqn: &str) -> bool
Check whether a bytes field at the given proto path should use
bytes::Bytes instead of Vec<u8>.
field_fqn is the fully-qualified proto field path, e.g.,
".my.pkg.MyMessage.data". Matches against config.bytes_fields
entries, which are prefix-matched (so "." matches all fields).