Skip to main content

CodeGenConfig

Struct CodeGenConfig 

Source
#[non_exhaustive]
pub struct CodeGenConfig {
Show 14 fields pub generate_views: bool, pub preserve_unknown_fields: bool, pub generate_json: bool, pub generate_arbitrary: bool, pub extern_paths: Vec<(String, String)>, pub bytes_fields: Vec<String>, pub strict_utf8_mapping: bool, pub allow_message_set: bool, pub generate_text: bool, pub emit_register_fn: bool, pub type_attributes: Vec<(String, String)>, pub field_attributes: Vec<(String, String)>, pub message_attributes: Vec<(String, String)>, pub enum_attributes: Vec<(String, String)>,
}
Expand description

Configuration for code generation.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§generate_views: bool

Whether to generate borrowed view types (MyMessageView<'a>) in addition to owned types.

§preserve_unknown_fields: bool

Whether to preserve unknown fields (default: true).

§generate_json: bool

Whether to derive serde::Serialize / serde::Deserialize on generated message structs and enum types, and emit #[serde(with = "...")] attributes for proto3 JSON’s special scalar encodings (int64 as quoted string, bytes as base64, etc.).

When this is true, the downstream crate must depend on serde and must enable the buffa/json feature for the runtime helpers.

Oneof fields use #[serde(flatten)] with custom Serialize / Deserialize impls so that each variant appears as a top-level JSON field (proto3 JSON inline oneof encoding).

§generate_arbitrary: bool

Whether to emit #[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] on generated message structs and enum types.

When this is true, the downstream crate must add arbitrary as an optional dependency and enable the buffa/arbitrary feature.

§extern_paths: Vec<(String, String)>

External type path mappings.

Each entry maps a fully-qualified protobuf path prefix (e.g., ".my.common") to a Rust module path (e.g., "::common_protos"). Types under the proto prefix will reference the extern Rust path instead of being generated, allowing shared proto packages to be compiled once in a dedicated crate and referenced from others.

Well-known types (google.protobuf.*) are automatically mapped to ::buffa_types::google::protobuf::* without needing an explicit entry here. To override with a custom implementation, add an extern_path for .google.protobuf pointing to your crate.

§bytes_fields: Vec<String>

Fully-qualified proto field paths whose bytes fields should use bytes::Bytes instead of Vec<u8>.

Each entry is a proto path prefix (e.g., ".my.pkg.MyMessage.data" for a specific field, or "." for all bytes fields). The path is matched as a prefix, so "." applies to every bytes field in every message.

§strict_utf8_mapping: bool

Honor features.utf8_validation = NONE by emitting Vec<u8> / &[u8] for such string fields instead of String / &str.

When false (the default), buffa emits String for all string fields and validates UTF-8 on decode — stricter than proto2 requires, but ergonomic and safe.

When true, string fields with utf8_validation = NONE (all proto2 strings by default, and editions fields that opt into NONE) become Vec<u8> / &[u8]. Decode skips validation; the caller decides at the call site whether to std::str::from_utf8 (checked) or from_utf8_unchecked (trusted-input fast path). This is the only sound Rust mapping when strings may actually contain non-UTF-8 bytes.

This is a breaking change for proto2 — enable only for new code or when profiling identifies UTF-8 validation as a bottleneck.

§allow_message_set: bool

Permit option message_set_wire_format = true on input messages.

MessageSet is a legacy Google-internal wire format that wraps each extension in a group structure instead of using regular field tags. When false (the default), encountering such a message is a codegen error — the flag exists to make MessageSet use explicit, since the format is obsolete outside of interop with very old Google protos.

§generate_text: bool

Whether to emit impl buffa::text::TextFormat on generated message structs for textproto (human-readable text format) encoding/decoding.

When this is true, the downstream crate must enable the buffa/text feature for the runtime encoder/decoder.

§emit_register_fn: bool

Whether the per-package .mod.rs stitcher emits __buffa::register_types(&mut TypeRegistry).

Default true. The fn aggregates Any type entries and extension entries for every message in the package. Set to false for crates that don’t use extensions/Any, or that hand-roll registration (e.g. buffa-typesregister_wkt_types, which knows the JSON-Any is_wkt special-casing the generic fn does not). The per-message __*_JSON_ANY / __*_TEXT_ANY consts are still emitted; only the aggregating fn is suppressed.

§type_attributes: Vec<(String, String)>

Custom attributes to inject on generated types (messages and enums).

Each entry is (proto_path, attribute). The proto_path is matched as a prefix against the fully-qualified proto name: "." applies to all types, ".my.pkg" to types in that package, ".my.pkg.MyMessage" to a specific type. The attribute is a raw Rust attribute string (e.g., "#[derive(serde::Serialize)]").

§field_attributes: Vec<(String, String)>

Custom attributes to inject on generated struct fields.

Each entry is (proto_path, attribute). The proto_path is matched as a prefix against the fully-qualified field path (e.g., ".my.pkg.MyMessage.my_field"). "." applies to all fields.

§message_attributes: Vec<(String, String)>

Custom attributes to inject on generated message structs only (not enums).

Same path-matching semantics as type_attributes, but only applied to message structs, not enum types. Useful for struct-only attributes like #[serde(default)].

§enum_attributes: Vec<(String, String)>

Custom attributes to inject on generated enum types only (not messages).

Same path-matching semantics as type_attributes, but only applied to enum types. Useful for enum-only attributes like #[derive(strum::EnumIter)] when the user does not want to apply the same attribute to every message in the matched scope.

Trait Implementations§

Source§

impl Clone for CodeGenConfig

Source§

fn clone(&self) -> CodeGenConfig

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CodeGenConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for CodeGenConfig

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.