1use prost_reflect::DescriptorPool;
7use std::collections::HashMap;
8use std::fs;
9use std::path::{Path, PathBuf};
10use std::process::Command;
11use tempfile::TempDir;
12use tracing::{debug, error, info, warn};
13
14#[derive(Debug, Clone)]
16pub struct ProtoService {
17 pub name: String,
19 pub package: String,
21 pub short_name: String,
23 pub methods: Vec<ProtoMethod>,
25}
26
27#[derive(Debug, Clone)]
29pub struct ProtoMethod {
30 pub name: String,
32 pub input_type: String,
34 pub output_type: String,
36 pub client_streaming: bool,
38 pub server_streaming: bool,
40}
41
42pub struct ProtoParser {
44 pool: DescriptorPool,
46 services: HashMap<String, ProtoService>,
48 include_paths: Vec<PathBuf>,
50 temp_dir: Option<TempDir>,
52}
53
54impl ProtoParser {
55 pub fn new() -> Self {
57 Self {
58 pool: DescriptorPool::new(),
59 services: HashMap::new(),
60 include_paths: vec![],
61 temp_dir: None,
62 }
63 }
64
65 pub fn with_include_paths(include_paths: Vec<PathBuf>) -> Self {
67 Self {
68 pool: DescriptorPool::new(),
69 services: HashMap::new(),
70 include_paths,
71 temp_dir: None,
72 }
73 }
74
75 pub async fn parse_directory(
77 &mut self,
78 proto_dir: &str,
79 ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
80 info!("Parsing proto files from directory: {}", proto_dir);
81
82 let proto_path = Path::new(proto_dir);
83 if !proto_path.exists() {
84 return Err(format!("Proto directory does not exist: {}", proto_dir).into());
85 }
86
87 let proto_files = self.discover_proto_files(proto_path)?;
89 if proto_files.is_empty() {
90 warn!("No proto files found in directory: {}", proto_dir);
91 return Ok(());
92 }
93
94 info!("Found {} proto files: {:?}", proto_files.len(), proto_files);
95
96 for proto_file in proto_files {
98 if let Err(e) = self.parse_proto_file(&proto_file).await {
99 error!("Failed to parse proto file {}: {}", proto_file, e);
100 }
102 }
103
104 if self.pool.services().count() > 0 {
106 self.extract_services()?;
107 } else {
108 debug!("No services found in descriptor pool, keeping mock services");
109 }
110
111 info!("Successfully parsed {} services", self.services.len());
112 Ok(())
113 }
114
115 #[allow(clippy::only_used_in_recursion)]
117 fn discover_proto_files(
118 &self,
119 dir: &Path,
120 ) -> Result<Vec<String>, Box<dyn std::error::Error + Send + Sync>> {
121 let mut proto_files = Vec::new();
122
123 if let Ok(entries) = fs::read_dir(dir) {
124 for entry in entries.flatten() {
125 let path = entry.path();
126
127 if path.is_dir() {
128 proto_files.extend(self.discover_proto_files(&path)?);
130 } else if path.extension().and_then(|s| s.to_str()) == Some("proto") {
131 proto_files.push(path.to_string_lossy().to_string());
133 }
134 }
135 }
136
137 Ok(proto_files)
138 }
139
140 async fn parse_proto_file(
142 &mut self,
143 proto_file: &str,
144 ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
145 debug!("Parsing proto file: {}", proto_file);
146
147 if self.temp_dir.is_none() {
149 self.temp_dir = Some(TempDir::new()?);
150 }
151
152 let temp_dir = self.temp_dir.as_ref().unwrap();
153 let descriptor_path = temp_dir.path().join("descriptors.bin");
154
155 match self.compile_with_protoc(proto_file, &descriptor_path).await {
157 Ok(()) => {
158 let descriptor_bytes = fs::read(&descriptor_path)?;
160 match self.pool.decode_file_descriptor_set(&*descriptor_bytes) {
161 Ok(()) => {
162 info!("Successfully compiled and loaded proto file: {}", proto_file);
163 if self.pool.services().count() > 0 {
165 self.extract_services()?;
166 }
167 return Ok(());
168 }
169 Err(e) => {
170 warn!("Failed to decode descriptor set, falling back to mock: {}", e);
171 }
172 }
173 }
174 Err(e) => {
175 warn!("Failed to compile with protoc, falling back to mock: {}", e);
176 }
177 }
178
179 if proto_file.contains("gretter.proto") || proto_file.contains("greeter.proto") {
181 debug!("Adding mock greeter service for {}", proto_file);
182 self.add_mock_greeter_service();
183 }
184
185 Ok(())
186 }
187
188 async fn compile_with_protoc(
190 &self,
191 proto_file: &str,
192 output_path: &Path,
193 ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
194 debug!("Compiling proto file with protoc: {}", proto_file);
195
196 let mut cmd = Command::new("protoc");
198
199 for include_path in &self.include_paths {
201 cmd.arg("-I").arg(include_path);
202 }
203
204 if let Some(parent_dir) = Path::new(proto_file).parent() {
206 cmd.arg("-I").arg(parent_dir);
207 }
208
209 let well_known_paths = [
211 "/usr/local/include",
212 "/usr/include",
213 "/opt/homebrew/include",
214 ];
215
216 for path in &well_known_paths {
217 if Path::new(path).exists() {
218 cmd.arg("-I").arg(path);
219 }
220 }
221
222 cmd.arg("--descriptor_set_out")
224 .arg(output_path)
225 .arg("--include_imports")
226 .arg("--include_source_info")
227 .arg(proto_file);
228
229 debug!("Running protoc command: {:?}", cmd);
230
231 let output = cmd.output()?;
233
234 if !output.status.success() {
235 let stderr = String::from_utf8_lossy(&output.stderr);
236 return Err(format!("protoc failed: {}", stderr).into());
237 }
238
239 info!("Successfully compiled proto file with protoc: {}", proto_file);
240 Ok(())
241 }
242
243 fn add_mock_greeter_service(&mut self) {
245 let service = ProtoService {
246 name: "mockforge.greeter.Greeter".to_string(),
247 package: "mockforge.greeter".to_string(),
248 short_name: "Greeter".to_string(),
249 methods: vec![
250 ProtoMethod {
251 name: "SayHello".to_string(),
252 input_type: "mockforge.greeter.HelloRequest".to_string(),
253 output_type: "mockforge.greeter.HelloReply".to_string(),
254 client_streaming: false,
255 server_streaming: false,
256 },
257 ProtoMethod {
258 name: "SayHelloStream".to_string(),
259 input_type: "mockforge.greeter.HelloRequest".to_string(),
260 output_type: "mockforge.greeter.HelloReply".to_string(),
261 client_streaming: false,
262 server_streaming: true,
263 },
264 ProtoMethod {
265 name: "SayHelloClientStream".to_string(),
266 input_type: "mockforge.greeter.HelloRequest".to_string(),
267 output_type: "mockforge.greeter.HelloReply".to_string(),
268 client_streaming: true,
269 server_streaming: false,
270 },
271 ProtoMethod {
272 name: "Chat".to_string(),
273 input_type: "mockforge.greeter.HelloRequest".to_string(),
274 output_type: "mockforge.greeter.HelloReply".to_string(),
275 client_streaming: true,
276 server_streaming: true,
277 },
278 ],
279 };
280
281 self.services.insert(service.name.clone(), service);
282 }
283
284 fn extract_services(&mut self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
286 debug!("Extracting services from descriptor pool");
287
288 let mock_services: HashMap<String, ProtoService> = self
290 .services
291 .drain()
292 .filter(|(name, _)| name.contains("mockforge.greeter"))
293 .collect();
294
295 self.services = mock_services;
296
297 for service_descriptor in self.pool.services() {
299 let service_name = service_descriptor.full_name().to_string();
300 let package_name = service_descriptor.parent_file().package_name().to_string();
301 let short_name = service_descriptor.name().to_string();
302
303 debug!("Found service: {} in package: {}", service_name, package_name);
304
305 let mut methods = Vec::new();
307 for method_descriptor in service_descriptor.methods() {
308 let method = ProtoMethod {
309 name: method_descriptor.name().to_string(),
310 input_type: method_descriptor.input().full_name().to_string(),
311 output_type: method_descriptor.output().full_name().to_string(),
312 client_streaming: method_descriptor.is_client_streaming(),
313 server_streaming: method_descriptor.is_server_streaming(),
314 };
315
316 debug!(
317 " Found method: {} ({} -> {})",
318 method.name, method.input_type, method.output_type
319 );
320
321 methods.push(method);
322 }
323
324 let service = ProtoService {
325 name: service_name.clone(),
326 package: package_name,
327 short_name,
328 methods,
329 };
330
331 self.services.insert(service_name, service);
332 }
333
334 info!("Extracted {} services from descriptor pool", self.services.len());
335 Ok(())
336 }
337
338 pub fn services(&self) -> &HashMap<String, ProtoService> {
340 &self.services
341 }
342
343 pub fn get_service(&self, name: &str) -> Option<&ProtoService> {
345 self.services.get(name)
346 }
347
348 pub fn pool(&self) -> &DescriptorPool {
350 &self.pool
351 }
352
353 pub fn into_pool(self) -> DescriptorPool {
355 self.pool
356 }
357}
358
359impl Default for ProtoParser {
360 fn default() -> Self {
361 Self::new()
362 }
363}
364
365#[cfg(test)]
366mod tests {
367 use super::*;
368
369 #[tokio::test]
370 async fn test_parse_proto_file() {
371 let proto_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap() + "/proto";
373 let proto_path = format!("{}/gretter.proto", proto_dir);
374
375 let mut parser = ProtoParser::new();
377 parser.parse_proto_file(&proto_path).await.unwrap();
378
379 let services = parser.services();
381 assert_eq!(services.len(), 1);
382
383 let service_name = "mockforge.greeter.Greeter";
384 assert!(services.contains_key(service_name));
385
386 let service = &services[service_name];
387 assert_eq!(service.name, service_name);
388 assert_eq!(service.methods.len(), 4); let say_hello = service.methods.iter().find(|m| m.name == "SayHello").unwrap();
392 assert_eq!(say_hello.input_type, "mockforge.greeter.HelloRequest");
393 assert_eq!(say_hello.output_type, "mockforge.greeter.HelloReply");
394 assert!(!say_hello.client_streaming);
395 assert!(!say_hello.server_streaming);
396
397 let say_hello_stream = service.methods.iter().find(|m| m.name == "SayHelloStream").unwrap();
399 assert!(!say_hello_stream.client_streaming);
400 assert!(say_hello_stream.server_streaming);
401 }
402
403 #[tokio::test]
404 async fn test_parse_directory() {
405 let proto_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap() + "/proto";
407
408 let mut parser = ProtoParser::new();
410 parser.parse_directory(&proto_dir).await.unwrap();
411
412 let services = parser.services();
414 assert_eq!(services.len(), 1);
415
416 let service_name = "mockforge.greeter.Greeter";
417 assert!(services.contains_key(service_name));
418
419 let service = &services[service_name];
420 assert_eq!(service.methods.len(), 4);
421
422 let method_names: Vec<&str> = service.methods.iter().map(|m| m.name.as_str()).collect();
424 assert!(method_names.contains(&"SayHello"));
425 assert!(method_names.contains(&"SayHelloStream"));
426 assert!(method_names.contains(&"SayHelloClientStream"));
427 assert!(method_names.contains(&"Chat"));
428 }
429}