Skip to main content

Crate cargo_quality

Crate cargo_quality 

Source
Expand description

Professional Rust code quality analysis with hardcoded standards.

cargo-quality is a zero-configuration code quality tool that enforces consistent standards across Rust projects. All rules are embedded in the binary, ensuring uniform analysis across your entire codebase.

§Overview

This library provides:

  • analyzer - Core trait and types for building analyzers
  • analyzers - Built-in analyzers for common code quality issues
  • formatter - Code formatting with hardcoded standards
  • differ - Diff generation and visualization
  • report - Analysis report generation
  • error - Error types for quality operations

§Quick Start

Analyze code for path imports that should be extracted:

use cargo_quality::{analyzer::Analyzer, analyzers::PathImportAnalyzer};

let analyzer = PathImportAnalyzer::new();
let code = r#"
    fn main() {
        let content = std::fs::read_to_string("file.txt");
    }
"#;
let ast = syn::parse_file(code).unwrap();
let result = analyzer.analyze(&ast, code).unwrap();

assert!(!result.issues.is_empty());
assert!(result.issues[0].message.contains("Use import"));

§Available Analyzers

AnalyzerDescription
PathImportAnalyzerDetects std::fs::read paths that should use use
FormatArgsAnalyzerFinds println!("{}", x) that should use {x}
EmptyLinesAnalyzerFinds empty lines in function bodies
InlineCommentsAnalyzerFinds // comments that should be ///

§Running All Analyzers

Use analyzers::get_analyzers() to get all built-in analyzers:

use cargo_quality::{analyzer::Analyzer, analyzers::get_analyzers};

let code = r#"
    fn main() {
        let x = std::fs::read("file");
    }
"#;
let ast = syn::parse_file(code).unwrap();

for analyzer in get_analyzers() {
    let result = analyzer.analyze(&ast, code).unwrap();
    println!("[{}] {} issues", analyzer.name(), result.issues.len());
}

§Custom Analyzers

Implement the analyzer::Analyzer trait to create custom analyzers:

use cargo_quality::analyzer::{AnalysisResult, Analyzer};
use masterror::AppResult;
use syn::File;

struct MyAnalyzer;

impl Analyzer for MyAnalyzer {
    fn name(&self) -> &'static str {
        "my_analyzer"
    }

    fn analyze(&self, _ast: &File, _content: &str) -> AppResult<AnalysisResult> {
        Ok(AnalysisResult::default())
    }

    fn fix(&self, _ast: &mut File) -> AppResult<usize> {
        Ok(0)
    }
}

§Feature Flags

This crate has no optional features. All functionality is enabled by default.

§Standards

This tool enforces standards from RustManifest:

  • No inline :: paths in code (use use statements)
  • Named format arguments for readability
  • No empty lines inside function bodies
  • Doc comments only (no inline // comments)

Modules§

analyzer
Core analyzer trait and types for code quality analysis.
analyzers
Built-in code quality analyzers.
differ
Diff generation and display with responsive layout.
error
Error types for cargo-quality operations.
file_utils
formatter
mod_rs
Module for detecting and fixing mod.rs files.
report
Report formatting for analysis results.