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.

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

Auto Trait Implementations§

§

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

§

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

§

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

§

impl<'a> Sync 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.