Skip to content

Commit

Permalink
Allow loading file descriptor sets again
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewhickman committed Jun 5, 2023
1 parent 06dd787 commit 6f996d2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
24 changes: 12 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 24 additions & 2 deletions src/protoc.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
use std::path::Path;
use std::{ffi::OsStr, path::Path};

use anyhow::{bail, Result};
use prost_reflect::DescriptorPool;

pub fn load_file(path: &Path) -> Result<DescriptorPool> {
match compile(path) {
Ok(pool) => Ok(pool),
Err(err) if err.is_parse() => match load(path) {
Ok(pool) => Ok(pool),
Err(_) => {
if path.extension() == Some(OsStr::new("proto")) {
bail!("{:?}", err)
} else {
bail!("failed to parse '{}' as either a protobuf source file or encoded file descriptor set: {}", path.display(), err)
}
}
},
Err(err) => bail!("{:?}", err),
}
}

fn compile(path: &Path) -> Result<DescriptorPool, protox::Error> {
match path.parent() {
Some(include) => Ok(protox::Compiler::new([include])?
.include_imports(true)
.include_source_info(false)
.open_file(path)?
.descriptor_pool()),
None => bail!("invalid path"),
None => Err(protox::Error::new("invalid path")),
}
}

fn load(path: &Path) -> Result<DescriptorPool> {
let bytes = fs_err::read(path)?;
DescriptorPool::decode(bytes.as_slice()).map_err(|err| anyhow::anyhow!("{:?}", err))
}

0 comments on commit 6f996d2

Please sign in to comment.