churnloom 0.1.0

Fast codebase health reports woven from churn, LOC, structure, and Cargo ownership
Documentation
use std::path::PathBuf;

use crate::cli::Cli;

/// Configuration for a programmatic Churnloom analysis.
///
/// Start with [`AnalysisOptions::new`] and change only the fields that matter
/// for the integration. The defaults match the command-line interface.
#[derive(Clone, Debug)]
pub struct AnalysisOptions {
    /// Repository or project directory to analyze.
    pub path: PathBuf,
    /// Worker threads used for parallel counting and analysis.
    pub jobs: usize,
    /// Recent Git churn window in calendar days.
    pub short_days: u32,
    /// Longer Git churn window in calendar days.
    pub long_days: u32,
    /// Include recognized generated source files.
    pub include_generated: bool,
    /// Analyze ignored and hidden source files too.
    pub all_files: bool,
    /// Additional gitignore-style exclusion patterns.
    pub exclude: Vec<String>,
    /// File extensions to analyze, without a leading dot.
    pub extensions: Vec<String>,
    /// Repository-relative path prefixes to include.
    pub include_prefix: Vec<String>,
    /// Repository-relative path prefixes to exclude.
    pub exclude_prefix: Vec<String>,
    /// Skip Cargo workspace discovery even when `Cargo.toml` is present.
    pub no_cargo: bool,
}

impl AnalysisOptions {
    /// Creates options for `path` with the same defaults as the CLI.
    pub fn new(path: impl Into<PathBuf>) -> Self {
        Self {
            path: path.into(),
            ..Self::default()
        }
    }
}

impl Default for AnalysisOptions {
    fn default() -> Self {
        Self {
            path: PathBuf::from("."),
            jobs: default_jobs(),
            short_days: 7,
            long_days: 30,
            include_generated: false,
            all_files: false,
            exclude: Vec::new(),
            extensions: Vec::new(),
            include_prefix: Vec::new(),
            exclude_prefix: Vec::new(),
            no_cargo: false,
        }
    }
}

impl From<&Cli> for AnalysisOptions {
    fn from(cli: &Cli) -> Self {
        Self {
            path: cli.path.clone(),
            jobs: cli.jobs,
            short_days: cli.short_days,
            long_days: cli.long_days,
            include_generated: cli.source.include_generated,
            all_files: cli.source.all_files,
            exclude: cli.source.exclude.clone(),
            extensions: cli.source.extensions.clone(),
            include_prefix: cli.source.include_prefix.clone(),
            exclude_prefix: cli.source.exclude_prefix.clone(),
            no_cargo: cli.source.no_cargo,
        }
    }
}

pub(crate) fn default_jobs() -> usize {
    std::thread::available_parallelism().map_or(1, |count| count.get().min(8))
}