churnloom 0.1.0

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

use anyhow::{Context, Result};
use tokei::{Config, LanguageType, Languages};

use crate::model::LineStats;

const DEFAULT_EXCLUDES: &[&str] = &[
    ".git",
    "target",
    "node_modules",
    "third_party",
    "vendor",
    "assets",
    "captures",
    "artifacts",
    "fonts",
    "licenses",
    "docs/archive",
];

#[derive(Debug)]
pub struct SourceFile {
    pub absolute_path: PathBuf,
    pub relative_path: String,
    pub language: String,
    pub lines: LineStats,
}

pub fn discover_sources(
    root: &Path,
    all_files: bool,
    include_generated: bool,
    extra_excludes: &[String],
    extensions: &[String],
    include_prefixes: &[String],
    exclude_prefixes: &[String],
) -> Result<Vec<SourceFile>> {
    let mut config = Config {
        hidden: Some(all_files),
        no_ignore: Some(all_files),
        ..Config::default()
    };
    // We do not need Tokei's per-file callback and being explicit makes the intent clear.
    config.for_each_fn = None;
    config.types = language_types_for_extensions(extensions);

    let exclusions = DEFAULT_EXCLUDES
        .iter()
        .map(ToString::to_string)
        .chain(extra_excludes.iter().cloned())
        .collect::<Vec<_>>();
    let exclusion_refs = exclusions.iter().map(String::as_str).collect::<Vec<_>>();

    let mut languages = Languages::new();
    languages.get_statistics(&[root], &exclusion_refs, &config);

    let mut files = Vec::new();
    for (language_type, language) in &languages {
        for report in &language.reports {
            let absolute_path = absolute(root, &report.name);
            let relative = absolute_path.strip_prefix(root).with_context(|| {
                format!(
                    "Tokei returned a path outside the project: {}",
                    absolute_path.display()
                )
            })?;
            let relative_path = slash_path(relative);
            if !include_generated && is_generated(&relative_path) {
                continue;
            }
            if !matches_source_filters(
                &relative_path,
                extensions,
                include_prefixes,
                exclude_prefixes,
            ) {
                continue;
            }
            let stats = report.stats.summarise();
            files.push(SourceFile {
                absolute_path,
                relative_path,
                language: language_type.to_string(),
                lines: LineStats {
                    code: stats.code,
                    comments: stats.comments,
                    blanks: stats.blanks,
                },
            });
        }
    }
    files.sort_by(|left, right| left.relative_path.cmp(&right.relative_path));
    Ok(files)
}

fn language_types_for_extensions(extensions: &[String]) -> Option<Vec<LanguageType>> {
    if extensions.is_empty() {
        return None;
    }
    let mut types = Vec::new();
    for extension in extensions {
        if let Some(language) = LanguageType::from_file_extension(extension)
            && !types.contains(&language)
        {
            types.push(language);
        }
    }
    Some(types)
}

fn matches_source_filters(
    path: &str,
    extensions: &[String],
    include_prefixes: &[String],
    exclude_prefixes: &[String],
) -> bool {
    let extension_matches = extensions.is_empty()
        || Path::new(path)
            .extension()
            .and_then(|extension| extension.to_str())
            .is_some_and(|extension| extensions.iter().any(|value| value == extension));
    extension_matches
        && (include_prefixes.is_empty()
            || include_prefixes
                .iter()
                .any(|prefix| path.starts_with(prefix)))
        && !exclude_prefixes
            .iter()
            .any(|prefix| path.starts_with(prefix))
}

fn absolute(root: &Path, path: &Path) -> PathBuf {
    if path.is_absolute() {
        path.to_path_buf()
    } else {
        root.join(path)
    }
}

pub fn slash_path(path: &Path) -> String {
    path.components()
        .map(|component| component.as_os_str().to_string_lossy())
        .collect::<Vec<_>>()
        .join("/")
}

fn is_generated(path: &str) -> bool {
    let file_name = Path::new(path)
        .file_name()
        .and_then(|name| name.to_str())
        .unwrap_or_default();
    path.split('/')
        .any(|component| matches!(component, "generated" | "gen"))
        || matches!(file_name, "struct_defaults.rs" | "pnext_impls.rs")
        || file_name.ends_with(".generated.rs")
}

#[cfg(test)]
mod tests {
    use super::{is_generated, matches_source_filters};

    #[test]
    fn recognizes_generated_paths_without_hiding_normal_generators() {
        assert!(is_generated("src/generated/types.rs"));
        assert!(is_generated("src/model.generated.rs"));
        assert!(!is_generated("src/generator.rs"));
    }

    #[test]
    fn source_filters_compose_extension_and_prefix_constraints() {
        let extensions = vec!["rs".to_string()];
        let includes = vec!["crates/".to_string()];
        let excludes = vec!["crates/generated/".to_string()];

        assert!(matches_source_filters(
            "crates/app/src/lib.rs",
            &extensions,
            &includes,
            &excludes,
        ));
        assert!(!matches_source_filters(
            "scripts/tool.rs",
            &extensions,
            &includes,
            &excludes,
        ));
        assert!(!matches_source_filters(
            "crates/app/src/lib.py",
            &extensions,
            &includes,
            &excludes,
        ));
        assert!(!matches_source_filters(
            "crates/generated/schema.rs",
            &extensions,
            &includes,
            &excludes,
        ));
    }
}