|
3 | 3 | //! The missing functionalities have been reimplemented in this module. |
4 | 4 |
|
5 | 5 | 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 | | -} |
16 | 6 |
|
17 | 7 | // vendered from https://github.com/stepancheg/rust-protobuf/blob/v3.7.2/protobuf-codegen/src/gen/rust/keywords.rs |
18 | 8 | fn is_rust_keyword(ident: &str) -> bool { |
@@ -78,82 +68,6 @@ fn is_rust_keyword(ident: &str) -> bool { |
78 | 68 | RUST_KEYWORDS.contains(&ident) |
79 | 69 | } |
80 | 70 |
|
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 | | - |
157 | 71 | // reimplementation based on https://github.com/stepancheg/rust-protobuf/blob/v3.7.2/protobuf-codegen/src/gen/scope.rs#L26 |
158 | 72 | // it only implements the `find_message` method with not extra dependencies |
159 | 73 | pub struct RootScope<'a> { |
@@ -198,7 +112,7 @@ impl ScopedMessage<'_> { |
198 | 112 | pub fn rust_fq_name(&self) -> String { |
199 | 113 | format!( |
200 | 114 | "{}::{}", |
201 | | - proto_path_to_rust_mod(self.fd.name()), |
| 115 | + super::proto_path_to_rust_mod(self.fd.name()), |
202 | 116 | self.rust_name() |
203 | 117 | ) |
204 | 118 | } |
|
0 commit comments