Skip to content

Commit 2e64d6f

Browse files
authored
prost-build: prost_path helper (#1018)
Use the helper function to replace repetitive expressions obtaining prost library path from the configuration with fallback to "::prost".
1 parent 38a00d8 commit 2e64d6f

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

prost-build/src/code_generator.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ fn push_indent(buf: &mut String, depth: u8) {
4444
buf.push_str(" ");
4545
}
4646
}
47+
48+
fn prost_path(config: &Config) -> &str {
49+
config.prost_path.as_deref().unwrap_or("::prost")
50+
}
51+
4752
impl<'a> CodeGenerator<'a> {
4853
pub fn generate(
4954
config: &mut Config,
@@ -183,7 +188,7 @@ impl<'a> CodeGenerator<'a> {
183188
.push_str("#[allow(clippy::derive_partial_eq_without_eq)]\n");
184189
self.buf.push_str(&format!(
185190
"#[derive(Clone, PartialEq, {}::Message)]\n",
186-
self.config.prost_path.as_deref().unwrap_or("::prost")
191+
prost_path(self.config)
187192
));
188193
self.append_skip_debug(&fq_message_name);
189194
self.push_indent();
@@ -473,7 +478,7 @@ impl<'a> CodeGenerator<'a> {
473478
self.buf.push_str(&to_snake(field.name()));
474479
self.buf.push_str(": ");
475480

476-
let prost_path = self.config.prost_path.as_deref().unwrap_or("::prost");
481+
let prost_path = prost_path(self.config);
477482

478483
if repeated {
479484
self.buf
@@ -594,7 +599,7 @@ impl<'a> CodeGenerator<'a> {
594599
.push_str("#[allow(clippy::derive_partial_eq_without_eq)]\n");
595600
self.buf.push_str(&format!(
596601
"#[derive(Clone, PartialEq, {}::Oneof)]\n",
597-
self.config.prost_path.as_deref().unwrap_or("::prost")
602+
prost_path(self.config)
598603
));
599604
self.append_skip_debug(&fq_message_name);
600605
self.push_indent();
@@ -712,7 +717,7 @@ impl<'a> CodeGenerator<'a> {
712717
self.buf.push_str(&format!(
713718
"#[derive(Clone, Copy, {}PartialEq, Eq, Hash, PartialOrd, Ord, {}::Enumeration)]\n",
714719
dbg,
715-
self.config.prost_path.as_deref().unwrap_or("::prost"),
720+
prost_path(self.config),
716721
));
717722
self.push_indent();
718723
self.buf.push_str("#[repr(i32)]\n");
@@ -924,8 +929,6 @@ impl<'a> CodeGenerator<'a> {
924929
}
925930

926931
fn resolve_type(&self, field: &FieldDescriptorProto, fq_message_name: &str) -> String {
927-
let prost_path = self.config.prost_path.as_deref().unwrap_or("::prost");
928-
929932
match field.r#type() {
930933
Type::Float => String::from("f32"),
931934
Type::Double => String::from("f64"),
@@ -934,7 +937,7 @@ impl<'a> CodeGenerator<'a> {
934937
Type::Int32 | Type::Sfixed32 | Type::Sint32 | Type::Enum => String::from("i32"),
935938
Type::Int64 | Type::Sfixed64 | Type::Sint64 => String::from("i64"),
936939
Type::Bool => String::from("bool"),
937-
Type::String => format!("{}::alloc::string::String", prost_path),
940+
Type::String => format!("{}::alloc::string::String", prost_path(self.config)),
938941
Type::Bytes => self
939942
.config
940943
.bytes_type

0 commit comments

Comments
 (0)