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;
507pub use generator::Generator;
508use micropb::{MessageDecode, PbDecoder};
509use pathtree::PathTree;
510
511#[derive(Debug, Clone, Copy, Default)]
512/// Whether to include encode and decode logic
513pub enum EncodeDecode {
514    /// Only include encode logic
515    EncodeOnly,
516    /// Only include decode logic
517    DecodeOnly,
518    #[default]
519    /// Include both encode and decode logic
520    Both,
521}
522
523impl EncodeDecode {
524    fn is_encode(self) -> bool {
525        matches!(self, Self::EncodeOnly | Self::Both)
526    }
527
528    fn is_decode(self) -> bool {
529        matches!(self, Self::DecodeOnly | Self::Both)
530    }
531}
532
533type WarningCb = fn(fmt::Arguments);
534
535fn warn_cargo_build(args: fmt::Arguments) {
536    println!("cargo::warning={args}");
537}
538
539#[allow(clippy::new_without_default)]
540impl Generator {
541    /// Create new generator with default settings
542    ///
543    /// By default, the generator assumes it's running inside a Cargo build script, so all warnings
544    /// will be emitted as compiler warnings. If the generator is not running inside a build
545    /// script, use [`with_warning_callback`](Self::with_warning_callback).
546    pub fn new() -> Self {
547        Self::with_warning_callback(warn_cargo_build)
548    }
549
550    /// Create a generator with a custom callback for emitting warnings
551    pub fn with_warning_callback(warning_cb: WarningCb) -> Self {
552        let config_tree = PathTree::new(Box::new(Config::new()));
553        Self {
554            syntax: Default::default(),
555            pkg_path: Default::default(),
556            pkg: Default::default(),
557            type_path: Default::default(),
558
559            warning_cb,
560
561            encode_decode: Default::default(),
562            retain_enum_prefix: Default::default(),
563            format: true,
564            calculate_max_size: true,
565            fdset_path: Default::default(),
566            protoc_args: Default::default(),
567            suffixed_package_names: true,
568            single_oneof_msg_as_enum: false,
569
570            config_tree,
571            extern_paths: Default::default(),
572        }
573    }
574
575    fn configure_with_path<'a>(&mut self, path: impl Iterator<Item = &'a str>, config: Config) {
576        let config_slot = self.config_tree.root.add_path(path).value_mut();
577        match config_slot {
578            Some(existing) => existing.merge(&config),
579            None => *config_slot = Some(Box::new(config)),
580        }
581    }
582
583    /// Apply code generator configurations to Protobuf types and fields. See
584    /// [`Config`](crate::Config) for possible configuration options.
585    ///
586    /// The `proto_path` argument is a fully-qualified Protobuf path that points to a package,
587    /// type, or field in the compiled `.proto` files. The configurations are applied to the
588    /// element specified by `proto_path`, as well as its children.
589    ///
590    /// # Example
591    /// ```
592    /// # use micropb_gen::{Generator, Config, config::IntSize};
593    /// # let mut gen = micropb_gen::Generator::new();
594    /// // Configure field attributes on a specific field of a message type
595    /// gen.configure(".pkg.Message.int_field", Config::new().field_attributes("#[serde(skip)]"));
596    ///
597    /// // Configure field attributes on all fields of a message type
598    /// gen.configure(".pkg.Message", Config::new().field_attributes("#[serde(skip)]"));
599    ///
600    /// // Configure field attributes on all fields in a package
601    /// gen.configure(".pkg", Config::new().field_attributes("#[serde(skip)]"));
602    ///
603    /// // Configure field attributes on all fields
604    /// gen.configure(".", Config::new().field_attributes("#[serde(skip)]"));
605    ///
606    /// // Configure types attributes on a specific message type
607    /// gen.configure(".pkg.Message", Config::new().type_attributes("#[derive(Serialize)]"));
608    ///
609    /// // Configure boxing behaviour on an oneof in a message type
610    /// gen.configure(".pkg.Message.my_oneof", Config::new().boxed(true));
611    ///
612    /// // Configure the int size on a variant of an oneof
613    /// gen.configure(".pkg.Message.my_oneof_variant", Config::new().int_size(IntSize::S8));
614    ///
615    /// // Configure the int size of an enum
616    /// // Note that enum variants cannot be configured
617    /// gen.configure(".pkg.Enum", Config::new().enum_int_size(IntSize::S8));
618    /// ```
619    ///
620    /// # Special paths
621    /// `configure` also supports special path suffixes for configuring fields in the generated
622    /// code that don't have a corresponding Protobuf path.
623    /// ```no_run
624    /// # use micropb_gen::{Generator, Config, config::IntSize};
625    /// # let mut gen = micropb_gen::Generator::new();
626    /// // Configure the int size of the elements in a repeated field via ".elem"
627    /// gen.configure(".pkg.Message.repeated_field.elem", Config::new().int_size(IntSize::S8));
628    ///
629    /// // Configure the int size of the keys in a map field via ".key"
630    /// gen.configure(".pkg.Message.map_field.key", Config::new().int_size(IntSize::S8));
631    /// // Configure the int size of the values in a map field via ".value"
632    /// gen.configure(".pkg.Message.map_field.value", Config::new().int_size(IntSize::S16));
633    ///
634    /// // Configure the field attributes of hazzer field and the type attributes of
635    /// // the hazzer struct in the message via "._has"
636    /// gen.configure(".pkg.Message._has",
637    ///     Config::new().field_attributes("#[serde(skip)]").type_attributes("#[derive(Serialize)]"));
638    ///
639    /// // Configure the field attributes for the unknown handler field of the message via "._unknown"
640    /// gen.configure(".pkg.Message._unknown", Config::new().field_attributes("#[serde(skip)]"));
641    ///
642    /// ```
643    pub fn configure(&mut self, proto_path: &str, config: Config) -> &mut Self {
644        self.configure_with_path(split_dot_prefixed_pkg_name(proto_path), config);
645        self
646    }
647
648    /// Apply one set of configurations to all provided Protobuf paths.
649    ///
650    /// See [`configure`](Self::configure) for how configurations are applied.
651    pub fn configure_many(&mut self, proto_paths: &[&str], config: Config) -> &mut Self {
652        for path in proto_paths {
653            self.configure(path, config.clone());
654        }
655        self
656    }
657
658    #[cfg(feature = "config-file")]
659    fn parse_config_bytes(&mut self, bytes: &[u8], prefix: &str) -> Result<(), toml::de::Error> {
660        let configs: std::collections::HashMap<String, Config> = toml::from_slice(bytes)?;
661        for (path, config) in configs.into_iter() {
662            let prefix_path = split_dot_prefixed_pkg_name(prefix);
663            let path = split_dot_prefixed_pkg_name(&path);
664            let full_path = prefix_path.chain(path);
665
666            self.configure_with_path(full_path, config);
667        }
668        Ok(())
669    }
670
671    /// Parse configurations from a TOML file and apply them to the specified Protobuf pacakge.
672    ///
673    /// # Example
674    ///
675    /// For example, if we have the following configuration in `build.rs`:
676    ///
677    /// ```
678    /// # use micropb_gen::{Config, config::{IntSize, OptionalRepr}};
679    /// let mut gen = micropb_gen::Generator::new();
680    /// gen.configure(
681    ///     ".my.pkg.Message.int_field",
682    ///     Config::new().int_size(IntSize::S16).optional_repr(OptionalRepr::Option)
683    /// );
684    /// gen.configure("my.pkg.Message.bad_field", Config::new().skip(true));
685    /// ```
686    ///
687    /// We can instead load the configuration for `.my.pkg` from a TOML file:
688    /// ```no_run
689    /// # use std::path::Path;
690    /// # let mut gen = micropb_gen::Generator::new();
691    /// gen.parse_config_file(Path::new("my.pkg.toml"), ".my.pkg")?;
692    /// # Ok::<_, std::io::Error>(())
693    /// ```
694    ///
695    /// `my.pkg.toml`
696    /// ```toml
697    /// # Each Config is represented as a table in the TOML document, keyed by the Protobuf path
698    /// ["Message.int_field"]
699    /// int_size = "S16"
700    /// optional_repr = "Option"
701    ///
702    /// ["Message.bad_field"]
703    /// skip = true
704    /// ```
705    ///
706    /// <div class="warning">Dot-separated Protobuf paths in config files MUST be wrapped in quotes
707    /// for TOML parsing to work correctly.</div>
708    #[cfg(feature = "config-file")]
709    pub fn parse_config_file(&mut self, file_path: &Path, package: &str) -> Result<(), io::Error> {
710        let file_bytes = fs::read(file_path)?;
711        self.parse_config_bytes(&file_bytes, package)
712            .map_err(io::Error::other)?;
713        Ok(())
714    }
715
716    /// Configure the generator to generate `heapless` containers for Protobuf `string`, `bytes`,
717    /// repeated, and `map` fields.
718    ///
719    /// If using this option, `micropb` should have the `container-heapless` feature enabled.
720    ///
721    /// Specifically, `heapless::String<N>` is generated for `string` fields, `heapless::Vec<u8, N>`
722    /// for `bytes` fields, `heapless::Vec<T, N>` for repeated fields, and
723    /// `heapless::FnvIndexMap<K, V, N>` for `map` fields. This uses [`configure`](Self::configure)
724    /// under the hood, so configurations set by this call can all be overriden.
725    ///
726    /// # Note
727    /// Since `heapless` containers are fixed size, [`max_len`](Config::max_len) or
728    /// [`max_bytes`](Config::max_bytes) must be set for all fields that generate these containers.
729    pub fn use_container_heapless(&mut self) -> &mut Self {
730        self.configure(
731            ".",
732            Config::new()
733                .vec_type("::micropb::heapless::Vec<$T, $N>")
734                .string_type("::micropb::heapless::String<$N>")
735                .bytes_type("::micropb::heapless::Vec<u8, $N>")
736                .map_type("::micropb::heapless::FnvIndexMap<$K, $V, $N>"),
737        );
738        self
739    }
740
741    /// Configure the generator to generate `arrayvec` containers for Protobuf `string`, `bytes`,
742    /// and repeated fields.
743    ///
744    /// If using this option, `micropb` should have the `container-arrayvec` feature enabled.
745    ///
746    /// Specifically, `arrayvec::ArrayString<N>` is generated for `string` fields,
747    /// `arrayvec::ArrayVec<u8, N>` for `bytes` fields, and `arrayvec::ArrayVec<T, N>` for repeated
748    /// fields. This uses [`configure`](Self::configure) under the hood, so configurations set by
749    /// this call can all be overriden.
750    ///
751    /// # Note
752    /// No container is configured for `map` fields, since `arrayvec` doesn't have a suitable map
753    /// type. If the .proto files contain `map` fields, [`map_type`](Config::map_type) will need to
754    /// be configured separately.
755    ///
756    /// Since `arrayvec` containers are fixed size, [`max_len`](Config::max_len) or
757    /// [`max_bytes`](Config::max_bytes) must be set for all fields that generate these containers.
758    pub fn use_container_arrayvec(&mut self) -> &mut Self {
759        self.configure(
760            ".",
761            Config::new()
762                .vec_type("::micropb::arrayvec::ArrayVec<$T, $N>")
763                .bytes_type("::micropb::arrayvec::ArrayVec<u8, $N>")
764                .string_type("::micropb::arrayvec::ArrayString<$N>"),
765        );
766        self
767    }
768
769    /// Configure the generator to generate `alloc` containers for Protobuf `string`, `bytes`,
770    /// repeated, and `map` fields.
771    ///
772    /// If using this option, `micropb` should have the `alloc` feature enabled.
773    ///
774    /// Specifically, `alloc::string::String` is generated for `string` fields,
775    /// `alloc::vec::Vec<u8>` is for `bytes` fields, `alloc::vec::Vec<T>` for repeated fields, and
776    /// `alloc::collections::BTreeMap<K, V>` for `map` fields. This uses
777    /// [`configure`](Self::configure) under the hood, so configurations set by this call can all
778    /// be overriden by future configurations.
779    pub fn use_container_alloc(&mut self) -> &mut Self {
780        self.configure(
781            ".",
782            Config::new()
783                .vec_type("::alloc::vec::Vec<$T>")
784                .bytes_type("::alloc::vec::Vec::<u8>")
785                .string_type("::alloc::string::String")
786                .map_type("::alloc::collections::BTreeMap<$K, $V>"),
787        );
788        self
789    }
790
791    /// Configure the generator to generate `std` containers for Protobuf `string`, `bytes`,
792    /// repeated, and `map` fields.
793    ///
794    /// If using this option, `micropb` should have the `std` feature enabled.
795    ///
796    /// Specifically, `std::string::String` is generated for `string` fields, `std::vec::Vec<u8>`
797    /// for `bytes` fields, `std::vec::Vec<T>` for repeated fields, and
798    /// `std::collections::HashMap<K, V>` for `map` fields. This uses
799    /// [`configure`](Self::configure) under the hood, so configurations set by this call can all
800    /// be overriden by future configurations.
801    pub fn use_container_std(&mut self) -> &mut Self {
802        self.configure(
803            ".",
804            Config::new()
805                .vec_type("::std::vec::Vec<$T>")
806                .bytes_type("::std::vec::Vec::<u8>")
807                .string_type("::std::string::String")
808                .map_type("::std::collections::HashMap<$K, $V>"),
809        );
810        self
811    }
812
813    /// Compile `.proto` files into a single Rust file.
814    ///
815    /// # Example
816    /// ```no_run
817    /// // build.rs
818    /// let mut gen = micropb_gen::Generator::new();
819    /// gen.compile_protos(&["server.proto", "client.proto"],
820    ///                     std::env::var("OUT_DIR").unwrap() + "/output.rs").unwrap();
821    /// ```
822    pub fn compile_protos(
823        &mut self,
824        protos: &[impl AsRef<Path>],
825        out_filename: impl AsRef<Path>,
826    ) -> io::Result<()> {
827        let tmp;
828        let fdset_file = if let Some(fdset_path) = &self.fdset_path {
829            fdset_path.to_owned()
830        } else {
831            tmp = tempfile::tempdir()?;
832            tmp.path().join("micropb-fdset")
833        };
834
835        // Get protoc command from PROTOC env-var, otherwise just use "protoc"
836        let mut cmd = Command::new(env::var("PROTOC").as_deref().unwrap_or("protoc"));
837        cmd.arg("-o").arg(fdset_file.as_os_str());
838        cmd.args(&self.protoc_args);
839
840        for proto in protos {
841            cmd.arg(proto.as_ref());
842        }
843
844        let output = cmd.output().map_err(|e| match e.kind() {
845            io::ErrorKind::NotFound => {
846                io::Error::new(e.kind(), "`protoc` was not found. Check your PATH.")
847            }
848            _ => e,
849        })?;
850        if !output.status.success() {
851            return Err(io::Error::other(format!(
852                "protoc failed: {}",
853                String::from_utf8_lossy(&output.stderr)
854            )));
855        }
856
857        self.compile_fdset_file(fdset_file, out_filename)
858    }
859
860    /// Compile a Protobuf file descriptor set into a Rust file.
861    ///
862    /// Similar to [`compile_protos`](Self::compile_protos), but it does not invoke `protoc` and
863    /// instead takes a file descriptor set.
864    pub fn compile_fdset_file(
865        &mut self,
866        fdset_file: impl AsRef<Path>,
867        out_filename: impl AsRef<Path>,
868    ) -> io::Result<()> {
869        let bytes = fs::read(fdset_file)?;
870        let mut decoder = PbDecoder::new(bytes.as_slice());
871        let mut fdset = descriptor::FileDescriptorSet::default();
872        fdset
873            .decode(&mut decoder, bytes.len())
874            .expect("file descriptor set decode failed");
875        let code = self.generate_fdset(&fdset)?;
876
877        self.warn_unused_configs();
878
879        #[cfg(feature = "format")]
880        let output = if self.format {
881            prettyplease::unparse(
882                &syn::parse2(code).expect("output code should be parseable as a file"),
883            )
884        } else {
885            code.to_string()
886        };
887        #[cfg(not(feature = "format"))]
888        let output = code.to_string();
889
890        let mut file = fs::File::create(out_filename)?;
891        file.write_all(output.as_bytes())?;
892
893        Ok(())
894    }
895
896    /// Determine whether the generator strips enum names from variant names.
897    ///
898    /// Protobuf enums commonly include the enum name as a prefix of variant names. `micropb`
899    /// strips this enum name prefix by default. Setting this to `true` prevents the prefix from
900    /// being stripped.
901    pub fn retain_enum_prefix(&mut self, retain_enum_prefix: bool) -> &mut Self {
902        self.retain_enum_prefix = retain_enum_prefix;
903        self
904    }
905
906    /// Determine whether the generator formats the output code.
907    ///
908    /// If the `format` feature isn't enabled, this does nothing.
909    pub fn format(&mut self, format: bool) -> &mut Self {
910        self.format = format;
911        self
912    }
913
914    /// Determine whether to generate logic for encoding and decoding Protobuf messages.
915    ///
916    /// Some applications don't need to support both encoding and decoding. This setting allows
917    /// either the encoding or decoding logic to be omitted from the output. By default, both
918    /// encoding and decoding are included.
919    ///
920    /// This setting allows omitting the `encode` or `decode` feature flag from `micropb`.
921    pub fn encode_decode(&mut self, encode_decode: EncodeDecode) -> &mut Self {
922        self.encode_decode = encode_decode;
923        self
924    }
925
926    /// When set, the file descriptor set generated by `protoc` is written to the provided path,
927    /// instead of a temporary directory.
928    pub fn file_descriptor_set_path<P: Into<PathBuf>>(&mut self, path: P) -> &mut Self {
929        self.fdset_path = Some(path.into());
930        self
931    }
932
933    /// Add an argument to the `protoc` invocation when compiling Protobuf files.
934    pub fn add_protoc_arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Self {
935        self.protoc_args.push(arg.as_ref().to_owned());
936        self
937    }
938
939    /// Declare an externally-provided Protobuf type.
940    ///
941    /// When compiling a `.proto` file that imports types from another `.proto` file, `micropb`
942    /// won't compile the imported file if it's not included in the
943    /// [`compile_protos`](Self::compile_protos) invocation. This is because the imported file may
944    /// have already been compiled in another crate. In order to recognize externally-imported
945    /// types, use `extern_type_path` to map the full Protobuf path of the imported type to the
946    /// full path of the corresponding Rust type.
947    ///
948    /// # Example
949    ///
950    /// For example, let's say we have `app.proto`:
951    /// ```proto
952    /// // app.proto
953    ///
954    /// syntax = "proto3";
955    /// package app;
956    ///
957    /// message App {
958    ///     time.Timestamp timestamp = 1;
959    ///     time.TZ timezone = 2;
960    /// }
961    /// ```
962    ///
963    /// `app.proto` imports from `time.proto`, which has already been compiled into the
964    /// `time` crate:
965    /// ```proto
966    /// // time.proto
967    ///
968    /// syntax = "proto3";
969    /// package time;
970    ///
971    /// message Timestamp {
972    ///     uint32 ts = 1;
973    /// }
974    ///
975    /// enum TZ {
976    ///     TZ_UTC = 0;
977    ///     TZ_PST = 1;
978    /// }
979    /// ```
980    ///
981    /// For our application, we're only interested in compiling `app.proto`, since `time.proto` has
982    /// already been compiled by another crate. As such, we need to substitute Protobuf types
983    /// imported from `time.proto` with Rust definitions from the `time` crate.
984    /// ```no_run
985    /// // build.rs of app
986    ///
987    /// let mut gen = micropb_gen::Generator::new();
988    /// // Substitute Timestamp message
989    /// gen.extern_type_path(".time.Timestamp", "time::Timestamp");
990    /// // Substitute TZ enum
991    /// gen.extern_type_path(".time.TZ", "time::Tz");
992    /// // Compile only app.proto, not time.proto
993    /// gen.compile_protos(&["app.proto"], std::env::var("OUT_DIR").unwrap() + "/output.rs").unwrap();
994    /// ```
995    ///
996    /// # Note
997    /// It's technically possible to substitute in Rust types that aren't generated by `micropb-gen`.
998    /// However, the generated code expects substituted messages to implement `MessageDecode` and
999    /// `MessageEncode`, and substituted enums to have the "open-enum" structure.
1000    pub fn extern_type_path<P1: AsRef<str>, P2: AsRef<str>>(
1001        &mut self,
1002        proto_path: P1,
1003        rust_path: P2,
1004    ) -> &mut Self {
1005        assert!(
1006            proto_path.as_ref().starts_with('.'),
1007            "Fully-qualified Proto path must start with '.'"
1008        );
1009        self.extern_paths.insert(
1010            proto_path.as_ref().to_owned(),
1011            syn::parse_str(rust_path.as_ref()).expect("failed to tokenize extern path"),
1012        );
1013        self
1014    }
1015
1016    /// Determines whether to generate code to calculate the `MAX_SIZE` constant on each message.
1017    ///
1018    /// By default, `micropb-gen` generates code to calculate the `MAX_SIZE` associated constant
1019    /// for each message struct, which determines the max buffer size needed to encode it. If this
1020    /// is set to false, then it replaces the calculations with `None`, effectively disabling the
1021    /// use of `MAX_SIZE`. This has no runtime impact, but it can reduce the size of the output
1022    /// file.
1023    pub fn calculate_max_size(&mut self, flag: bool) -> &mut Self {
1024        self.calculate_max_size = flag;
1025        self
1026    }
1027
1028    /// Determines whether the modules names generated from package specifiers are suffixed with an
1029    /// underscore.
1030    ///
1031    /// This is on by default. Even when off, module names like "super" and modules created from
1032    /// from message names will still be suffixed.
1033    pub fn suffixed_package_names(&mut self, suffixed: bool) -> &mut Self {
1034        self.suffixed_package_names = suffixed;
1035        self
1036    }
1037
1038    /// For messages with only a single oneof and no other fields, generate an enum representing
1039    /// the oneof rather than a struct.
1040    ///
1041    /// # Example
1042    ///
1043    /// Given the following message:
1044    /// ```proto
1045    /// message Number {
1046    ///     oneof inner {
1047    ///         sint32 signed = 1;
1048    ///         uint32 unsigned = 2;
1049    ///         float fraction = 3;
1050    ///     }
1051    /// }
1052    /// ```
1053    ///
1054    /// The following enum type will be generated:
1055    /// ```no_run
1056    /// pub enum Number {
1057    ///     Signed(i32),
1058    ///     Unsigned(u32),
1059    ///     Fraction(f32),
1060    ///     None,
1061    /// }
1062    /// ```
1063    ///
1064    /// All other message structures, including those with multiple oneofs or a single oneof plus
1065    /// normal fields, will be generated as normal message structs.
1066    ///
1067    /// # Ignored configs
1068    ///
1069    /// With this option, configurations that apply to the oneof itself (`.Number.inner`) will be
1070    /// ignored. Also, [`unknown_handler`](Config::unknown_handler) will be ignored.
1071    pub fn single_oneof_msg_as_enum(&mut self, as_enum: bool) -> &mut Self {
1072        self.single_oneof_msg_as_enum = as_enum;
1073        self
1074    }
1075}
1076
1077fn split_pkg_name(name: &str) -> impl Iterator<Item = &str> {
1078    // ignore empty segments, so empty pkg name points to root node
1079    name.split('.').filter(|seg| !seg.is_empty())
1080}
1081
1082fn split_dot_prefixed_pkg_name(mut name: &str) -> impl Iterator<Item = &str> {
1083    if name.starts_with('.') {
1084        name = &name[1..];
1085    }
1086    split_pkg_name(name)
1087}