cargo-mate 1.7.6

Rust development companion that enhances cargo with intelligent workflows, state management, performance optimization, and comprehensive project monitoring.
Documentation
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
use super::{Tool, Result, ToolError, common_options, parse_output_format, OutputFormat};
use clap::{Arg, ArgMatches, Command};
use colored::*;
use std::collections::HashSet;
use std::path::Path;
use std::fs;
use syn::{parse_file, ItemTrait, TraitItem, FnArg, Pat, Type};
use quote::quote;
use proc_macro2::TokenStream;
use serde::{Serialize, Deserialize};
#[derive(Debug, Clone)]
pub struct MockDeriveTool;
#[derive(Debug, Clone, Serialize, Deserialize)]
struct TraitInfo {
    name: String,
    methods: Vec<MethodInfo>,
    is_unsafe: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct MethodInfo {
    name: String,
    return_type: Option<String>,
    params: Vec<ParamInfo>,
    is_async: bool,
    is_unsafe: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct ParamInfo {
    name: String,
    ty: String,
    is_self: bool,
}
impl MockDeriveTool {
    pub fn new() -> Self {
        Self
    }
    fn parse_trait_from_file(&self, file_path: &str) -> Result<Vec<TraitInfo>> {
        let content = fs::read_to_string(file_path).map_err(|e| ToolError::IoError(e))?;
        let syntax = parse_file(&content)
            .map_err(|e| ToolError::ExecutionFailed(
                format!("Failed to parse Rust file: {}", e),
            ))?;
        let mut traits = Vec::new();
        for item in syntax.items {
            if let syn::Item::Trait(trait_def) = item {
                let trait_info = self.parse_trait(&trait_def)?;
                traits.push(trait_info);
            }
        }
        Ok(traits)
    }
    fn parse_trait(&self, trait_def: &syn::ItemTrait) -> Result<TraitInfo> {
        let name = trait_def.ident.to_string();
        let is_unsafe = trait_def.unsafety.is_some();
        let mut methods = Vec::new();
        for item in &trait_def.items {
            if let syn::TraitItem::Fn(method) = item {
                let method_info = self.parse_trait_method(method)?;
                methods.push(method_info);
            }
        }
        Ok(TraitInfo {
            name,
            methods,
            is_unsafe,
        })
    }
    fn parse_trait_method(&self, method: &syn::TraitItemFn) -> Result<MethodInfo> {
        let name = method.sig.ident.to_string();
        let is_async = method.sig.asyncness.is_some();
        let is_unsafe = method.sig.unsafety.is_some();
        let return_type = if let syn::ReturnType::Type(_, ty) = &method.sig.output {
            Some(quote!(# ty).to_string())
        } else {
            None
        };
        let mut params = Vec::new();
        for arg in &method.sig.inputs {
            match arg {
                syn::FnArg::Receiver(receiver) => {
                    let self_type = if receiver.reference.is_some() {
                        if receiver.mutability.is_some() { "&mut self" } else { "&self" }
                    } else {
                        "self"
                    };
                    params
                        .push(ParamInfo {
                            name: "self".to_string(),
                            ty: self_type.to_string(),
                            is_self: true,
                        });
                }
                syn::FnArg::Typed(pat_type) => {
                    if let syn::Pat::Ident(pat_ident) = &*pat_type.pat {
                        let param_name = pat_ident.ident.to_string();
                        let param_type = quote!(# pat_type.ty).to_string();
                        params
                            .push(ParamInfo {
                                name: param_name,
                                ty: param_type,
                                is_self: false,
                            });
                    }
                }
            }
        }
        Ok(MethodInfo {
            name,
            return_type,
            params,
            is_async,
            is_unsafe,
        })
    }
    fn generate_mock_struct(&self, trait_info: &TraitInfo) -> String {
        let struct_name = format!("Mock{}", trait_info.name);
        let mut mock_code = format!(
            "/// Auto-generated mock for trait `{}`\n", trait_info.name
        );
        mock_code.push_str("#[derive(Debug, Clone, Default)]\n");
        mock_code.push_str(&format!("pub struct {} {{\n", struct_name));
        for method in &trait_info.methods {
            mock_code
                .push_str(
                    &format!(
                        "    pub {}_call_count: std::sync::atomic::AtomicUsize,\n",
                        method.name
                    ),
                );
        }
        for method in &trait_info.methods {
            if let Some(return_type) = &method.return_type {
                mock_code
                    .push_str(
                        &format!(
                            "    pub {}_return_value: Option<{}>,\n", method.name,
                            return_type
                        ),
                    );
            }
        }
        mock_code.push_str("}\n\n");
        mock_code
            .push_str(&format!("impl {} for {} {{\n", trait_info.name, struct_name));
        for method in &trait_info.methods {
            mock_code.push_str(&self.generate_mock_method(method));
        }
        mock_code.push_str("}\n\n");
        mock_code.push_str(&self.generate_helper_methods(trait_info));
        mock_code
    }
    fn generate_mock_method(&self, method: &MethodInfo) -> String {
        let mut method_code = String::new();
        if method.is_async {
            method_code.push_str("    async ");
        }
        if method.is_unsafe {
            method_code.push_str("unsafe ");
        }
        method_code.push_str(&format!("fn {}(", method.name));
        let param_strs: Vec<String> = method
            .params
            .iter()
            .map(|param| {
                if param.is_self {
                    param.ty.clone()
                } else {
                    format!("{}: {}", param.name, param.ty)
                }
            })
            .collect();
        method_code.push_str(&param_strs.join(", "));
        method_code.push_str(")");
        if let Some(return_type) = &method.return_type {
            method_code.push_str(&format!(" -> {}", return_type));
        }
        method_code.push_str(" {\n");
        method_code
            .push_str(
                &format!(
                    "        self.{}_call_count.fetch_add(1, std::sync::atomic::Ordering::Relaxed);\n",
                    method.name
                ),
            );
        if let Some(return_type) = &method.return_type {
            method_code
                .push_str(
                    &format!(
                        "        self.{}_return_value.unwrap_or_default()\n", method.name
                    ),
                );
        } else {
            method_code.push_str("        ()\n");
        }
        method_code.push_str("    }\n\n");
        method_code
    }
    fn generate_helper_methods(&self, trait_info: &TraitInfo) -> String {
        let struct_name = format!("Mock{}", trait_info.name);
        let mut helper_code = String::new();
        helper_code.push_str(&format!("impl {} {{\n", struct_name));
        for method in &trait_info.methods {
            helper_code
                .push_str(
                    &format!("    pub fn {}_called(&self) -> bool {{\n", method.name),
                );
            helper_code
                .push_str(
                    &format!(
                        "        self.{}_call_count.load(std::sync::atomic::Ordering::Relaxed) > 0\n",
                        method.name
                    ),
                );
            helper_code.push_str("    }\n\n");
            helper_code
                .push_str(
                    &format!(
                        "    pub fn {}_call_count(&self) -> usize {{\n", method.name
                    ),
                );
            helper_code
                .push_str(
                    &format!(
                        "        self.{}_call_count.load(std::sync::atomic::Ordering::Relaxed)\n",
                        method.name
                    ),
                );
            helper_code.push_str("    }\n\n");
        }
        for method in &trait_info.methods {
            if let Some(return_type) = &method.return_type {
                helper_code
                    .push_str(
                        &format!(
                            "    pub fn set_{}_return(&mut self, value: {}) {{\n", method
                            .name, return_type
                        ),
                    );
                helper_code
                    .push_str(
                        &format!(
                            "        self.{}_return_value = Some(value);\n", method.name
                        ),
                    );
                helper_code.push_str("    }\n\n");
            }
        }
        helper_code.push_str("    pub fn reset(&mut self) {\n");
        for method in &trait_info.methods {
            helper_code
                .push_str(
                    &format!(
                        "        self.{}_call_count.store(0, std::sync::atomic::Ordering::Relaxed);\n",
                        method.name
                    ),
                );
            if method.return_type.is_some() {
                helper_code
                    .push_str(
                        &format!("        self.{}_return_value = None;\n", method.name),
                    );
            }
        }
        helper_code.push_str("    }\n");
        helper_code.push_str("}\n\n");
        helper_code
    }
    fn display_analysis(
        &self,
        traits: &[TraitInfo],
        output_format: OutputFormat,
        verbose: bool,
    ) {
        match output_format {
            OutputFormat::Human => {
                println!("\n{}", "🎭 Mock Generation Analysis Report".bold().blue());
                println!("{}", "".repeat(50).blue());
                println!("\n📋 Found {} trait(s):", traits.len());
                for trait_info in traits {
                    println!(
                        "{} - {} method(s)", trait_info.name.green(), trait_info
                        .methods.len()
                    );
                    if verbose {
                        for method in &trait_info.methods {
                            let async_str = if method.is_async { "async " } else { "" };
                            let unsafe_str = if method.is_unsafe {
                                "unsafe "
                            } else {
                                ""
                            };
                            println!(
                                "    - {}{}{}({} params)", async_str.bright_blue(),
                                unsafe_str.red(), method.name, method.params.len()
                            );
                        }
                    }
                }
                if !traits.is_empty() {
                    println!("\n💡 Generated mock structs:");
                    for trait_info in traits {
                        println!(
                            "  • Mock{} - Ready for testing", trait_info.name.green()
                        );
                    }
                }
            }
            OutputFormat::Json => {
                let json = serde_json::to_string_pretty(traits)
                    .unwrap_or_else(|_| "[]".to_string());
                println!("{}", json);
            }
            OutputFormat::Table => {
                println!(
                    "{:<25} {:<10} {:<15} {:<10}", "Trait", "Methods", "Unsafe", "Async"
                );
                println!("{}", "".repeat(70));
                for trait_info in traits {
                    let async_count = trait_info
                        .methods
                        .iter()
                        .filter(|m| m.is_async)
                        .count();
                    let unsafe_count = trait_info
                        .methods
                        .iter()
                        .filter(|m| m.is_unsafe)
                        .count();
                    println!(
                        "{:<25} {:<10} {:<15} {:<10}", trait_info.name, trait_info
                        .methods.len().to_string(), unsafe_count.to_string(), async_count
                        .to_string()
                    );
                }
            }
        }
    }
}
impl Tool for MockDeriveTool {
    fn name(&self) -> &'static str {
        "mock-derive"
    }
    fn description(&self) -> &'static str {
        "Auto-generate mock implementations for traits"
    }
    fn command(&self) -> Command {
        Command::new(self.name())
            .about(self.description())
            .long_about(
                "Generate mock implementations for Rust traits automatically. \
                        This tool analyzes trait definitions and creates mock structs \
                        with call counting, return value injection, and testing helpers.

EXAMPLES:
    cm tool mock-derive src/lib.rs --output mocks/
    cm tool mock-derive src/traits.rs --traits UserService,Database
    cm tool mock-derive --workspace --verbose",
            )
            .args(
                &[
                    Arg::new("input")
                        .help("Input Rust file(s) containing traits")
                        .value_name("FILE")
                        .index(1),
                    Arg::new("output")
                        .long("output")
                        .short('o')
                        .help("Output directory for generated mocks")
                        .default_value("mocks/"),
                    Arg::new("traits")
                        .long("traits")
                        .short('t')
                        .help("Specific traits to mock (comma-separated)"),
                    Arg::new("workspace")
                        .long("workspace")
                        .help("Generate mocks for all traits in workspace")
                        .action(clap::ArgAction::SetTrue),
                    Arg::new("force")
                        .long("force")
                        .short('f')
                        .help("Overwrite existing mock files")
                        .action(clap::ArgAction::SetTrue),
                    Arg::new("include-async")
                        .long("include-async")
                        .help("Include async trait support")
                        .action(clap::ArgAction::SetTrue),
                ],
            )
            .args(&common_options())
    }
    fn execute(&self, matches: &ArgMatches) -> Result<()> {
        let input = matches.get_one::<String>("input");
        let output_dir = matches.get_one::<String>("output").unwrap();
        let traits_filter = matches
            .get_one::<String>("traits")
            .map(|s| s.split(',').map(|s| s.trim().to_string()).collect::<HashSet<_>>())
            .unwrap_or_default();
        let workspace = matches.get_flag("workspace");
        let force = matches.get_flag("force");
        let include_async = matches.get_flag("include-async");
        let output_format = parse_output_format(matches);
        let verbose = matches.get_flag("verbose");
        std::fs::create_dir_all(output_dir).map_err(|e| ToolError::IoError(e))?;
        let mut all_traits = Vec::new();
        if workspace {
            let workspace_files = self.find_workspace_files()?;
            for file_path in workspace_files {
                if let Ok(traits) = self.parse_trait_from_file(&file_path) {
                    for trait_info in traits {
                        if traits_filter.is_empty()
                            || traits_filter.contains(&trait_info.name)
                        {
                            all_traits.push((file_path.clone(), trait_info));
                        }
                    }
                }
            }
        } else if let Some(input_file) = input {
            let traits = self.parse_trait_from_file(input_file)?;
            for trait_info in traits {
                if traits_filter.is_empty() || traits_filter.contains(&trait_info.name) {
                    all_traits.push((input_file.to_string(), trait_info));
                }
            }
        } else {
            return Err(
                ToolError::InvalidArguments(
                    "Either specify an input file or use --workspace".to_string(),
                ),
            );
        }
        if all_traits.is_empty() {
            println!("{}", "No traits found matching criteria".yellow());
            return Ok(());
        }
        let mut generated_files = Vec::new();
        for (file_path, trait_info) in &all_traits {
            let mock_code = self.generate_mock_struct(trait_info);
            let file_name = format!("mock_{}.rs", trait_info.name.to_lowercase());
            let output_path = Path::new(output_dir).join(file_name);
            if !force && output_path.exists() {
                println!(
                    "⚠️  Skipping {} (use --force to overwrite)", output_path
                    .display()
                );
                continue;
            }
            fs::write(&output_path, mock_code).map_err(|e| ToolError::IoError(e))?;
            generated_files
                .push((trait_info.name.clone(), output_path.display().to_string()));
            if verbose {
                println!(
                    "✅ Generated mock for trait {} -> {}", trait_info.name, output_path
                    .display()
                );
            }
        }
        let trait_infos: Vec<TraitInfo> = all_traits
            .into_iter()
            .map(|(_, info)| info)
            .collect();
        self.display_analysis(&trait_infos, output_format, verbose);
        if !generated_files.is_empty() {
            println!("\n🎭 Generated {} mock(s):", generated_files.len());
            for (trait_name, file_path) in &generated_files {
                println!("  • Mock{} -> {}", trait_name.green(), file_path);
            }
            println!("\n💡 Add to your tests:");
            println!("  ```rust");
            println!("  use mocks::MockMyTrait;");
            println!("  let mut mock = MockMyTrait::default();");
            println!("  mock.set_my_method_return(42);");
            println!("  ```");
        }
        Ok(())
    }
}
impl MockDeriveTool {
    fn find_workspace_files(&self) -> Result<Vec<String>> {
        let mut files = Vec::new();
        let cargo_toml_path = "Cargo.toml";
        if !Path::new(cargo_toml_path).exists() {
            return Err(
                ToolError::InvalidArguments(
                    "Not in a Rust project (Cargo.toml not found)".to_string(),
                ),
            );
        }
        self.find_rs_files("src", &mut files)?;
        Ok(files)
    }
    fn find_rs_files(&self, dir: &str, files: &mut Vec<String>) -> Result<()> {
        let path = Path::new(dir);
        if !path.exists() {
            return Ok(());
        }
        for entry in fs::read_dir(path)? {
            let entry = entry?;
            let path = entry.path();
            if path.is_dir() {
                let dir_name = path.file_name().unwrap_or_default().to_string_lossy();
                if !matches!(dir_name.as_ref(), "target" | ".git" | "mocks" | "tests") {
                    self.find_rs_files(&path.to_string_lossy(), files)?;
                }
            } else if let Some(ext) = path.extension() {
                if ext == "rs" {
                    files.push(path.to_string_lossy().to_string());
                }
            }
        }
        Ok(())
    }
}
impl Default for MockDeriveTool {
    fn default() -> Self {
        Self::new()
    }
}