risingwave_connector_codec/common/protobuf/
compiler.rs1use std::collections::HashMap;
16
17use prost_types::FileDescriptorSet;
18use protox::Error;
19use protox::file::{ChainFileResolver, File, FileResolver, GoogleFileResolver};
20
21pub fn compile_pb(
23 main_file: (String, String),
24 dependencies: impl IntoIterator<Item = (String, String)>,
25) -> Result<FileDescriptorSet, Error> {
26 struct MyResolver {
27 map: HashMap<String, String>,
28 }
29
30 impl MyResolver {
31 fn new(
32 main_file: (String, String),
33 dependencies: impl IntoIterator<Item = (String, String)>,
34 ) -> Self {
35 let map = std::iter::once(main_file).chain(dependencies).collect();
36
37 Self { map }
38 }
39 }
40
41 impl FileResolver for MyResolver {
42 fn open_file(&self, name: &str) -> Result<File, Error> {
43 if let Some(content) = self.map.get(name) {
44 Ok(File::from_source(name, content)?)
45 } else {
46 Err(Error::file_not_found(name))
47 }
48 }
49 }
50
51 let main_file_name = main_file.0.clone();
52
53 let mut resolver = ChainFileResolver::new();
54 resolver.add(GoogleFileResolver::new());
55 resolver.add(MyResolver::new(main_file, dependencies));
56
57 let fd = protox::Compiler::with_file_resolver(resolver)
58 .include_imports(true)
59 .open_file(&main_file_name)?
60 .file_descriptor_set();
61
62 Ok(fd)
63}