Skip to content

Commit 944a6c4

Browse files
Feature: Add folder support to .proto parser (#273)
* Feat: Add folder support to .proto parser. * [autofix.ci] apply automated fixes * Fix: Proper error handling --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 3c62922 commit 944a6c4

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

mitmproxy-contentviews/src/protobuf/existing_proto_definitions.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,32 @@ impl std::fmt::Display for RpcInfo {
151151
fn parse_file_descriptor_set(definitions_path: &Path) -> anyhow::Result<Vec<FileDescriptor>> {
152152
let mut parser = Parser::new();
153153
parser.pure();
154-
if let Some(parent) = definitions_path.parent() {
155-
parser.include(parent);
154+
if definitions_path.is_dir() {
155+
walk_proto_directory(definitions_path, &mut parser)?;
156+
} else {
157+
if let Some(parent) = definitions_path.parent() {
158+
parser.include(parent);
159+
}
160+
parser.input(definitions_path);
156161
}
157-
parser.input(definitions_path);
158162
let fds = parser.file_descriptor_set()?;
159163
FileDescriptor::new_dynamic_fds(fds.file, &[])
160164
.context("failed to create dynamic file descriptors")
161165
}
166+
167+
fn walk_proto_directory(definitions_path: &Path, parser: &mut Parser) -> anyhow::Result<()> {
168+
parser.include(definitions_path);
169+
for entry in definitions_path
170+
.read_dir()
171+
.context("failed to read protobuf directory")?
172+
{
173+
if let Ok(entry) = entry {
174+
if entry.metadata()?.is_dir() {
175+
walk_proto_directory(entry.path().as_path(), parser)?;
176+
} else if entry.file_name().to_string_lossy().ends_with(".proto") {
177+
parser.input(entry.path());
178+
}
179+
}
180+
}
181+
Ok(())
182+
}

0 commit comments

Comments
 (0)