1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
//! `micropb-gen` compiles `.proto` files into Rust code. It is intended to be used inside
//! `build.rs` for build-time code generation.
//!
//! Unlike other Protobuf code generators in the Rust ecosystem, `micropb` is aimed for constrained
//! environments without an allocator.
//!
//! The entry point of this crate is the [`Generator`] type.
//!
//! For info on the "library layer" of `micropb-gen`, see [`micropb`].
//!
//! # Getting Started
//!
//! Add `micropb` crates to your `Cargo.toml`:
//! ```toml
//! [dependencies]
//! # Allow types from `heapless` to be used for container fields
//! micropb = { version = "0.3.0", features = ["container-heapless-0-9"] }
//! heapless = "0.9"
//!
//! [build-dependencies]
//! micropb-gen = "0.3.0"
//! ```
//!
//! Then, place your `.proto` file into the project's root directory:
//! ```proto
//! // example.proto
//! message Example {
//! int32 field1 = 1;
//! bool field2 = 2;
//! double field3 = 3;
//! }
//! ```
//!
//! `micropb-gen` requires `protoc` to build `.proto` files, so [install
//! `protoc`](https://grpc.io/docs/protoc-installation) and add it to your PATH, then invoke the
//! code generator in `build.rs`:
//!
//! ```rust,no_run
//! let mut generator = micropb_gen::Generator::new();
//! // Compile example.proto into a Rust module
//! generator.compile_protos(&["example.proto"], std::env::var("OUT_DIR").unwrap() + "/example.rs").unwrap();
//! ```
//!
//! Finally, include the generated file in your code:
//! ```rust,ignore
//! // main.rs
//! use micropb::{MessageDecode, MessageEncode, PbEncoder};
//!
//! mod example {
//! #![allow(clippy::all)]
//! #![allow(nonstandard_style, unused, irrefutable_let_patterns)]
//! // Let's assume that Example is the only message define in the .proto file that has been
//! // converted into a Rust struct
//! include!(concat!(env!("OUT_DIR"), "/example.rs"));
//! }
//!
//! let example = example::Example {
//! field1: 12,
//! field2: true,
//! field3: 0.234,
//! };
//!
//! // Maximum size of the message type on the wire, scaled to the next power of 2 for heapless::Vec
//! const CAPACITY: usize = example::Example::MAX_SIZE.unwrap().next_power_of_two();
//! // For the example message above we can use a smaller capacity
//! // const CAPACITY: usize = 32;
//!
//! // Use heapless::Vec as the output stream and build an encoder around it
//! let mut encoder = PbEncoder::new(heapless::Vec::<u8, CAPACITY>::new());
//!
//! // Compute the size of the `Example` on the wire
//! let _size = example.compute_size();
//! // Encode the `Example` to the data stream
//! example.encode(&mut encoder).expect("Vec over capacity");
//!
//! // Decode a new instance of `Example` into a new struct
//! let mut new = example::Example::default();
//! let data = encoder.as_writer().as_slice();
//! new.decode_from_bytes(data).expect("decoding failed");
//! assert_eq!(example, new);
//! ```
//!
//! # Messages
//!
//! Protobuf messages are translated directly into Rust structs, and each message field translates into a Rust field.
//!
//! Given the following Protobuf definition:
//! ```proto
//! syntax = "proto3";
//!
//! package example;
//!
//! message Example {
//! int32 f_int32 = 1;
//! int64 f_int64 = 2;
//! uint32 f_uint32 = 3;
//! uint64 f_uint64 = 4;
//! sint32 f_sint32 = 5;
//! sint64 f_sint64 = 6;
//! bool f_bool = 7;
//! fixed32 f_fixed32 = 8;
//! fixed64 f_fixed64 = 9;
//! sfixed32 f_sfixed32 = 10;
//! sfixed64 f_sfixed64 = 11;
//! float f_float = 12;
//! double f_double = 13;
//! }
//! ```
//!
//! `micropb-gen` will generate the following Rust structs and APIs:
//! ```rust,ignore
//! pub mod example_ {
//! #[derive(Debug, Clone, Copy)]
//! pub struct Example {
//! pub f_int32: i32,
//! pub f_int64: i64,
//! pub f_uint32: u32,
//! pub f_uint64: u64,
//! pub f_sint32: i32,
//! pub f_sint64: i64,
//! pub f_bool: bool,
//! pub f_fixed32: u32,
//! pub f_fixed64: u64,
//! pub f_sfixed32: u32,
//! pub f_sfixed64: u64,
//! pub f_float: f32,
//! pub f_double: f64,
//! }
//!
//! impl Example {
//! /// Return reference to f_int32
//! pub fn f_int32(&self) -> &i32;
//! /// Return mutable reference to f_int32
//! pub fn mut_f_int32(&mut self) -> &mut i32;
//! /// Set value of f_int32
//! pub fn set_f_int32(&mut self, val: i32) -> &mut Self;
//! /// Builder method that sets f_int32. Useful for initializing the message.
//! pub fn init_f_int32(mut self, val: i32) -> Self;
//!
//! // Same APIs for the other singular fields
//! }
//!
//! impl Default for Example { /* ... */ }
//!
//! impl PartialEq for Example { /* ... */ }
//!
//! impl micropb::MessageEncode for Example { /* ... */ }
//!
//! impl micropb::MessageDecode for Example { /* ... */ }
//! }
//! ```
//!
//! The generated [`MessageDecode`](micropb::MessageEncode) and
//! [`MessageEncode`](micropb::MessageDecode) implementations provide APIs for decoding, encoding,
//! and computing the size of `Example`.
//!
//! Implementations or derives for `Default`, `Clone`, `PartialEq`, and `Debug` are also provided.
//! `Copy` derives are generated for messages consisting entirely of copyable fields.
//!
//! ## Optional Fields
//!
//! While the obvious choice for representing optional fields is [`Option`], this is not actually
//! ideal in embedded systems because `Option<T>` actually takes up twice as much space as `T` for
//! many types, such as `u32` and `i32`. Instead, **`micropb` tracks the presence of all optional
//! fields of a message in a separate bitfield called a _hazzer_**, which is usually small enough to
//! fit into the padding. Field presence can either be queried directly from the hazzer or from
//! message APIs that return `Option`.
//!
//! For example, given the following Protobuf message:
//! ```proto
//! message Example {
//! optional int32 f_int32 = 1;
//! optional int64 f_int64 = 2;
//! optional bool f_bool = 3;
//! }
//! ```
//!
//! `micropb-gen` generates the following Rust struct and APIs:
//! ```rust,ignore
//! pub struct Example {
//! pub f_int32: i32,
//! pub f_int64: i64,
//! pub f_bool: bool,
//!
//! pub _has: Example_::_Hazzer,
//! }
//!
//! impl Example {
//! /// Return reference to f_int32 as an Option
//! pub fn f_int32(&self) -> Option<&i32>;
//! /// Return mutable reference to f_int32 as an Option
//! pub fn mut_f_int32(&mut self) -> Option<&mut i32>;
//! /// Set value and presence of f_int32
//! pub fn set_f_int32(&mut self, val: i32) -> &mut Self;
//! /// Clear presence of f_int32
//! pub fn clear_f_int32(&mut self) -> &mut Self;
//! /// Take f_int32 and return it
//! pub fn take_f_int32(&mut self) -> Option<i32>;
//! /// Builder method that sets f_int32. Useful for initializing the message.
//! pub fn init_f_int32(mut self, val: i32) -> Self;
//!
//! // Same APIs for other optional fields
//! }
//!
//! pub mod Example_ {
//! /// Tracks whether the optional fields are present
//! #[derive(Debug, Default, Clone, PartialEq, Copy)]
//! pub struct _Hazzer([u8; 1]);
//!
//! impl _Hazzer {
//! /// Create an empty Hazzer with all fields cleared
//! pub const fn _new() -> Self;
//!
//! /// Query presence of f_int32
//! pub const fn f_int32(&self) -> bool;
//! /// Set presence of f_int32
//! pub const fn set_f_int32(&mut self) -> &mut Self;
//! /// Clear presence of f_int32
//! pub const fn clear_f_int32(&mut self) -> &mut Self;
//! /// Builder method that toggles on the presence of f_int32. Useful for initializing the Hazzer.
//! pub const fn init_f_int32(mut self) -> Self;
//!
//! // Same APIs for other optional fields
//! }
//! }
//!
//! // trait impls, decode/encode logic, etc
//! ```
//!
//! ### Note on Initialization
//!
//! **A field will be considered empty (and ignored by the encoder) if its bit in the hazzer is not
//! set, _even if the field itself has been written_.** The following is an easy way to initialize a
//! message with all optional fields set:
//! ```rust,ignore
//! Example::default().init_f_int32(4).init_f_int64(-5).init_f_bool(true)
//! ```
//!
//! Alternatively, we can initialize the message using the constructor:
//! ```rust,ignore
//! Example {
//! f_int32: 4,
//! f_int64: -5,
//! f_bool: true,
//! // initialize the hazzer with all fields set to true
//! // without initializing the hazzer, all fields in Example will be considered unset
//! _has: Example_::_Hazzer::default()
//! .init_f_int32()
//! .init_f_int64()
//! .init_f_bool()
//! }
//! ```
//!
//! ### Fallback to [`Option`]
//!
//! By default, optional fields are represented by bitfields, as shown above. If an optional field
//! is configured to be boxed via [`Config::boxed`], it will instead be represented as an `Option`,
//! because `Option<Box<T>>` doesn't take up extra space compared to `Box<T>`. To override these default
//! behaviours, see [`Config::optional_repr`].
//!
//! ### Required fields
//!
//! The generator treats required fields exactly the same way it treats optional fields.
//!
//! ## Message fields
//!
//! Message fields are generated as the corresponding Rust struct. If the message field has no
//! modifier in `proto3`, it will be treated as an optional field. Cyclical references between
//! parent message types and field types will be broken by automatically boxing the field to
//! prevent infinite-sized structs.
//!
//! ## Oneof Fields
//!
//! Protobuf oneofs are translated into Rust enums. The enum type is defined in an internal
//! module under the message, and its type name is the same as the name of the oneof field.
//!
//! For example, given this Protobuf definition:
//! ```proto
//! message Example {
//! oneof number {
//! int32 int = 1;
//! float decimal = 2;
//! }
//! }
//! ```
//!
//! `micropb-gen` generates the following definition:
//! ```rust,no_run
//! #[derive(Debug, Clone, PartialEq, Copy)]
//! pub struct Example {
//! pub number: Option<Example_::Number>,
//! }
//!
//! pub mod Example_ {
//! #[derive(Debug, Clone, PartialEq, Copy)]
//! pub enum Number {
//! Int(i32),
//! Decimal(f32),
//! }
//! }
//! ```
//!
//! ## Repeated, `map`, `string`, and `bytes` Fields
//!
//! Repeated, `map`, `string`, and `bytes` fields need to be represented as Rust "container" types,
//! since they contain multiple elements or bytes. Normally standard types like `String` and `Vec`
//! are used, but they aren't available in no-alloc environments. Instead, we need stack-allocated
//! containers with fixed capacity. Since there is no defacto standard for such containers in Rust,
//! **users are expected to configure the code generator with their own container types** (see
//! [`Config`] for more details).
//!
//! For example, given the following Protobuf definition:
//! ```proto
//! message Containers {
//! string f_string = 1;
//! bytes f_bytes = 2;
//! repeated int32 f_repeated = 3;
//! map<int32, int64> f_map = 4;
//! }
//! ```
//!
//! and the following configuration in `build.rs`:
//! ```rust,no_run
//! let mut generator = micropb_gen::Generator::new();
//! // Configure our own container types
//! generator.configure(".",
//! micropb_gen::Config::new()
//! .string_type("crate::MyString<$N>")
//! .bytes_type("crate::MyVec<u8, $N>")
//! .vec_type("crate::MyVec<$T, $N>")
//! .map_type("crate::MyMap<$K, $V, $N>")
//! );
//!
//! // We can also use container types from `heapless`, which have fixed capacity
//! generator.use_container_heapless();
//!
//! // Same shorthand exists for containers from `arrayvec` or `alloc`
//! // generator.use_container_arrayvec();
//! // generator.use_container_alloc();
//!
//!
//! // Since we're using fixed containers, we need to specify the max capacity of each field.
//! // For simplicity, configure capacity of all repeated/map fields to 4 and string/bytes to 8.
//! generator.configure(".", micropb_gen::Config::new().max_len(4).max_bytes(8));
//! ```
//!
//! The following Rust struct will be generated:
//! ```rust,no_run
//! pub struct Containers {
//! f_string: heapless::String<8>,
//! f_bytes: heapless::Vec<u8, 8>,
//! f_repeated: heapless::Vec<i32, 4>,
//! f_map: heapless::index_map::FnvIndexMap<i32, i64, 4>,
//! }
//! ```
//!
//! For **decoding**, container types should implement [`PbVec`](micropb::PbVec) (repeated fields),
//! [`PbString`](micropb::PbString), [`PbBytes`](micropb::PbBytes), or [`PbMap`](micropb::PbMap)
//! For convenience, [`micropb`] comes with built-in implementations of the container traits for
//! types from [`heapless`](https://docs.rs/heapless/latest/heapless),
//! [`arrayvec`](https://docs.rs/arrayvec/latest/arrayvec), and
//! [`alloc`](https://doc.rust-lang.org/alloc), as well as implementations on `[u8; N]` arrays and
//! [`FixedLenString`](micropb::FixedLenString).
//!
//! For **encoding**, container types need to dereference into `&[T]` (repeated fields), `&str`, or
//! `&[u8]`. Maps just need to iterate through key-value pairs.
//!
//! ## Message Lifetime
//!
//! A message struct may have up to one lifetime parameter. `micropb-gen` automatically generates
//! the lifetime parameter for each message by checking if there's a lifetime in any of the fields.
//!
//! For example, given the Protobuf file from the previous section and the following `build.rs`
//! config:
//! ```rust,no_run
//! # use micropb_gen::{Generator, Config, config::CustomField};
//! # let mut generator = Generator::new();
//! // Use `Cow` as container type with lifetime of 'a
//! generator.configure(".",
//! Config::new()
//! .string_type("alloc::borrow::Cow<'a, str>")
//! .bytes_type("alloc::borrow::Cow<'a, [u8]>")
//! .vec_type("alloc::borrow::Cow<'a, [$T]>")
//! );
//! // Use a custom type for the `f_map` field, also with lifetime of 'a
//! generator.configure(".Containers.f_map",
//! Config::new().custom_field(CustomField::from_type("MyField<'a>"))
//! );
//! ```
//!
//! `micropb-gen` generates the following struct:
//! ```rust,no_run
//! # extern crate alloc;
//! # struct MyField<'a>(&'a u8);
//! pub struct Containers<'a> {
//! f_string: alloc::borrow::Cow<'a, str>,
//! f_bytes: alloc::borrow::Cow<'a, [u8]>,
//! f_repeated: alloc::borrow::Cow<'a, [i32]>,
//! f_map: MyField<'a>,
//! }
//! ```
//!
//! Note that message types can only have a single lifetime, so don't mix multiple lifetime
//! identifiers in your configuration.
//!
//! # Enums
//!
//! Protobuf enums are translated into "open" enums in Rust, rather than normal Rust enums. This is
//! because proto3 requires enums to store unrecognized values, which is only possible with open
//! enums.
//!
//! For example, given this Protobuf enum:
//! ```proto
//! enum Language {
//! RUST = 0,
//! C = 1,
//! CPP = 2,
//! }
//! ```
//!
//! `micropb-gen` generates the following Rust definition:
//! ```rust,ignore
//! #[derive(Debug, Clone, Default, Copy, PartialEq, Eq, Hash)]
//! #[repr(transparent)]
//! pub struct Language(pub i32);
//!
//! impl Language {
//! // Default value
//! pub const Rust: Self = Self(0);
//! pub const C: Self = Self(1);
//! pub const Cpp: Self = Self(2);
//! }
//!
//! impl From<i32> for Language { /* .. */ }
//! ```
//!
//! # Packages and Modules
//!
//! `micropb-gen` translates Protobuf package names into Rust modules by appending an underscore.
//!
//! For example, given the following Protobuf file:
//! ```proto
//! package foo.bar;
//!
//! // Protobuf contents
//! ```
//!
//! The generated Rust file will look like:
//! ```rust,ignore
//! pub mod foo_ {
//! pub mod bar_ {
//! // Generated code lives here
//! }
//! }
//! ```
//!
//! If a Protobuf file does not have a package specifier, the generated code will instead live in
//! the root module
//!
//! Message names are also translated into Rust modules by appending an underscore. For example,
//! code generated from oneofs and nested messages within the `Name` message will live in the
//! `Name_` module.
//!
//! # Configuring the Generator
//!
//! One of `micropb-gen`'s main features is its granular configuration system, which allows users
//! to control how code is generated at the level of the module, message, or even individual
//! fields. See [`Generator::configure`] and [`Config`] for more info on the configuration system.
//!
//! ## Notable Configurations
//!
//! - **Integer size**: Controls the width of the integer types used to represent [integer
//! fields](Config::int_size). This can also be done for [enums](Config::enum_int_size).
//!
//! - **Attributes**: Apply custom attributes to [fields](Config::field_attributes) and
//! [messages](Config::type_attributes).
//!
//! - **Custom fields**: Substitute your own type into the generated code, allowing complete
//! control over the encode and decode behaviour. Can be applied to [normal
//! fields](Config::custom_field) or [unknown fields](Config::unknown_handler).
//!
//! - **Max container size**: Specify the max capacity of [`string`/`bytes`
//! fields](Config::max_bytes) as well as [repeated fields](Config::max_len), which is necessary
//! when using fixed-capacity containers like `ArrayVec`.
//!
//! ## Configuration Files
//!
//! Configurations can be stored in TOML files rather than in `build.rs`. See
//! [`Generator::parse_config_file`] for more info.
pub
// This module was generated from example/file-descriptor-proto
use ;
pub use Config;
pub use ;
use ;
use PathTree;
use TokenStream;
use crateContext;
/// Whether to include encode and decode logic
type WarningCb = fn;
/// Protobuf code generator
///
/// Use this in `build.rs` to compile `.proto` files into a Rust module.
///
/// The main way to control the compilation process is to call [`configure`](Generator::configure),
/// which allows the user to customize how code is generated from Protobuf types and fields of
/// their choosing.
///
/// # Note
/// It's recommended to call one of [`use_container_alloc`](Self::use_container_alloc),
/// [`use_container_heapless`](Self::use_container_heapless), or
/// [`use_container_alloc`](Self::use_container_alloc) to ensure that container types are
/// configured for `string`, `bytes`, repeated, and `map` fields. The generator will throw an
/// error if it reaches any such field that doesn't have a container configured.
///
/// # Example
/// ```no_run
/// use micropb_gen::{Generator, Config};
///
/// let mut generator = Generator::new();
/// // Use container types from `heapless`
/// generator.use_container_heapless()
/// // Set max length of repeated fields in .test.Data to 4
/// .configure(".test.Data", Config::new().max_len(4))
/// // Wrap .test.Data.value inside a Box
/// .configure(".test.Data.value", Config::new().boxed(true));
/// // Compile test.proto into a Rust module
/// generator.compile_protos(
/// &["test.proto"],
/// std::env::var("OUT_DIR").unwrap() + "/test_proto.rs",
/// )
/// .unwrap();
/// ```