-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow loading file descriptor sets again
- Loading branch information
1 parent
06dd787
commit 6f996d2
Showing
2 changed files
with
36 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |