Skip to main content

CodeGenContext

Struct CodeGenContext 

Source
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 CodeGenConfig

Code 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>

Source

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.

File-level extern resolution (currently only descriptor.proto / compiler/plugin.proto → buffa-descriptor) is not applied by this constructor — use for_generate, which computes and passes the file-level mappings, when generating code that may reference descriptor types.

This constructor plans keyword-escaped member names over every file in files. for_generate plans them over the generated files only.

Source

pub fn nested_module_name(&self, package: &str, name: &str) -> String

Returns the name of the module that holds the nested types of a top-level message.

package is the proto package (empty for none), name the message’s proto name. The module is snake_case(name), with _ appended until the name is free when it is taken in the package: oof_ when message Oof is beside package <pkg>.oof (issue #135), item_ for message item, whose struct has the module’s name, and crate_, arbitrary_ and oneof_ for message Crate, message Arbitrary and message Oneof. Both emission and reference resolution use this value, so they agree.

Returns snake_case(name) for a name that is not a top-level message of package, and for a message that has a per-type extern_path entry.

Source

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 + auto-injected descriptor.proto / compiler/plugin.proto file-level mapping to buffa-descriptor) 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.

Source

pub fn rust_type(&self, proto_fqn: &str) -> Option<&str>

Look up the Rust type path for a fully-qualified protobuf type name.

Source

pub fn comment(&self, fqn: &str) -> Option<&str>

Look up the source comment for a protobuf element by FQN.

fqn uses the same dotted form as proto_fqn throughout codegen (no leading dot). For sub-elements, append the element name:

  • Message: "pkg.Message"
  • Field: "pkg.Message.field_name"
  • Enum value: "pkg.Enum.VALUE_NAME"
  • Oneof: "pkg.Message.oneof_name"
Source

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).

Source

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.
Source

pub fn rust_type_relative_split( &self, proto_fqn: &str, current_package: &str, nesting: usize, ) -> Option<SplitPath>

Like rust_type_relative but returns the path split at the target-package boundary.

Ancillary kinds (views, oneof enums) live in the __buffa::<kind>:: sub-tree of each package; callers compose the final path as to_package + "::__buffa::" + <kind> + "::" + within_package.

nesting is the total module depth of the caller’s emission scope below the current package root — i.e. message-nesting plus any __buffa::<kind>:: levels the caller is already inside (0 for owned types, +2 for __buffa::view::, +3 for __buffa::view::oneof::).

Source

pub fn bytes_repr(&self, field_fqn: &str) -> BytesRepr

Resolve the BytesRepr for a bytes field at the given proto path.

field_fqn is the fully-qualified proto field path, e.g., ".my.pkg.MyMessage.data". Rules in config.bytes_fields are matched using proto-segment-aware prefix matching ("." matches all, ".my.pkg" matches ".my.pkg.Msg.data" but not ".my.pkgs.X.data"); the last matching rule wins, letting a specific override follow a broad default. Fields matching no rule use BytesRepr::Vec.

Source

pub fn preserve_unknown_fields(&self, msg_fqn: &str) -> bool

Whether this message stores unknown fields.

Starts from CodeGenConfig::preserve_unknown_fields and applies CodeGenConfig::preserve_unknown_fields_in; the last matching rule wins. Rules use proto-segment-aware prefix matching, so a rule naming a message also covers the messages nested inside it, while a rule naming a nested message does not cover its enclosing message. msg_fqn is the message’s proto path, with or without a leading dot ("pkg.Msg" or ".pkg.Msg").

Source

pub fn deny_unknown_json_fields(&self, msg_fqn: &str) -> bool

Whether this message’s generated JSON deserializer rejects unknown fields.

Starts from CodeGenConfig::deny_unknown_json_fields and applies CodeGenConfig::deny_unknown_json_fields_in; the last matching rule wins. Matching follows the same proto-segment-aware prefix rules as preserve_unknown_fields, so a rule naming a message also covers the messages nested inside it, while a rule naming a nested message does not cover its enclosing message. msg_fqn is the message’s proto path, with or without a leading dot.

Source

pub fn skip_debug(&self, message_fqn: &str) -> bool

Whether a skip_debug rule covers the message at message_fqn. Matched owned messages and their oneofs omit the generated Debug impl; matched views redact their output.

Rules use the same proto-segment-aware prefix matching as preserve_unknown_fields: a rule naming a message also covers the messages nested inside it, and "." matches every message. Enums are matched by skip_debug_enum.

Source

pub fn skip_debug_enum(&self, enum_fqn: &str) -> bool

Whether a skip_debug rule names the enum at enum_fqn, so that its derived Debug is omitted.

The match is exact. A rule for an enclosing message or package leaves the enum’s derive in place, because buffa::Enumeration requires Debug and an enum without it compiles only once the consuming crate implements it.

Source

pub fn oneof_unboxed(&self, variant_fqn: &str) -> bool

Check whether a message-typed oneof variant at the given proto path is stored inline (opted out of Box wrapping).

variant_fqn is the fully-qualified variant path, e.g. ".my.pkg.MyMessage.body.small". This consults the set resolved at context construction by the internal resolve_unboxed_variants pass, not the raw config rules: recursive variants matched only by a prefix rule are excluded there (they stay boxed), so every codegen site that asks agrees with the emitted enum declaration.

Source

pub fn string_repr(&self, field_fqn: &str) -> StringRepr

Resolve the StringRepr for a string field at the given proto path.

field_fqn is the fully-qualified proto field path, e.g. ".my.pkg.MyMessage.name". Rules in config.string_fields are matched with the same proto-segment-aware prefix logic as bytes_repr; the last matching rule wins, letting a specific override follow a broad default. Fields matching no rule use StringRepr::String.

Source

pub fn map_repr(&self, field_fqn: &str) -> MapRepr

Resolve the MapRepr for a map field at the given proto path.

field_fqn is the fully-qualified proto field path, e.g. ".my.pkg.MyMessage.entries". Rules in config.map_fields are matched with the same proto-segment-aware prefix logic as string_repr; the last matching rule wins, letting a specific override follow a broad default. Fields matching no rule use MapRepr::HashMap.

Source

pub fn pointer_repr(&self, field_fqn: &str) -> PointerRepr

Resolve the PointerRepr for a singular message field at the given proto path. Last matching rule wins (proto-segment prefix match); fields matching no rule use PointerRepr::Box.

Inline is recursion-aware: a path whose raw rule resolves to PointerRepr::Inline but is absent from the precomputed inlined_message_fields set (a recursive singular field, or a non-singular path such as a oneof variant) is demoted to Box so the generated type stays sized.

Source

pub fn repeated_repr(&self, field_fqn: &str) -> RepeatedRepr

Resolve the RepeatedRepr for a repeated field at the given proto path.

field_fqn is the fully-qualified proto field path, e.g. ".my.pkg.MyMessage.items". Rules in config.repeated_fields are matched with the same proto-segment-aware prefix logic as string_repr; the last matching rule wins, letting a specific override follow a broad default. Fields matching no rule use RepeatedRepr::Vec.

Auto Trait Implementations§

§

impl<'a> !Freeze for CodeGenContext<'a>

§

impl<'a> !RefUnwindSafe for CodeGenContext<'a>

§

impl<'a> !Sync for CodeGenContext<'a>

§

impl<'a> Forget for CodeGenContext<'a>

§

impl<'a> Move for CodeGenContext<'a>

§

impl<'a> Send for CodeGenContext<'a>

§

impl<'a> Unpin for CodeGenContext<'a>

§

impl<'a> UnsafeUnpin for CodeGenContext<'a>

§

impl<'a> UnwindSafe for CodeGenContext<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.