micropb_gen/
lib.rs

1#![warn(missing_docs)]
2//! `micropb-gen` compiles `.proto` files into Rust code. It is intended to be used inside
3//! `build.rs` for build-time code generation.
4//!
5//! Unlike other Protobuf code generators in the Rust ecosystem, `micropb` is aimed for constrained
6//! environments without an allocator.
7//!
8//! The entry point of this crate is the [`Generator`] type.
9//!
10//! For info on the "library layer" of `micropb-gen`, see [`micropb`].
11//!
12//! # Getting Started
13//!
14//! Add `micropb` crates to your `Cargo.toml`:
15//! ```protobuf
16//! [dependencies]
17//! # Allow types from `heapless` to be used for container fields
18//! micropb = { version = "0.3.0", features = ["container-heapless"] }
19//!
20//! [build-dependencies]
21//! micropb-gen = "0.3.0"
22//! ```
23//!
24//! Then, place your `.proto` file into the project's root directory:
25//! ```proto
26//! // example.proto
27//! message Example {
28//!     int32 field1 = 1;
29//!     bool field2 = 2;
30//!     double field3 = 3;
31//! }
32//! ```
33//!
34//! `micropb-gen` requires `protoc` to build `.proto` files, so [install
35//! `protoc`](https://grpc.io/docs/protoc-installation) and add it to your PATH, then invoke the
36//! code generator in `build.rs`:
37//!
38//! ```rust,no_run
39//! let mut gen = micropb_gen::Generator::new();
40//! // Compile example.proto into a Rust module
41//! gen.compile_protos(&["example.proto"], std::env::var("OUT_DIR").unwrap() + "/example.rs").unwrap();
42//! ```
43//!
44//! Finally, include the generated file in your code:
45//! ```rust,ignore
46//! // main.rs
47//! use micropb::{MessageDecode, MessageEncode, PbEncoder};
48//!
49//! mod example {
50//!     #![allow(clippy::all)]
51//!     #![allow(nonstandard_style, unused, irrefutable_let_patterns)]
52//!     // Let's assume that Example is the only message define in the .proto file that has been
53//!     // converted into a Rust struct
54//!     include!(concat!(env!("OUT_DIR"), "/example.rs"));
55//! }
56//!
57//! let example = example::Example {
58//!     field1: 12,
59//!     field2: true,
60//!     field3: 0.234,
61//! };
62//!
63//! // Maximum size of the message type on the wire, scaled to the next power of 2 for heapless::Vec
64//! const CAPACITY: usize = example::Example::MAX_SIZE.unwrap().next_power_of_two();
65//! // For the example message above we can use a smaller capacity
66//! // const CAPACITY: usize = 32;
67//!
68//! // Use heapless::Vec as the output stream and build an encoder around it
69//! let mut encoder = PbEncoder::new(micropb::heapless::Vec::<u8, CAPACITY>::new());
70//!
71//! // Compute the size of the `Example` on the wire
72//! let _size = example.compute_size();
73//! // Encode the `Example` to the data stream
74//! example.encode(&mut encoder).expect("Vec over capacity");
75//!
76//! // Decode a new instance of `Example` into a new struct
77//! let mut new = example::Example::default();
78//! let data = encoder.as_writer().as_slice();
79//! new.decode_from_bytes(data).expect("decoding failed");
80//! assert_eq!(example, new);
81//! ```
82//!
83//! # Messages
84//!
85//! Protobuf messages are translated directly into Rust structs, and each message field translates into a Rust field.
86//!
87//! Given the following Protobuf definition:
88//! ```proto
89//! syntax = "proto3";
90//!
91//! package example;
92//!
93//! message Example {
94//!     int32 f_int32 = 1;
95//!     int64 f_int64 = 2;
96//!     uint32 f_uint32 = 3;
97//!     uint64 f_uint64 = 4;
98//!     sint32 f_sint32 = 5;
99//!     sint64 f_sint64 = 6;
100//!     bool f_bool = 7;
101//!     fixed32 f_fixed32 = 8;
102//!     fixed64 f_fixed64 = 9;
103//!     sfixed32 f_sfixed32 = 10;
104//!     sfixed64 f_sfixed64 = 11;
105//!     float f_float = 12;
106//!     double f_double = 13;
107//! }
108//! ```
109//!
110//! `micropb-gen` will generate the following Rust structs and APIs:
111//! ```rust,ignore
112//! pub mod example_ {
113//!     #[derive(Debug, Clone)]
114//!     pub struct Example {
115//!         pub f_int32: i32,
116//!         pub f_int64: i64,
117//!         pub f_uint32: u32,
118//!         pub f_uint64: u64,
119//!         pub f_sint32: i32,
120//!         pub f_sint64: i64,
121//!         pub f_bool: bool,
122//!         pub f_fixed32: u32,
123//!         pub f_fixed64: u64,
124//!         pub f_sfixed32: u32,
125//!         pub f_sfixed64: u64,
126//!         pub f_float: f32,
127//!         pub f_double: f64,
128//!     }
129//!
130//!     impl Example {
131//!         /// Return reference to f_int32
132//!         pub fn f_int32(&self) -> &i32;
133//!         /// Return mutable reference to f_int32
134//!         pub fn mut_f_int32(&mut self) -> &mut i32;
135//!         /// Set value of f_int32
136//!         pub fn set_f_int32(&mut self, val: i32) -> &mut Self;
137//!         /// Builder method that sets f_int32. Useful for initializing the message.
138//!         pub fn init_f_int32(mut self, val: i32) -> Self;
139//!
140//!         // Same APIs for the other singular fields
141//!     }
142//!
143//!     impl Default for Example { /* ... */ }
144//!
145//!     impl PartialEq for Example { /* ... */ }
146//!
147//!     impl micropb::MessageEncode for Example { /* ... */ }
148//!
149//!     impl micropb::MessageDecode for Example { /* ... */ }
150//! }
151//! ```
152//!
153//! The generated [`MessageDecode`](micropb::MessageEncode) and
154//! [`MessageEncode`](micropb::MessageDecode) implementations provide APIs for decoding, encoding,
155//! and computing the size of `Example`.
156//!
157//! ## Optional Fields
158//!
159//! While the obvious choice for representing optional fields is [`Option`], this is not actually
160//! ideal in embedded systems because `Option<T>` actually takes up twice as much space as `T` for
161//! many types, such as `u32` and `i32`. Instead, **`micropb` tracks the presence of all optional
162//! fields of a message in a separate bitfield called a _hazzer_**, which is usually small enough to
163//! fit into the padding. Field presence can either be queried directly from the hazzer or from
164//! message APIs that return `Option`.
165//!
166//! For example, given the following Protobuf message:
167//! ```proto
168//! message Example {
169//!     optional int32 f_int32 = 1;
170//!     optional int64 f_int64 = 2;
171//!     optional bool f_bool = 3;
172//! }
173//! ```
174//!
175//! `micropb-gen` generates the following Rust struct and APIs:
176//! ```rust,ignore
177//! pub struct Example {
178//!     pub f_int32: i32,
179//!     pub f_int64: i64,
180//!     pub f_bool: bool,
181//!
182//!     pub _has: Example_::_Hazzer,
183//! }
184//!
185//! impl Example {
186//!     /// Return reference to f_int32 as an Option
187//!     pub fn f_int32(&self) -> Option<&i32>;
188//!     /// Return mutable reference to f_int32 as an Option
189//!     pub fn mut_f_int32(&mut self) -> Option<&mut i32>;
190//!     /// Set value and presence of f_int32
191//!     pub fn set_f_int32(&mut self, val: i32) -> &mut Self;
192//!     /// Clear presence of f_int32
193//!     pub fn clear_f_int32(&mut self) -> &mut Self;
194//!     /// Take f_int32 and return it
195//!     pub fn take_f_int32(&mut self) -> Option<i32>;
196//!     /// Builder method that sets f_int32. Useful for initializing the message.
197//!     pub fn init_f_int32(mut self, val: i32) -> Self;
198//!
199//!     // Same APIs for other optional fields
200//! }
201//!
202//! pub mod Example_ {
203//!     /// Tracks whether the optional fields are present
204//!     #[derive(Debug, Default, Clone, PartialEq)]
205//!     pub struct _Hazzer([u8; 1]);
206//!
207//!     impl _Hazzer {
208//!         /// Create an empty Hazzer with all fields cleared
209//!         pub const fn _new() -> Self;
210//!
211//!         /// Query presence of f_int32
212//!         pub const fn f_int32(&self) -> bool;
213//!         /// Set presence of f_int32
214//!         pub const fn set_f_int32(&mut self) -> &mut Self;
215//!         /// Clear presence of f_int32
216//!         pub const fn clear_f_int32(&mut self) -> &mut Self;
217//!         /// Builder method that toggles on the presence of f_int32. Useful for initializing the Hazzer.
218//!         pub const fn init_f_int32(mut self) -> Self;
219//!
220//!         // Same APIs for other optional fields
221//!     }
222//! }
223//!
224//! // trait impls, decode/encode logic, etc
225//! ```
226//!
227//! ### Note on Initialization
228//!
229//! **A field will be considered empty (and ignored by the encoder) if its bit in the hazzer is not
230//! set, _even if the field itself has been written_.** The following is an easy way to initialize a
231//! message with all optional fields set:
232//! ```rust,ignore
233//! Example::default().init_f_int32(4).init_f_int64(-5).init_f_bool(true)
234//! ```
235//!
236//! Alternatively, we can initialize the message using the constructor:
237//! ```rust,ignore
238//! Example {
239//!     f_int32: 4,
240//!     f_int64: -5,
241//!     f_bool: true,
242//!     // initialize the hazzer with all fields set to true
243//!     // without initializing the hazzer, all fields in Example will be considered unset
244//!     _has: Example_::_Hazzer::default()
245//!             .init_f_int32()
246//!             .init_f_int64()
247//!             .init_f_bool()
248//! }
249//! ```
250//!
251//! ### Fallback to [`Option`]
252//!
253//! By default, optional fields are represented by bitfields, as shown above. If an optional field
254//! is configured to be boxed via [`Config::boxed`], it will instead be represented as an `Option`,
255//! because `Option<Box<T>>` doesn't take up extra space compared to `Box<T>`. To override these default
256//! behaviours, see [`Config::optional_repr`].
257//!
258//! ### Required fields
259//!
260//! The generator treats required fields exactly the same way it treats optional fields.
261//!
262//! ## Oneof Fields
263//!
264//! Protobuf oneofs are translated into Rust enums. The enum type is defined in an internal
265//! module under the message, and its type name is the same as the name of the oneof field.
266//!
267//! For example, given this Protobuf definition:
268//! ```proto
269//! message Example {
270//!     oneof number {
271//!         int32 int = 1;
272//!         float decimal = 2;
273//!     }
274//! }
275//! ```
276//!
277//! `micropb-gen` generates the following definition:
278//! ```rust,no_run
279//! #[derive(Debug, Clone, PartialEq)]
280//! pub struct Example {
281//!     pub number: Option<Example_::Number>,
282//! }
283//!
284//! pub mod Example_ {
285//!     #[derive(Debug, Clone, PartialEq)]
286//!     pub enum Number {
287//!         Int(i32),
288//!         Decimal(f32),
289//!     }
290//! }
291//! ```
292//!
293//! ## Repeated, `map`, `string`, and `bytes` Fields
294//!
295//! Repeated, `map`, `string`, and `bytes` fields need to be represented as Rust "container" types,
296//! since they contain multiple elements or bytes. Normally standard types like `String` and `Vec`
297//! are used, but they aren't available in no-alloc environments. Instead, we need stack-allocated
298//! containers with fixed capacity. Since there is no defacto standard for such containers in Rust,
299//! **users are expected to configure the code generator with their own container types** (see
300//! [`Config`] for more details).
301//!
302//! For example, given the following Protobuf definition:
303//! ```proto
304//! message Containers {
305//!     string f_string = 1;
306//!     bytes f_bytes = 2;
307//!     repeated int32 f_repeated = 3;
308//!     map<int32, int64> f_map = 4;
309//! }
310//! ```
311//!
312//! and the following configuration in `build.rs`:
313//! ```rust,no_run
314//! let mut gen = micropb_gen::Generator::new();
315//! // Configure our own container types
316//! gen.configure(".",
317//!     micropb_gen::Config::new()
318//!         .string_type("crate::MyString<$N>")
319//!         .bytes_type("crate::MyVec<u8, $N>")
320//!         .vec_type("crate::MyVec<$T, $N>")
321//!         .map_type("crate::MyMap<$K, $V, $N>")
322//! );
323//!
324//! // We can also use container types from `heapless`, which have fixed capacity
325//! gen.use_container_heapless();
326//!
327//! // Same shorthand exists for containers from `arrayvec` or `alloc`
328//! // gen.use_container_arrayvec();
329//! // gen.use_container_alloc();
330//!
331//!
332//! // Since we're using fixed containers, we need to specify the max capacity of each field.
333//! // For simplicity, configure capacity of all repeated/map fields to 4 and string/bytes to 8.
334//! gen.configure(".", micropb_gen::Config::new().max_len(4).max_bytes(8));
335//! ```
336//!
337//! The following Rust struct will be generated:
338//! ```rust,no_run
339//! # use micropb::heapless as heapless;
340//! pub struct Containers {
341//!     f_string: heapless::String<8>,
342//!     f_bytes: heapless::Vec<u8, 8>,
343//!     f_repeated: heapless::Vec<i32, 4>,
344//!     f_map: heapless::FnvIndexMap<i32, i64, 4>,
345//! }
346//! ```
347//!
348//! For **decoding**, container types should implement [`PbVec`](micropb::PbVec) (repeated fields),
349//! [`PbString`](micropb::PbString), [`PbBytes`](micropb::PbBytes), or [`PbMap`](micropb::PbMap)
350//! For convenience, [`micropb`] comes with built-in implementations of the container traits for
351//! types from [`heapless`](https://docs.rs/heapless/latest/heapless),
352//! [`arrayvec`](https://docs.rs/arrayvec/latest/arrayvec), and
353//! [`alloc`](https://doc.rust-lang.org/alloc), as well as implementations on `[u8; N]` arrays and
354//! [`FixedLenString`](micropb::FixedLenString).
355//!
356//! For **encoding**, container types need to dereference into `&[T]` (repeated fields), `&str`, or
357//! `&[u8]`. Maps just need to iterate through key-value pairs.
358//!
359//! ## Message Lifetime
360//!
361//! A message struct may have up to one lifetime parameter. `micropb-gen` automatically generates
362//! the lifetime parameter for each message by checking if there's a lifetime in any of the fields.
363//!
364//! For example, given the Protobuf file from the previous section and the following `build.rs`
365//! config:
366//! ```rust,no_run
367//! # use micropb_gen::{Generator, Config, config::CustomField};
368//! # let mut gen = Generator::new();
369//! // Use `Cow` as container type with lifetime of 'a
370//! gen.configure(".",
371//!     Config::new()
372//!         .string_type("alloc::borrow::Cow<'a, str>")
373//!         .bytes_type("alloc::borrow::Cow<'a, [u8]>")
374//!         .vec_type("alloc::borrow::Cow<'a, [$T]>")
375//! );
376//! // Use a custom type for the `f_map` field, also with lifetime of 'a
377//! gen.configure(".Containers.f_map",
378//!     Config::new().custom_field(CustomField::from_type("MyField<'a>"))
379//! );
380//! ```
381//!
382//! `micropb-gen` generates the following struct:
383//! ```rust,no_run
384//! # extern crate alloc;
385//! # struct MyField<'a>(&'a u8);
386//! pub struct Containers<'a> {
387//!     f_string: alloc::borrow::Cow<'a, str>,
388//!     f_bytes: alloc::borrow::Cow<'a, [u8]>,
389//!     f_repeated: alloc::borrow::Cow<'a, [i32]>,
390//!     f_map: MyField<'a>,
391//! }
392//! ```
393//!
394//! ### Note
395//! `micropb-gen` cannot detect if a message field contains a lifetime or not, so any field that's
396//! a message with a lifetime must be configured with [`field_lifetime`](Config::field_lifetime).
397//!
398//! # Enums
399//!
400//! Protobuf enums are translated into "open" enums in Rust, rather than normal Rust enums. This is
401//! because proto3 requires enums to store unrecognized values, which is only possible with open
402//! enums.
403//!
404//! For example, given this Protobuf enum:
405//! ```proto
406//! enum Language {
407//!     RUST = 0,
408//!     C = 1,
409//!     CPP = 2,
410//! }
411//! ```
412//!
413//! `micropb-gen` generates the following Rust definition:
414//! ```rust,ignore
415//! #[derive(Debug, Clone, Default, Copy, PartialEq, Eq, Hash)]
416//! #[repr(transparent)]
417//! pub struct Language(pub i32);
418//!
419//! impl Language {
420//!     // Default value
421//!     pub const Rust: Self = Self(0);
422//!     pub const C: Self = Self(1);
423//!     pub const Cpp: Self = Self(2);
424//! }
425//!
426//! impl From<i32> for Language { /* .. */ }
427//! ```
428//!
429//! # Packages and Modules
430//!
431//! `micropb-gen` translates Protobuf package names into Rust modules by appending an underscore.
432//!
433//! For example, given the following Protobuf file:
434//! ```proto
435//! package foo.bar;
436//!
437//! // Protobuf contents
438//! ```
439//!
440//! The generated Rust file will look like:
441//! ```rust,ignore
442//! pub mod foo_ {
443//!     pub mod bar_ {
444//!         // Generated code lives here
445//!     }
446//! }
447//! ```
448//!
449//! If a Protobuf file does not have a package specifier, the generated code will instead live in
450//! the root module
451//!
452//! Message names are also translated into Rust modules by appending an underscore. For example,
453//! code generated from oneofs and nested messages within the `Name` message will live in the
454//! `Name_` module.
455//!
456//! # Configuring the Generator
457//!
458//! One of `micropb-gen`'s main features is its granular configuration system, which allows users
459//! to control how code is generated at the level of the module, message, or even individual
460//! fields. See [`Generator::configure`] and [`Config`] for more info on the configuration system.
461//!
462//! ## Notable Configurations
463//!
464//! - **Integer size**: Controls the width of the integer types used to represent [integer
465//!   fields](Config::int_size). This can also be done for [enums](Config::enum_int_size).
466//!
467//! - **Attributes**: Apply custom attributes to [fields](Config::field_attributes) and
468//!   [messages](Config::type_attributes).
469//!
470//! - **Custom fields**: Substitute your own type into the generated code, allowing complete
471//!   control over the encode and decode behaviour. Can be applied to [normal
472//!   fields](Config::custom_field) or [unknown fields](Config::unknown_handler).
473//!
474//! - **Max container size**: Specify the max capacity of [`string`/`bytes`
475//!   fields](Config::max_bytes) as well as [repeated fields](Config::max_len), which is necessary
476//!   when using fixed-capacity containers like `ArrayVec`.
477//!
478//! ## Configuration Files
479//!
480//! Configurations can be stored in TOML files rather than in `build.rs`. See
481//! [`Generator::parse_config_file`] for more info.
482
483pub mod config;
484mod generator;
485mod pathtree;
486mod utils;
487
488// This module was generated from example/file-descriptor-proto
489mod descriptor {
490    #![allow(clippy::all)]
491    #![allow(nonstandard_style, dead_code, unused_imports)]
492    include!("descriptor.rs");
493
494    pub use google_::protobuf_::*;
495}
496
497use std::{
498    env,
499    ffi::OsStr,
500    fmt, fs,
501    io::{self, Write},
502    path::{Path, PathBuf},
503    process::Command,
504};
505
506pub use config::Config;
507use generator::location::Comments;
508pub use generator::Generator;
509use micropb::{MessageDecode, PbDecoder};
510use pathtree::PathTree;
511
512#[derive(Debug, Clone, Copy, Default)]
513/// Whether to include encode and decode logic
514pub enum EncodeDecode {
515    /// Only include encode logic
516    EncodeOnly,
517    /// Only include decode logic
518    DecodeOnly,
519    #[default]
520    /// Include both encode and decode logic
521    Both,
522}
523
524impl EncodeDecode {
525    fn is_encode(self) -> bool {
526        matches!(self, Self::EncodeOnly | Self::Both)
527    }
528
529    fn is_decode(self) -> bool {
530        matches!(self, Self::DecodeOnly | Self::Both)
531    }
532}
533
534type WarningCb = fn(fmt::Arguments);
535
536fn warn_cargo_build(args: fmt::Arguments) {
537    println!("cargo::warning={args}");
538}
539
540#[allow(clippy::new_without_default)]
541impl Generator {
542    /// Create new generator with default settings
543    ///
544    /// By default, the generator assumes it's running inside a Cargo build script, so all warnings
545    /// will be emitted as compiler warnings. If the generator is not running inside a build
546    /// script, use [`with_warning_callback`](Self::with_warning_callback).
547    pub fn new() -> Self {
548        Self::with_warning_callback(warn_cargo_build)
549    }
550
551    /// Create a generator with a custom callback for emitting warnings
552    pub fn with_warning_callback(warning_cb: WarningCb) -> Self {
553        let config_tree = PathTree::new(Box::new(Config::default()));
554        // Unused Comments at the tree root
555        let comment_tree = PathTree::new(Comments::default());
556
557        Self {
558            syntax: Default::default(),
559            pkg_path: Default::default(),
560            pkg: Default::default(),
561            type_path: Default::default(),
562
563            warning_cb,
564
565            encode_decode: Default::default(),
566            retain_enum_prefix: Default::default(),
567            format: true,
568            calculate_max_size: true,
569            fdset_path: Default::default(),
570            protoc_args: Default::default(),
571            suffixed_package_names: true,
572            single_oneof_msg_as_enum: false,
573            comments_to_docs: true,
574
575            config_tree,
576            comment_tree,
577            extern_paths: Default::default(),
578        }
579    }
580
581    fn configure_with_path<'a>(&mut self, path: impl Iterator<Item = &'a str>, config: Config) {
582        let config_slot = self.config_tree.root.add_path(path).value_mut();
583        match config_slot {
584            Some(existing) => existing.merge(&config),
585            None => *config_slot = Some(Box::new(config)),
586        }
587    }
588
589    /// Apply code generator configurations to Protobuf types and fields. See
590    /// [`Config`](crate::Config) for possible configuration options.
591    ///
592    /// The `proto_path` argument is a fully-qualified Protobuf path that points to a package,
593    /// type, or field in the compiled `.proto` files. The configurations are applied to the
594    /// element specified by `proto_path`, as well as its children.
595    ///
596    /// # Example
597    /// ```
598    /// # use micropb_gen::{Generator, Config, config::IntSize};
599    /// # let mut gen = micropb_gen::Generator::new();
600    /// // Configure field attributes on a specific field of a message type
601    /// gen.configure(".pkg.Message.int_field", Config::new().field_attributes("#[serde(skip)]"));
602    ///
603    /// // Configure field attributes on all fields of a message type
604    /// gen.configure(".pkg.Message", Config::new().field_attributes("#[serde(skip)]"));
605    ///
606    /// // Configure field attributes on all fields in a package
607    /// gen.configure(".pkg", Config::new().field_attributes("#[serde(skip)]"));
608    ///
609    /// // Configure field attributes on all fields
610    /// gen.configure(".", Config::new().field_attributes("#[serde(skip)]"));
611    ///
612    /// // Configure types attributes on a specific message type
613    /// gen.configure(".pkg.Message", Config::new().type_attributes("#[derive(Serialize)]"));
614    ///
615    /// // Configure boxing behaviour on an oneof in a message type
616    /// gen.configure(".pkg.Message.my_oneof", Config::new().boxed(true));
617    ///
618    /// // Configure the int size on a variant of an oneof
619    /// gen.configure(".pkg.Message.my_oneof_variant", Config::new().int_size(IntSize::S8));
620    ///
621    /// // Configure the int size of an enum
622    /// // Note that enum variants cannot be configured
623    /// gen.configure(".pkg.Enum", Config::new().enum_int_size(IntSize::S8));
624    /// ```
625    ///
626    /// # Special paths
627    /// `configure` also supports special path suffixes for configuring fields in the generated
628    /// code that don't have a corresponding Protobuf path.
629    /// ```no_run
630    /// # use micropb_gen::{Generator, Config, config::IntSize};
631    /// # let mut gen = micropb_gen::Generator::new();
632    /// // Configure the int size of the elements in a repeated field via ".elem"
633    /// gen.configure(".pkg.Message.repeated_field.elem", Config::new().int_size(IntSize::S8));
634    ///
635    /// // Configure the int size of the keys in a map field via ".key"
636    /// gen.configure(".pkg.Message.map_field.key", Config::new().int_size(IntSize::S8));
637    /// // Configure the int size of the values in a map field via ".value"
638    /// gen.configure(".pkg.Message.map_field.value", Config::new().int_size(IntSize::S16));
639    ///
640    /// // Configure the field attributes of hazzer field and the type attributes of
641    /// // the hazzer struct in the message via "._has"
642    /// gen.configure(".pkg.Message._has",
643    ///     Config::new().field_attributes("#[serde(skip)]").type_attributes("#[derive(Serialize)]"));
644    ///
645    /// // Configure the field attributes for the unknown handler field of the message via "._unknown"
646    /// gen.configure(".pkg.Message._unknown", Config::new().field_attributes("#[serde(skip)]"));
647    ///
648    /// ```
649    pub fn configure(&mut self, proto_path: &str, config: Config) -> &mut Self {
650        self.configure_with_path(split_dot_prefixed_pkg_name(proto_path), config);
651        self
652    }
653
654    /// Apply one set of configurations to all provided Protobuf paths.
655    ///
656    /// See [`configure`](Self::configure) for how configurations are applied.
657    pub fn configure_many(&mut self, proto_paths: &[&str], config: Config) -> &mut Self {
658        for path in proto_paths {
659            self.configure(path, config.clone());
660        }
661        self
662    }
663
664    #[cfg(feature = "config-file")]
665    fn parse_config_bytes(&mut self, bytes: &[u8], prefix: &str) -> Result<(), toml::de::Error> {
666        let configs: std::collections::HashMap<String, Config> = toml::from_slice(bytes)?;
667        for (path, config) in configs.into_iter() {
668            let prefix_path = split_dot_prefixed_pkg_name(prefix);
669            let path = split_dot_prefixed_pkg_name(&path);
670            let full_path = prefix_path.chain(path);
671
672            self.configure_with_path(full_path, config);
673        }
674        Ok(())
675    }
676
677    /// Parse configurations from a TOML file and apply them to the specified Protobuf pacakge.
678    ///
679    /// # Example
680    ///
681    /// For example, if we have the following configuration in `build.rs`:
682    ///
683    /// ```
684    /// # use micropb_gen::{Config, config::{IntSize, OptionalRepr}};
685    /// let mut gen = micropb_gen::Generator::new();
686    /// gen.configure(
687    ///     ".my.pkg.Message.int_field",
688    ///     Config::new().int_size(IntSize::S16).optional_repr(OptionalRepr::Option)
689    /// );
690    /// gen.configure("my.pkg.Message.bad_field", Config::new().skip(true));
691    /// ```
692    ///
693    /// We can instead load the configuration for `.my.pkg` from a TOML file:
694    /// ```no_run
695    /// # use std::path::Path;
696    /// # let mut gen = micropb_gen::Generator::new();
697    /// gen.parse_config_file(Path::new("my.pkg.toml"), ".my.pkg")?;
698    /// # Ok::<_, std::io::Error>(())
699    /// ```
700    ///
701    /// `my.pkg.toml`
702    /// ```toml
703    /// # Each Config is represented as a table in the TOML document, keyed by the Protobuf path
704    /// ["Message.int_field"]
705    /// int_size = "S16"
706    /// optional_repr = "Option"
707    ///
708    /// ["Message.bad_field"]
709    /// skip = true
710    /// ```
711    ///
712    /// <div class="warning">Dot-separated Protobuf paths in config files MUST be wrapped in quotes
713    /// for TOML parsing to work correctly.</div>
714    #[cfg(feature = "config-file")]
715    pub fn parse_config_file(&mut self, file_path: &Path, package: &str) -> Result<(), io::Error> {
716        let file_bytes = fs::read(file_path)?;
717        self.parse_config_bytes(&file_bytes, package)
718            .map_err(io::Error::other)?;
719        Ok(())
720    }
721
722    /// Configure the generator to generate `heapless` containers for Protobuf `string`, `bytes`,
723    /// repeated, and `map` fields.
724    ///
725    /// If using this option, `micropb` should have the `container-heapless` feature enabled.
726    ///
727    /// Specifically, `heapless::String<N>` is generated for `string` fields, `heapless::Vec<u8, N>`
728    /// for `bytes` fields, `heapless::Vec<T, N>` for repeated fields, and
729    /// `heapless::FnvIndexMap<K, V, N>` for `map` fields. This uses [`configure`](Self::configure)
730    /// under the hood, so configurations set by this call can all be overriden.
731    ///
732    /// # Note
733    /// Since `heapless` containers are fixed size, [`max_len`](Config::max_len) or
734    /// [`max_bytes`](Config::max_bytes) must be set for all fields that generate these containers.
735    pub fn use_container_heapless(&mut self) -> &mut Self {
736        self.configure(
737            ".",
738            Config::new()
739                .vec_type("::micropb::heapless::Vec<$T, $N>")
740                .string_type("::micropb::heapless::String<$N>")
741                .bytes_type("::micropb::heapless::Vec<u8, $N>")
742                .map_type("::micropb::heapless::FnvIndexMap<$K, $V, $N>"),
743        );
744        self
745    }
746
747    /// Configure the generator to generate `arrayvec` containers for Protobuf `string`, `bytes`,
748    /// and repeated fields.
749    ///
750    /// If using this option, `micropb` should have the `container-arrayvec` feature enabled.
751    ///
752    /// Specifically, `arrayvec::ArrayString<N>` is generated for `string` fields,
753    /// `arrayvec::ArrayVec<u8, N>` for `bytes` fields, and `arrayvec::ArrayVec<T, N>` for repeated
754    /// fields. This uses [`configure`](Self::configure) under the hood, so configurations set by
755    /// this call can all be overriden.
756    ///
757    /// # Note
758    /// No container is configured for `map` fields, since `arrayvec` doesn't have a suitable map
759    /// type. If the .proto files contain `map` fields, [`map_type`](Config::map_type) will need to
760    /// be configured separately.
761    ///
762    /// Since `arrayvec` containers are fixed size, [`max_len`](Config::max_len) or
763    /// [`max_bytes`](Config::max_bytes) must be set for all fields that generate these containers.
764    pub fn use_container_arrayvec(&mut self) -> &mut Self {
765        self.configure(
766            ".",
767            Config::new()
768                .vec_type("::micropb::arrayvec::ArrayVec<$T, $N>")
769                .bytes_type("::micropb::arrayvec::ArrayVec<u8, $N>")
770                .string_type("::micropb::arrayvec::ArrayString<$N>"),
771        );
772        self
773    }
774
775    /// Configure the generator to generate `alloc` containers for Protobuf `string`, `bytes`,
776    /// repeated, and `map` fields.
777    ///
778    /// If using this option, `micropb` should have the `alloc` feature enabled.
779    ///
780    /// Specifically, `alloc::string::String` is generated for `string` fields,
781    /// `alloc::vec::Vec<u8>` is for `bytes` fields, `alloc::vec::Vec<T>` for repeated fields, and
782    /// `alloc::collections::BTreeMap<K, V>` for `map` fields. This uses
783    /// [`configure`](Self::configure) under the hood, so configurations set by this call can all
784    /// be overriden by future configurations.
785    pub fn use_container_alloc(&mut self) -> &mut Self {
786        self.configure(
787            ".",
788            Config::new()
789                .vec_type("::alloc::vec::Vec<$T>")
790                .bytes_type("::alloc::vec::Vec::<u8>")
791                .string_type("::alloc::string::String")
792                .map_type("::alloc::collections::BTreeMap<$K, $V>"),
793        );
794        self
795    }
796
797    /// Configure the generator to generate `std` containers for Protobuf `string`, `bytes`,
798    /// repeated, and `map` fields.
799    ///
800    /// If using this option, `micropb` should have the `std` feature enabled.
801    ///
802    /// Specifically, `std::string::String` is generated for `string` fields, `std::vec::Vec<u8>`
803    /// for `bytes` fields, `std::vec::Vec<T>` for repeated fields, and
804    /// `std::collections::HashMap<K, V>` for `map` fields. This uses
805    /// [`configure`](Self::configure) under the hood, so configurations set by this call can all
806    /// be overriden by future configurations.
807    pub fn use_container_std(&mut self) -> &mut Self {
808        self.configure(
809            ".",
810            Config::new()
811                .vec_type("::std::vec::Vec<$T>")
812                .bytes_type("::std::vec::Vec::<u8>")
813                .string_type("::std::string::String")
814                .map_type("::std::collections::HashMap<$K, $V>"),
815        );
816        self
817    }
818
819    /// Compile `.proto` files into a single Rust file.
820    ///
821    /// # Example
822    /// ```no_run
823    /// // build.rs
824    /// let mut gen = micropb_gen::Generator::new();
825    /// gen.compile_protos(&["server.proto", "client.proto"],
826    ///                     std::env::var("OUT_DIR").unwrap() + "/output.rs").unwrap();
827    /// ```
828    pub fn compile_protos(
829        &mut self,
830        protos: &[impl AsRef<Path>],
831        out_filename: impl AsRef<Path>,
832    ) -> io::Result<()> {
833        let tmp;
834        let fdset_file = if let Some(fdset_path) = &self.fdset_path {
835            fdset_path.to_owned()
836        } else {
837            tmp = tempfile::tempdir()?;
838            tmp.path().join("micropb-fdset")
839        };
840
841        // Get protoc command from PROTOC env-var, otherwise just use "protoc"
842        let mut cmd = Command::new(env::var("PROTOC").as_deref().unwrap_or("protoc"));
843        cmd.arg("-o").arg(fdset_file.as_os_str());
844        if self.comments_to_docs {
845            cmd.arg("--include_source_info");
846        }
847        cmd.args(&self.protoc_args);
848
849        for proto in protos {
850            cmd.arg(proto.as_ref());
851        }
852
853        let output = cmd.output().map_err(|e| match e.kind() {
854            io::ErrorKind::NotFound => {
855                io::Error::new(e.kind(), "`protoc` was not found. Check your PATH.")
856            }
857            _ => e,
858        })?;
859        if !output.status.success() {
860            return Err(io::Error::other(format!(
861                "protoc failed: {}",
862                String::from_utf8_lossy(&output.stderr)
863            )));
864        }
865
866        self.compile_fdset_file(fdset_file, out_filename)
867    }
868
869    /// Compile a Protobuf file descriptor set into a Rust file.
870    ///
871    /// Similar to [`compile_protos`](Self::compile_protos), but it does not invoke `protoc` and
872    /// instead takes a file descriptor set.
873    pub fn compile_fdset_file(
874        &mut self,
875        fdset_file: impl AsRef<Path>,
876        out_filename: impl AsRef<Path>,
877    ) -> io::Result<()> {
878        let bytes = fs::read(fdset_file)?;
879        let mut decoder = PbDecoder::new(bytes.as_slice());
880        let mut fdset = descriptor::FileDescriptorSet::default();
881        fdset
882            .decode(&mut decoder, bytes.len())
883            .expect("file descriptor set decode failed");
884        let code = self.generate_fdset(&fdset)?;
885
886        self.warn_unused_configs();
887
888        #[cfg(feature = "format")]
889        let output = if self.format {
890            prettyplease::unparse(
891                &syn::parse2(code).expect("output code should be parseable as a file"),
892            )
893        } else {
894            code.to_string()
895        };
896        #[cfg(not(feature = "format"))]
897        let output = code.to_string();
898
899        let mut file = fs::File::create(out_filename)?;
900        file.write_all(output.as_bytes())?;
901
902        Ok(())
903    }
904
905    /// Determine whether the generator strips enum names from variant names.
906    ///
907    /// Protobuf enums commonly include the enum name as a prefix of variant names. `micropb`
908    /// strips this enum name prefix by default. Setting this to `true` prevents the prefix from
909    /// being stripped.
910    pub fn retain_enum_prefix(&mut self, retain_enum_prefix: bool) -> &mut Self {
911        self.retain_enum_prefix = retain_enum_prefix;
912        self
913    }
914
915    /// Determine whether the generator formats the output code.
916    ///
917    /// If the `format` feature isn't enabled, this does nothing.
918    pub fn format(&mut self, format: bool) -> &mut Self {
919        self.format = format;
920        self
921    }
922
923    /// Determine whether to generate logic for encoding and decoding Protobuf messages.
924    ///
925    /// Some applications don't need to support both encoding and decoding. This setting allows
926    /// either the encoding or decoding logic to be omitted from the output. By default, both
927    /// encoding and decoding are included.
928    ///
929    /// This setting allows omitting the `encode` or `decode` feature flag from `micropb`.
930    pub fn encode_decode(&mut self, encode_decode: EncodeDecode) -> &mut Self {
931        self.encode_decode = encode_decode;
932        self
933    }
934
935    /// When set, the file descriptor set generated by `protoc` is written to the provided path,
936    /// instead of a temporary directory.
937    pub fn file_descriptor_set_path<P: Into<PathBuf>>(&mut self, path: P) -> &mut Self {
938        self.fdset_path = Some(path.into());
939        self
940    }
941
942    /// Add an argument to the `protoc` invocation when compiling Protobuf files.
943    pub fn add_protoc_arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Self {
944        self.protoc_args.push(arg.as_ref().to_owned());
945        self
946    }
947
948    /// Declare an externally-provided Protobuf type.
949    ///
950    /// When compiling a `.proto` file that imports types from another `.proto` file, `micropb`
951    /// won't compile the imported file if it's not included in the
952    /// [`compile_protos`](Self::compile_protos) invocation. This is because the imported file may
953    /// have already been compiled in another crate. In order to recognize externally-imported
954    /// types, use `extern_type_path` to map the full Protobuf path of the imported type to the
955    /// full path of the corresponding Rust type.
956    ///
957    /// # Example
958    ///
959    /// For example, let's say we have `app.proto`:
960    /// ```proto
961    /// // app.proto
962    ///
963    /// syntax = "proto3";
964    /// package app;
965    ///
966    /// message App {
967    ///     time.Timestamp timestamp = 1;
968    ///     time.TZ timezone = 2;
969    /// }
970    /// ```
971    ///
972    /// `app.proto` imports from `time.proto`, which has already been compiled into the
973    /// `time` crate:
974    /// ```proto
975    /// // time.proto
976    ///
977    /// syntax = "proto3";
978    /// package time;
979    ///
980    /// message Timestamp {
981    ///     uint32 ts = 1;
982    /// }
983    ///
984    /// enum TZ {
985    ///     TZ_UTC = 0;
986    ///     TZ_PST = 1;
987    /// }
988    /// ```
989    ///
990    /// For our application, we're only interested in compiling `app.proto`, since `time.proto` has
991    /// already been compiled by another crate. As such, we need to substitute Protobuf types
992    /// imported from `time.proto` with Rust definitions from the `time` crate.
993    /// ```no_run
994    /// // build.rs of app
995    ///
996    /// let mut gen = micropb_gen::Generator::new();
997    /// // Substitute Timestamp message
998    /// gen.extern_type_path(".time.Timestamp", "time::Timestamp");
999    /// // Substitute TZ enum
1000    /// gen.extern_type_path(".time.TZ", "time::Tz");
1001    /// // Compile only app.proto, not time.proto
1002    /// gen.compile_protos(&["app.proto"], std::env::var("OUT_DIR").unwrap() + "/output.rs").unwrap();
1003    /// ```
1004    ///
1005    /// # Note
1006    /// It's technically possible to substitute in Rust types that aren't generated by `micropb-gen`.
1007    /// However, the generated code expects substituted messages to implement `MessageDecode` and
1008    /// `MessageEncode`, and substituted enums to have the "open-enum" structure.
1009    pub fn extern_type_path<P1: AsRef<str>, P2: AsRef<str>>(
1010        &mut self,
1011        proto_path: P1,
1012        rust_path: P2,
1013    ) -> &mut Self {
1014        assert!(
1015            proto_path.as_ref().starts_with('.'),
1016            "Fully-qualified Proto path must start with '.'"
1017        );
1018        self.extern_paths.insert(
1019            proto_path.as_ref().to_owned(),
1020            syn::parse_str(rust_path.as_ref()).expect("failed to tokenize extern path"),
1021        );
1022        self
1023    }
1024
1025    /// Determines whether to generate code to calculate the `MAX_SIZE` constant on each message.
1026    ///
1027    /// By default, `micropb-gen` generates code to calculate the `MAX_SIZE` associated constant
1028    /// for each message struct, which determines the max buffer size needed to encode it. If this
1029    /// is set to false, then it replaces the calculations with `None`, effectively disabling the
1030    /// use of `MAX_SIZE`. This has no runtime impact, but it can reduce the size of the output
1031    /// file.
1032    pub fn calculate_max_size(&mut self, flag: bool) -> &mut Self {
1033        self.calculate_max_size = flag;
1034        self
1035    }
1036
1037    /// Determines whether the modules names generated from package specifiers are suffixed with an
1038    /// underscore.
1039    ///
1040    /// This is on by default. Even when off, module names like "super" and modules created from
1041    /// from message names will still be suffixed.
1042    pub fn suffixed_package_names(&mut self, suffixed: bool) -> &mut Self {
1043        self.suffixed_package_names = suffixed;
1044        self
1045    }
1046
1047    /// For messages with only a single oneof and no other fields, generate an enum representing
1048    /// the oneof rather than a struct.
1049    ///
1050    /// # Example
1051    ///
1052    /// Given the following message:
1053    /// ```proto
1054    /// message Number {
1055    ///     oneof inner {
1056    ///         sint32 signed = 1;
1057    ///         uint32 unsigned = 2;
1058    ///         float fraction = 3;
1059    ///     }
1060    /// }
1061    /// ```
1062    ///
1063    /// The following enum type will be generated:
1064    /// ```no_run
1065    /// pub enum Number {
1066    ///     Signed(i32),
1067    ///     Unsigned(u32),
1068    ///     Fraction(f32),
1069    ///     None,
1070    /// }
1071    /// ```
1072    ///
1073    /// All other message structures, including those with multiple oneofs or a single oneof plus
1074    /// normal fields, will be generated as normal message structs.
1075    ///
1076    /// # Ignored configs
1077    ///
1078    /// With this option, configurations that apply to the oneof itself (`.Number.inner`) will be
1079    /// ignored. Also, [`unknown_handler`](Config::unknown_handler) will be ignored.
1080    pub fn single_oneof_msg_as_enum(&mut self, as_enum: bool) -> &mut Self {
1081        self.single_oneof_msg_as_enum = as_enum;
1082        self
1083    }
1084
1085    /// If enabled, comments in the Proto file will be used to generate doc comments on the
1086    /// messages, enums, oneofs, and fields in the generated code.
1087    ///
1088    /// Enabled by default.
1089    pub fn comments_to_docs(&mut self, flag: bool) -> &mut Self {
1090        self.comments_to_docs = flag;
1091        self
1092    }
1093}
1094
1095fn split_pkg_name(name: &str) -> impl Iterator<Item = &str> {
1096    // ignore empty segments, so empty pkg name points to root node
1097    name.split('.').filter(|seg| !seg.is_empty())
1098}
1099
1100fn split_dot_prefixed_pkg_name(mut name: &str) -> impl Iterator<Item = &str> {
1101    if name.starts_with('.') {
1102        name = &name[1..];
1103    }
1104    split_pkg_name(name)
1105}