Skip to content

Commit a13e6cf

Browse files
committed
Proto file names no longer need to match package names when automatically finding config files
1 parent f3884eb commit a13e6cf

5 files changed

Lines changed: 50 additions & 46 deletions

File tree

micropb-gen/src/generator.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,18 @@ impl<'proto> Context<'proto> {
162162
}
163163

164164
pub(crate) fn generate_fdset(
165-
generator: Generator,
165+
#[allow(unused_mut)] mut generator: Generator,
166166
fdset: &'proto FileDescriptorSet,
167+
#[allow(unused)] find_config_files: bool,
167168
) -> crate::Result<TokenStream> {
168-
// Pre-generate the comment trees for every file
169+
// Pre-generate the comment trees and configs for every file
169170
let mut comment_trees = vec![];
170171
for file in &fdset.file {
172+
#[cfg(feature = "config-file")]
173+
if find_config_files && let Some(name) = file.name() {
174+
generator.parse_config_from_proto(std::path::Path::new(name), file.package())?;
175+
}
176+
171177
let mut comment_tree = PathTree::new(Comments::default());
172178
if let Some(src) = file.source_code_info() {
173179
for location in &src.location {

micropb-gen/src/lib.rs

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -886,48 +886,37 @@ impl Generator {
886886
self
887887
}
888888

889+
#[cfg(feature = "config-file")]
890+
pub(crate) fn parse_config_from_proto(
891+
&mut self,
892+
proto_path: &Path,
893+
pkg: Option<&String>,
894+
) -> Result<()> {
895+
let toml_path = proto_path.with_extension("toml");
896+
let pkg_path = match pkg {
897+
Some(pkg) => format!(".{pkg}"),
898+
None => ".".to_owned(),
899+
};
900+
match self.parse_config_file(&toml_path, &pkg_path) {
901+
// If config file doesn't exist then just assume there's no configs
902+
Err(Error::Io(err)) if err.kind() == std::io::ErrorKind::NotFound => Ok(()),
903+
Err(err) => Err(err),
904+
Ok(()) => Ok(()),
905+
}
906+
}
907+
889908
/// Compile `.proto` files and configuration files into a single Rust file.
890909
///
891910
/// Configuration files are derived from the proto files by replacing the file extension with
892911
/// `.toml`. For example, if `server.proto` is passed in, the generator will look for the
893-
/// `server.toml` config file and apply it to the `.server` package.
894-
///
895-
/// <div class="warning">
896-
/// The package name of each proto file must match the file name, because that's what the
897-
/// generator assumes when applying the config files. For example, `client.rpc.proto` must
898-
/// contain the specifier `package client.rpc;`.
899-
/// </div>
912+
/// `server.toml` config file and apply it to the package specified in `server.proto`.
900913
#[cfg(feature = "config-file")]
901914
pub fn compile_protos_with_config_files(
902-
mut self,
915+
self,
903916
protos: &[impl AsRef<Path>],
904917
out_filename: impl AsRef<Path>,
905918
) -> Result<()> {
906-
for proto_path in protos {
907-
let proto_path = proto_path.as_ref();
908-
if let Some(stem) = proto_path.file_stem().and_then(OsStr::to_str) {
909-
let pkg = if stem.starts_with('.') {
910-
stem.to_owned()
911-
} else {
912-
format!(".{stem}")
913-
};
914-
let toml_path = proto_path.with_extension("toml");
915-
916-
match self.parse_config_file(&toml_path, &pkg) {
917-
// If config file doesn't exist then just assume there's no configs
918-
Err(Error::Io(err)) if err.kind() == std::io::ErrorKind::NotFound => {}
919-
Err(err) => return Err(err),
920-
Ok(()) => {}
921-
}
922-
} else {
923-
(self.warning_cb)(format_args!(
924-
"Couldn't derive config file path from {}",
925-
proto_path.display()
926-
));
927-
}
928-
}
929-
930-
self.compile_protos(protos, out_filename)
919+
self.compile_protos_inner(protos, out_filename.as_ref(), true)
931920
}
932921

933922
/// Compile `.proto` files into a single Rust file.
@@ -945,6 +934,15 @@ impl Generator {
945934
self,
946935
protos: &[impl AsRef<Path>],
947936
out_filename: impl AsRef<Path>,
937+
) -> Result<()> {
938+
self.compile_protos_inner(protos, out_filename.as_ref(), false)
939+
}
940+
941+
fn compile_protos_inner(
942+
self,
943+
protos: &[impl AsRef<Path>],
944+
out_filename: &Path,
945+
find_config_files: bool,
948946
) -> Result<()> {
949947
let tmp;
950948
let fdset_file = if let Some(fdset_path) = &self.fdset_path {
@@ -978,7 +976,7 @@ impl Generator {
978976
));
979977
}
980978

981-
self.compile_fdset_file(fdset_file, out_filename)
979+
self.compile_fdset_file_inner(fdset_file.as_path(), out_filename, find_config_files)
982980
}
983981

984982
/// Compile a Protobuf file descriptor set into a Rust file.
@@ -989,6 +987,15 @@ impl Generator {
989987
self,
990988
fdset_file: impl AsRef<Path>,
991989
out_filename: impl AsRef<Path>,
990+
) -> crate::Result<()> {
991+
self.compile_fdset_file_inner(fdset_file.as_ref(), out_filename.as_ref(), false)
992+
}
993+
994+
fn compile_fdset_file_inner(
995+
self,
996+
fdset_file: &Path,
997+
out_filename: &Path,
998+
find_config_files: bool,
992999
) -> crate::Result<()> {
9931000
#[allow(unused)]
9941001
let format = self.format;
@@ -999,7 +1006,7 @@ impl Generator {
9991006
fdset
10001007
.decode(&mut decoder, bytes.len())
10011008
.expect("file descriptor set decode failed");
1002-
let code = Context::generate_fdset(self, &fdset)?;
1009+
let code = Context::generate_fdset(self, &fdset, find_config_files)?;
10031010

10041011
#[cfg(feature = "format")]
10051012
let output = if format {

tests/basic-proto/build.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::path::Path;
2-
31
use micropb_gen::{
42
config::{CustomField, IntSize, OptionalRepr},
53
Config, EncodeDecode, Generator,
@@ -535,19 +533,12 @@ fn minimal_accessors() {
535533
fn with_config_file() {
536534
let mut generator = Generator::new();
537535
generator.use_container_heapless();
538-
generator
539-
.parse_config_file(Path::new("proto/my.collections.toml"), ".")
540-
.unwrap();
541-
generator
542-
.parse_config_file(Path::new("proto/my.map.toml"), ".")
543-
.unwrap();
544536

545537
generator
546538
.compile_protos_with_config_files(
547539
&[
548540
"proto/collections.proto",
549541
"proto/map.proto",
550-
// Only basic.toml will be picked up my this function
551542
"proto/basic.proto",
552543
],
553544
std::env::var("OUT_DIR").unwrap() + "/with_config_file.rs",
File renamed without changes.

0 commit comments

Comments
 (0)