1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#![deny(missing_docs)]
#![deny(rustdoc::broken_intra_doc_links)]
extern crate protobuf;
extern crate protobuf_codegen;
use std::path::Path;
use std::path::PathBuf;
use std::process;
pub use protobuf_codegen::Customize;
use protobuf_parse::pure;
#[derive(Debug, Default)]
pub struct Codegen {
out_dir: PathBuf,
includes: Vec<PathBuf>,
inputs: Vec<PathBuf>,
customize: Customize,
}
impl Codegen {
pub fn new() -> Self {
Self::default()
}
pub fn out_dir(&mut self, out_dir: impl AsRef<Path>) -> &mut Self {
self.out_dir = out_dir.as_ref().to_owned();
self
}
pub fn include(&mut self, include: impl AsRef<Path>) -> &mut Self {
self.includes.push(include.as_ref().to_owned());
self
}
pub fn includes(&mut self, includes: impl IntoIterator<Item = impl AsRef<Path>>) -> &mut Self {
for include in includes {
self.include(include);
}
self
}
pub fn input(&mut self, input: impl AsRef<Path>) -> &mut Self {
self.inputs.push(input.as_ref().to_owned());
self
}
pub fn inputs(&mut self, inputs: impl IntoIterator<Item = impl AsRef<Path>>) -> &mut Self {
for input in inputs {
self.input(input);
}
self
}
pub fn customize(&mut self, customize: Customize) -> &mut Self {
self.customize = customize;
self
}
pub fn run(&self) -> anyhow::Result<()> {
let p = pure::parse_and_typecheck(&self.includes, &self.inputs)?;
protobuf_codegen::gen_and_write(
&p.file_descriptors,
&format!("protobuf-codegen-pure={}", env!("CARGO_PKG_VERSION")),
&p.relative_paths,
&self.out_dir,
&self.customize,
)
}
pub fn run_from_script(&self) {
if let Err(e) = self.run() {
eprintln!("pure rust codegen failed: {}", e);
process::exit(1);
}
}
}