rustquty-core 0.3.1

Core library for rustquty, a local-first quality scanner for Rust projects
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
//! Size collector — measures file and function size metrics.

use super::{Collector, CollectorError, CollectorOutput};
use crate::config::SizeConfig;
use crate::context::Context;
use std::fs;
use std::path::Path;
use walkdir::WalkDir;

/// A detected function in a Rust source file.
#[derive(Debug, Clone)]
struct FunctionInfo {
    /// Name of the function.
    name: String,
    /// Source file path.
    file: String,
    /// Starting line number (1-indexed).
    start_line: u32,
    /// Ending line number (1-indexed).
    #[allow(dead_code)]
    end_line: u32,
    /// Total lines in the function (end_line - start_line + 1).
    total_lines: u32,
    /// Number of explicit parameters.
    param_count: usize,
}

/// Count total lines in a file.
fn count_total_lines(content: &str) -> u32 {
    content.lines().count() as u32
}

/// Count lines that are code (not blank, not a pure comment line).
fn count_code_lines(content: &str) -> u32 {
    let mut code_lines = 0u32;
    for line in content.lines() {
        let trimmed = line.trim();
        if trimmed.is_empty() {
            continue;
        }
        // Simple comment line detection:
        // - // single line comment (including /// and //! variants)
        // - /* block comment start
        // - * block comment continuation
        // - */ block comment end
        if trimmed.starts_with("//") || trimmed.starts_with("/*") || trimmed.ends_with("*/") {
            continue;
        }
        code_lines += 1;
    }
    code_lines
}

/// Parse a Rust source file and extract function information using syn.
fn extract_functions(content: &str, file_path: &Path) -> Vec<FunctionInfo> {
    use syn::spanned::Spanned;
    use syn::{Item, parse_file};

    let mut functions = Vec::new();
    let file_stem = file_path
        .file_name()
        .map(|s| s.to_string_lossy().to_string())
        .unwrap_or_else(|| "unknown".to_string());

    // Parse the file using syn.
    let Ok(syn_file) = parse_file(content) else {
        return functions;
    };

    // Process standalone functions (ItemFn).
    for item in &syn_file.items {
        if let Item::Fn(item_fn) = item {
            let name = item_fn.sig.ident.to_string();
            let span = item_fn.span();
            let start_loc = span.start();
            let end_loc = span.end();

            let start_line = start_loc.line as u32;
            let end_line = end_loc.line as u32;
            let total_lines = end_line.saturating_sub(start_line) + 1;

            // Count parameters (including self/&self/&mut self).
            let param_count = item_fn.sig.inputs.len();

            functions.push(FunctionInfo {
                name,
                file: file_stem.clone(),
                start_line,
                end_line,
                total_lines,
                param_count,
            });
        }
    }

    // Process impl blocks for methods.
    for item in &syn_file.items {
        if let Item::Impl(item_impl) = item {
            for item_impl_item in &item_impl.items {
                if let syn::ImplItem::Fn(item_fn) = item_impl_item {
                    let name = item_fn.sig.ident.to_string();
                    let span = item_fn.span();
                    let start_loc = span.start();
                    let end_loc = span.end();

                    let start_line = start_loc.line as u32;
                    let end_line = end_loc.line as u32;
                    let total_lines = end_line.saturating_sub(start_line) + 1;

                    // Count parameters (including self/&self/&mut self).
                    let param_count = item_fn.sig.inputs.len();

                    functions.push(FunctionInfo {
                        name,
                        file: file_stem.clone(),
                        start_line,
                        end_line,
                        total_lines,
                        param_count,
                    });
                }
            }
        }
    }

    functions
}

/// A violation detected by the size collector.
#[derive(Debug, Clone, serde::Serialize)]
struct SizeViolation {
    #[serde(rename = "ruleId")]
    rule_id: String,
    file: String,
    line: u32,
    function: Option<String>,
    message: String,
    actual: u32,
    threshold: u32,
    severity: String,
}

/// Configuration for the size collector (from [gate.size] in TOML).
#[derive(Debug, Clone, Default)]
pub struct SizeCollectorConfig {
    pub max_lines_per_file: Option<u32>,
    pub max_code_lines_per_file: Option<u32>,
    pub max_lines_per_function: Option<u32>,
    pub max_parameters_per_function: Option<u32>,
}

/// The size collector.
pub struct SizeCollector {
    config: SizeCollectorConfig,
}

impl SizeCollector {
    pub fn new() -> Self {
        Self {
            config: SizeCollectorConfig::default(),
        }
    }

    /// Configure the collector from a SizeConfig (TOML [gate.size] section).
    pub fn with_config(config: SizeConfig) -> Self {
        Self {
            config: SizeCollectorConfig {
                max_lines_per_file: config.max_lines_per_file,
                max_code_lines_per_file: config.max_code_lines_per_file,
                max_lines_per_function: config.max_lines_per_function,
                max_parameters_per_function: config.max_parameters_per_function,
            },
        }
    }

    fn collect_impl(&self, ctx: &Context) -> Result<CollectorOutput, CollectorError> {
        let start = std::time::Instant::now();

        let mut total_files: u32 = 0;
        let mut max_lines_per_file: u32 = 0;
        let mut max_code_lines_per_file: u32 = 0;
        let mut max_lines_per_function: u32 = 0;
        let mut max_parameters_per_function: u32 = 0;

        let mut all_functions: Vec<FunctionInfo> = Vec::new();
        let mut violations: Vec<SizeViolation> = Vec::new();

        // Walk all Rust files recursively.
        for entry in WalkDir::new(&ctx.workspace_root)
            .into_iter()
            .filter_map(|e| e.ok())
        {
            let path = entry.path();
            if path.is_file() && path.extension().is_some_and(|e| e == "rs") {
                total_files += 1;

                if let Ok(content) = fs::read_to_string(&path) {
                    let lines = count_total_lines(&content);
                    let code_lines = count_code_lines(&content);

                    if lines > max_lines_per_file {
                        max_lines_per_file = lines;
                    }
                    if code_lines > max_code_lines_per_file {
                        max_code_lines_per_file = code_lines;
                    }

                    let file_name = path
                        .file_name()
                        .map(|s| s.to_string_lossy().to_string())
                        .unwrap_or_else(|| "unknown".to_string());

                    // Check file-level violations.
                    if let Some(max) = self.config.max_lines_per_file
                        && lines > max
                    {
                        violations.push(SizeViolation {
                            rule_id: "size:max-lines-per-file".to_string(),
                            file: file_name.clone(),
                            line: 1,
                            function: None,
                            message: format!(
                                "File has {} lines; maximum allowed is {}",
                                lines, max
                            ),
                            actual: lines,
                            threshold: max,
                            severity: "major".to_string(),
                        });
                    }
                    if let Some(max) = self.config.max_code_lines_per_file
                        && code_lines > max
                    {
                        violations.push(SizeViolation {
                            rule_id: "size:max-code-lines-per-file".to_string(),
                            file: file_name.clone(),
                            line: 1,
                            function: None,
                            message: format!(
                                "File has {} code lines; maximum allowed is {}",
                                code_lines, max
                            ),
                            actual: code_lines,
                            threshold: max,
                            severity: "major".to_string(),
                        });
                    }

                    // Extract and process functions.
                    let functions = extract_functions(&content, &path);
                    for func in functions {
                        if func.total_lines > max_lines_per_function {
                            max_lines_per_function = func.total_lines;
                        }
                        if func.param_count as u32 > max_parameters_per_function {
                            max_parameters_per_function = func.param_count as u32;
                        }

                        // Check function-level violations.
                        if let Some(max) = self.config.max_lines_per_function
                            && func.total_lines > max
                        {
                            violations.push(SizeViolation {
                                rule_id: "size:max-lines-per-function".to_string(),
                                file: func.file.clone(),
                                line: func.start_line,
                                function: Some(func.name.clone()),
                                message: format!(
                                    "Function `{}` has {} lines; maximum allowed is {}",
                                    func.name, func.total_lines, max
                                ),
                                actual: func.total_lines,
                                threshold: max,
                                severity: "major".to_string(),
                            });
                        }
                        if let Some(max) = self.config.max_parameters_per_function
                            && func.param_count as u32 > max
                        {
                            violations.push(SizeViolation {
                                rule_id: "size:max-parameters-per-function".to_string(),
                                file: func.file.clone(),
                                line: func.start_line,
                                function: Some(func.name.clone()),
                                message: format!(
                                    "Function `{}` has {} parameters; maximum allowed is {}",
                                    func.name, func.param_count, max
                                ),
                                actual: func.param_count as u32,
                                threshold: max,
                                severity: "major".to_string(),
                            });
                        }

                        all_functions.push(func);
                    }
                }
            }
        }

        let duration_ms = start.elapsed().as_millis() as u64;

        // Determine overall status.
        let status = if violations.is_empty() {
            crate::schema::CollectorStatus::Pass
        } else {
            crate::schema::CollectorStatus::Fail
        };

        let details = serde_json::json!({
            "files": total_files,
            "maxLinesPerFile": max_lines_per_file,
            "maxCodeLinesPerFile": max_code_lines_per_file,
            "maxLinesPerFunction": max_lines_per_function,
            "maxParametersPerFunction": max_parameters_per_function,
            "violations": violations,
        });

        Ok(CollectorOutput {
            status,
            duration_ms,
            stdout: serde_json::to_string(&details).unwrap_or_default(),
            stderr: String::new(),
        })
    }
}

impl Collector for SizeCollector {
    fn name(&self) -> &'static str {
        "size"
    }

    fn is_available(&self) -> bool {
        true
    }

    fn collect(&self, ctx: &Context) -> Result<CollectorOutput, CollectorError> {
        self.collect_impl(ctx)
    }
}

impl Default for SizeCollector {
    fn default() -> Self {
        Self::new()
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    // Helper: create a temp file with given content.
    fn temp_file(content: &str) -> tempfile::TempDir {
        let dir = tempfile::TempDir::new().unwrap();
        let file_path = dir.path().join("test.rs");
        std::fs::write(&file_path, content).unwrap();
        dir
    }

    // Helper: run collector on a single file.
    fn run_on_content(content: &str) -> CollectorOutput {
        let dir = temp_file(content);
        let ctx = Context::new(dir.path().to_path_buf());
        let collector = SizeCollector::new();
        collector.collect(&ctx).unwrap()
    }

    // Helper: parse details from stdout.
    fn parse_details(output: &CollectorOutput) -> serde_json::Value {
        serde_json::from_str(&output.stdout).unwrap()
    }

    // --- Line counting tests ---

    #[test]
    fn test_count_total_lines_empty() {
        assert_eq!(count_total_lines(""), 0);
    }

    #[test]
    fn test_count_total_lines_single_line() {
        assert_eq!(count_total_lines("fn main() {}"), 1);
    }

    #[test]
    fn test_count_total_lines_multiple_lines() {
        let content = "fn main() {\n    println!(\"hello\");\n}";
        assert_eq!(count_total_lines(content), 3);
    }

    #[test]
    fn test_count_code_lines_simple() {
        // Blank lines should not be counted as code.
        let content = "fn main() {\n\n\n}";
        assert_eq!(count_code_lines(content), 2); // "fn main() {" and "}"
    }

    #[test]
    fn test_count_code_lines_with_comments() {
        // Comments should not be counted as code lines.
        // "// comment" is skipped (// comment)
        // "fn main() {}" is code
        // "/* block */" is skipped (/* block */)
        let content = "// comment\nfn main() {}\n/* block */";
        assert_eq!(count_code_lines(content), 1); // "fn main() {}"
    }

    #[test]
    fn test_count_code_lines_with_doc_comments() {
        // All comment variants should be skipped.
        let content = "/// doc comment\n//! inner doc\nfn main() {}\n/* multi\n   line */";
        assert_eq!(count_code_lines(content), 1); // "fn main() {}"
    }

    #[test]
    fn test_collector_reports_total_lines() {
        let content = "fn main() {\n    let x = 1;\n    let y = 2;\n}";
        let output = run_on_content(content);
        let details = parse_details(&output);
        assert_eq!(details["files"], 1);
        assert_eq!(details["maxLinesPerFile"], 4);
    }

    #[test]
    fn test_collector_reports_code_lines() {
        let content = "// comment\n\nfn main() {\n    let x = 1;\n}";
        let output = run_on_content(content);
        let details = parse_details(&output);
        // 1 line for fn main, 1 for let x = 1, 1 for }
        // The empty line and comment line are not code lines.
        assert!(details["maxCodeLinesPerFile"].as_u64().unwrap() >= 3);
    }

    // --- Function detection tests ---

    #[test]
    fn test_detect_free_function() {
        let content = "fn free_function(a: i32, b: String) -> bool {\n    true\n}";
        let dir = temp_file(content);
        let functions = extract_functions(content, &dir.path().join("test.rs"));
        assert_eq!(functions.len(), 1);
        assert_eq!(functions[0].name, "free_function");
        assert_eq!(functions[0].param_count, 2);
        assert!(functions[0].total_lines >= 3);
    }

    #[test]
    fn test_detect_method_in_impl() {
        let content = r#"
impl Foo {
    fn method(&self, x: i32) -> i32 {
        x
    }
}
"#;
        let dir = temp_file(content);
        let functions = extract_functions(content, &dir.path().join("test.rs"));
        assert_eq!(functions.len(), 1);
        assert_eq!(functions[0].name, "method");
        assert_eq!(functions[0].param_count, 2); // &self, x
    }

    #[test]
    fn test_detect_async_fn() {
        let content = "async fn async_main() {\n    println!(\"hello\");\n}";
        let dir = temp_file(content);
        let functions = extract_functions(content, &dir.path().join("test.rs"));
        assert_eq!(functions.len(), 1);
        assert_eq!(functions[0].name, "async_main");
    }

    #[test]
    fn test_detect_unsafe_fn() {
        let content = "unsafe fn unsafe_read(ptr: *const i32) -> i32 {\n    *ptr\n}";
        let dir = temp_file(content);
        let functions = extract_functions(content, &dir.path().join("test.rs"));
        assert_eq!(functions.len(), 1);
        assert_eq!(functions[0].name, "unsafe_read");
        assert_eq!(functions[0].param_count, 1);
    }

    #[test]
    fn test_parameter_count_free_function() {
        let content = "fn three_params(a: i32, b: i32, c: i32) -> i32 {\n    a + b + c\n}";
        let dir = temp_file(content);
        let functions = extract_functions(content, &dir.path().join("test.rs"));
        assert_eq!(functions[0].param_count, 3);
    }

    #[test]
    fn test_parameter_count_with_self() {
        let content =
            "impl Bar {\n    fn with_self(&self, x: usize) -> usize {\n        x\n    }\n}";
        let dir = temp_file(content);
        let functions = extract_functions(content, &dir.path().join("test.rs"));
        assert_eq!(functions[0].param_count, 2); // &self, x
    }

    // --- Violation tests ---

    #[test]
    fn test_violation_max_lines_per_file() {
        let mut collector = SizeCollector::new();
        collector.config.max_lines_per_file = Some(5);

        // File with exactly 5 lines (threshold = 5, so no violation).
        let dir = temp_file("fn main() {\n    let a = 1;\n    let b = 2;\n    let c = 3;\n}");
        let ctx = Context::new(dir.path().to_path_buf());
        let output = collector.collect(&ctx).unwrap();
        let details: serde_json::Value = serde_json::from_str(&output.stdout).unwrap();

        // 5 lines: "fn main() {", "let a", "let b", "let c", "}"
        assert_eq!(details["violations"].as_array().unwrap().len(), 0);
        assert_eq!(details["maxLinesPerFile"], 5);

        // File with 6 lines against threshold 5 should produce violation.
        let dir2 = temp_file(
            "fn main() {\n    let a = 1;\n    let b = 2;\n    let c = 3;\n    let d = 4;\n}",
        );
        let ctx2 = Context::new(dir2.path().to_path_buf());
        let output2 = collector.collect(&ctx2).unwrap();
        let details2: serde_json::Value = serde_json::from_str(&output2.stdout).unwrap();
        assert_eq!(details2["violations"].as_array().unwrap().len(), 1);
        assert_eq!(
            details2["violations"][0]["ruleId"],
            "size:max-lines-per-file"
        );
    }

    #[test]
    fn test_violation_max_code_lines_per_file() {
        let mut collector = SizeCollector::new();
        collector.config.max_code_lines_per_file = Some(3);

        let content = "// comment line 1\n// comment line 2\nfn main() {\n    let a = 1;\n    let b = 2;\n    let c = 3;\n}";
        let dir = temp_file(content);
        let ctx = Context::new(dir.path().to_path_buf());
        let output = collector.collect(&ctx).unwrap();
        let details: serde_json::Value = serde_json::from_str(&output.stdout).unwrap();

        // 4 code lines (fn main, let a, let b, let c) against threshold 3.
        assert_eq!(details["violations"].as_array().unwrap().len(), 1);
        assert_eq!(
            details["violations"][0]["ruleId"],
            "size:max-code-lines-per-file"
        );
    }

    #[test]
    fn test_violation_max_lines_per_function() {
        let mut collector = SizeCollector::new();
        collector.config.max_lines_per_function = Some(3);

        let content = "fn short() {\n    1;\n}\n\nfn longer() {\n    1;\n    2;\n    3;\n    4;\n}";
        let dir = temp_file(content);
        let ctx = Context::new(dir.path().to_path_buf());
        let output = collector.collect(&ctx).unwrap();
        let details: serde_json::Value = serde_json::from_str(&output.stdout).unwrap();

        let violations = details["violations"].as_array().unwrap();
        assert_eq!(violations.len(), 1);
        assert_eq!(violations[0]["ruleId"], "size:max-lines-per-function");
        assert_eq!(violations[0]["function"], "longer");
    }

    #[test]
    fn test_violation_max_parameters_per_function() {
        let mut collector = SizeCollector::new();
        collector.config.max_parameters_per_function = Some(2);

        let content =
            "fn two_params(a: i32, b: i32) {}\n\nfn three_params(a: i32, b: i32, c: i32) {}";
        let dir = temp_file(content);
        let ctx = Context::new(dir.path().to_path_buf());
        let output = collector.collect(&ctx).unwrap();
        let details: serde_json::Value = serde_json::from_str(&output.stdout).unwrap();

        let violations = details["violations"].as_array().unwrap();
        assert_eq!(violations.len(), 1);
        assert_eq!(violations[0]["ruleId"], "size:max-parameters-per-function");
        assert_eq!(violations[0]["function"], "three_params");
    }

    #[test]
    fn test_no_config_no_violations() {
        // Without any configuration, the collector should report metrics but not fail.
        let content = "fn huge_function(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32, g: i32) {\n    1;\n    2;\n    3;\n    4;\n    5;\n    6;\n    7;\n    8;\n    9;\n    10;\n}";
        let output = run_on_content(content);
        let details: serde_json::Value = serde_json::from_str(&output.stdout).unwrap();

        // No violations when not configured.
        assert_eq!(details["violations"].as_array().unwrap().len(), 0);
        assert_eq!(output.status, crate::schema::CollectorStatus::Pass);
    }

    #[test]
    fn test_collector_name() {
        let collector = SizeCollector::new();
        assert_eq!(collector.name(), "size");
    }

    #[test]
    fn test_collector_available() {
        let collector = SizeCollector::new();
        assert!(collector.is_available());
    }

    #[test]
    fn test_multiple_functions_same_name() {
        // Multiple functions with the same name in different impl blocks.
        let content = r#"
impl Foo {
    fn bar(x: i32) -> i32 { x }
}

impl Baz {
    fn bar(y: i32) -> i32 { y }
}
"#;
        let dir = temp_file(content);
        let functions = extract_functions(content, &dir.path().join("test.rs"));
        assert_eq!(functions.len(), 2);
    }
}