Skip to content

Commit b0d14cc

Browse files
committed
move vendored functionalities to more descriptive modules
move the different functionalities vendored from older protobuf versions into more descriptive modules within the util module. Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>
1 parent 5799985 commit b0d14cc

File tree

5 files changed

+94
-91
lines changed

5 files changed

+94
-91
lines changed

compiler/src/codegen.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ use std::{
4343
};
4444

4545
use crate::{
46-
vendored::{proto_path_to_rust_mod, CodeWriter, RootScope},
47-
Customize,
46+
util::proto_path_to_rust_mod, util::scope::RootScope, util::writer::CodeWriter, Customize,
4847
};
4948
use protobuf::{
5049
descriptor::*,

compiler/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
pub mod codegen;
2727
pub mod prost_codegen;
2828
mod util;
29-
mod vendored;
3029

3130
/// Customize generated code.
3231
#[derive(Default, Debug, Clone)]

compiler/src/util.rs renamed to compiler/src/util/mod.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
use std::fmt;
1717
use std::str;
1818

19-
use crate::vendored::CodeWriter;
19+
pub mod scope;
20+
pub mod writer;
21+
22+
use writer::CodeWriter;
2023

2124
// A struct that divide a name into serveral parts that meets rust's guidelines.
2225
struct NameSpliter<'a> {
@@ -132,6 +135,15 @@ where
132135
async_fn_block(w, false, sig, cb);
133136
}
134137

138+
// proto_name_to_rs is constructor as "{proto_path_to_rust_mod}.rs"
139+
// see https://github.com/stepancheg/rust-protobuf/blob/v3.7.2/protobuf-codegen/src/gen/paths.rs#L43
140+
pub fn proto_path_to_rust_mod(path: &str) -> String {
141+
protobuf_codegen::proto_name_to_rs(path)
142+
.strip_suffix(".rs")
143+
.unwrap()
144+
.to_string()
145+
}
146+
135147
pub enum MethodType {
136148
Unary,
137149
ClientStreaming,

compiler/src/vendored.rs renamed to compiler/src/util/scope.rs

Lines changed: 1 addition & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,6 @@
33
//! The missing functionalities have been reimplemented in this module.
44
55
use protobuf::descriptor::{DescriptorProto, FileDescriptorProto};
6-
use protobuf_codegen::proto_name_to_rs;
7-
8-
// proto_name_to_rs is constructor as "{proto_path_to_rust_mod}.rs"
9-
// see https://github.com/stepancheg/rust-protobuf/blob/v3.7.2/protobuf-codegen/src/gen/paths.rs#L43
10-
pub fn proto_path_to_rust_mod(path: &str) -> String {
11-
proto_name_to_rs(path)
12-
.strip_suffix(".rs")
13-
.unwrap()
14-
.to_string()
15-
}
166

177
// vendered from https://github.com/stepancheg/rust-protobuf/blob/v3.7.2/protobuf-codegen/src/gen/rust/keywords.rs
188
fn is_rust_keyword(ident: &str) -> bool {
@@ -78,82 +68,6 @@ fn is_rust_keyword(ident: &str) -> bool {
7868
RUST_KEYWORDS.contains(&ident)
7969
}
8070

81-
// adapted from https://github.com/stepancheg/rust-protobuf/blob/v3.7.2/protobuf-codegen/src/gen/code_writer.rs#L12
82-
#[derive(Default)]
83-
pub struct CodeWriter {
84-
writer: String,
85-
indent: String,
86-
}
87-
88-
impl CodeWriter {
89-
pub fn new() -> CodeWriter {
90-
Self::default()
91-
}
92-
93-
pub fn code(&self) -> &str {
94-
&self.writer
95-
}
96-
97-
pub fn take_code(&mut self) -> String {
98-
std::mem::take(&mut self.writer)
99-
}
100-
101-
pub fn write_line(&mut self, line: impl AsRef<str>) {
102-
if line.as_ref().is_empty() {
103-
self.writer.push('\n');
104-
} else {
105-
self.writer.push_str(&self.indent);
106-
self.writer.push_str(line.as_ref());
107-
self.writer.push('\n');
108-
}
109-
}
110-
111-
pub fn block(
112-
&mut self,
113-
first_line: impl AsRef<str>,
114-
last_line: impl AsRef<str>,
115-
cb: impl FnOnce(&mut CodeWriter),
116-
) {
117-
self.write_line(first_line);
118-
self.indented(cb);
119-
self.write_line(last_line);
120-
}
121-
122-
pub fn expr_block(&mut self, prefix: impl AsRef<str>, cb: impl FnOnce(&mut CodeWriter)) {
123-
self.block(format!("{} {{", prefix.as_ref()), "}", cb);
124-
}
125-
126-
pub fn indented(&mut self, cb: impl FnOnce(&mut CodeWriter)) {
127-
self.indent.push_str(" ");
128-
cb(self);
129-
self.indent.truncate(self.indent.len() - 4);
130-
}
131-
132-
pub fn pub_fn(&mut self, sig: impl AsRef<str>, cb: impl FnOnce(&mut CodeWriter)) {
133-
self.expr_block(format!("pub fn {}", sig.as_ref()), cb)
134-
}
135-
136-
pub fn def_fn(&mut self, sig: impl AsRef<str>, cb: impl FnOnce(&mut CodeWriter)) {
137-
self.expr_block(format!("fn {}", sig.as_ref()), cb)
138-
}
139-
140-
pub fn pub_struct(&mut self, name: impl AsRef<str>, cb: impl FnOnce(&mut CodeWriter)) {
141-
self.expr_block(format!("pub struct {}", name.as_ref()), cb);
142-
}
143-
144-
pub fn field_decl(&mut self, name: impl AsRef<str>, field_type: impl AsRef<str>) {
145-
self.write_line(format!("{}: {},", name.as_ref(), field_type.as_ref()));
146-
}
147-
148-
pub fn impl_self_block(&mut self, name: impl AsRef<str>, cb: impl FnOnce(&mut CodeWriter)) {
149-
self.expr_block(format!("impl {}", name.as_ref()), cb);
150-
}
151-
152-
pub fn pub_trait(&mut self, name: impl AsRef<str>, cb: impl FnOnce(&mut CodeWriter)) {
153-
self.expr_block(format!("pub trait {}", name.as_ref()), cb);
154-
}
155-
}
156-
15771
// reimplementation based on https://github.com/stepancheg/rust-protobuf/blob/v3.7.2/protobuf-codegen/src/gen/scope.rs#L26
15872
// it only implements the `find_message` method with not extra dependencies
15973
pub struct RootScope<'a> {
@@ -198,7 +112,7 @@ impl ScopedMessage<'_> {
198112
pub fn rust_fq_name(&self) -> String {
199113
format!(
200114
"{}::{}",
201-
proto_path_to_rust_mod(self.fd.name()),
115+
super::proto_path_to_rust_mod(self.fd.name()),
202116
self.rust_name()
203117
)
204118
}

compiler/src/util/writer.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//! This module contains functionalities that where previously available in
2+
//! the protobuf / protobuf-codegen crates, but were then removed.
3+
//! The missing functionalities have been reimplemented in this module.
4+
5+
// adapted from https://github.com/stepancheg/rust-protobuf/blob/v3.7.2/protobuf-codegen/src/gen/code_writer.rs#L12
6+
#[derive(Default)]
7+
pub struct CodeWriter {
8+
writer: String,
9+
indent: String,
10+
}
11+
12+
impl CodeWriter {
13+
pub fn new() -> CodeWriter {
14+
Self::default()
15+
}
16+
17+
pub fn code(&self) -> &str {
18+
&self.writer
19+
}
20+
21+
pub fn take_code(&mut self) -> String {
22+
std::mem::take(&mut self.writer)
23+
}
24+
25+
pub fn write_line(&mut self, line: impl AsRef<str>) {
26+
if line.as_ref().is_empty() {
27+
self.writer.push('\n');
28+
} else {
29+
self.writer.push_str(&self.indent);
30+
self.writer.push_str(line.as_ref());
31+
self.writer.push('\n');
32+
}
33+
}
34+
35+
pub fn block(
36+
&mut self,
37+
first_line: impl AsRef<str>,
38+
last_line: impl AsRef<str>,
39+
cb: impl FnOnce(&mut CodeWriter),
40+
) {
41+
self.write_line(first_line);
42+
self.indented(cb);
43+
self.write_line(last_line);
44+
}
45+
46+
pub fn expr_block(&mut self, prefix: impl AsRef<str>, cb: impl FnOnce(&mut CodeWriter)) {
47+
self.block(format!("{} {{", prefix.as_ref()), "}", cb);
48+
}
49+
50+
pub fn indented(&mut self, cb: impl FnOnce(&mut CodeWriter)) {
51+
self.indent.push_str(" ");
52+
cb(self);
53+
self.indent.truncate(self.indent.len() - 4);
54+
}
55+
56+
pub fn pub_fn(&mut self, sig: impl AsRef<str>, cb: impl FnOnce(&mut CodeWriter)) {
57+
self.expr_block(format!("pub fn {}", sig.as_ref()), cb)
58+
}
59+
60+
pub fn def_fn(&mut self, sig: impl AsRef<str>, cb: impl FnOnce(&mut CodeWriter)) {
61+
self.expr_block(format!("fn {}", sig.as_ref()), cb)
62+
}
63+
64+
pub fn pub_struct(&mut self, name: impl AsRef<str>, cb: impl FnOnce(&mut CodeWriter)) {
65+
self.expr_block(format!("pub struct {}", name.as_ref()), cb);
66+
}
67+
68+
pub fn field_decl(&mut self, name: impl AsRef<str>, field_type: impl AsRef<str>) {
69+
self.write_line(format!("{}: {},", name.as_ref(), field_type.as_ref()));
70+
}
71+
72+
pub fn impl_self_block(&mut self, name: impl AsRef<str>, cb: impl FnOnce(&mut CodeWriter)) {
73+
self.expr_block(format!("impl {}", name.as_ref()), cb);
74+
}
75+
76+
pub fn pub_trait(&mut self, name: impl AsRef<str>, cb: impl FnOnce(&mut CodeWriter)) {
77+
self.expr_block(format!("pub trait {}", name.as_ref()), cb);
78+
}
79+
}

0 commit comments

Comments
 (0)