Skip to main content

dupes_core/
analyzer.rs

1use std::path::Path;
2
3use crate::code_unit::CodeUnit;
4use crate::config::AnalysisConfig;
5
6/// Trait for language-specific code analysis.
7///
8/// Implementors provide file-extension detection and parsing logic,
9/// allowing `dupes-core` to work with any language.
10///
11/// **Test code handling:** Analyzers should set [`CodeUnit::is_test`] to `true`
12/// for test functions, test modules, etc. The [`crate::analyze`] function will
13/// filter them out when `Config::exclude_tests` is enabled, using [`is_test_code`].
14pub trait LanguageAnalyzer: Send + Sync {
15    /// File extensions this analyzer handles (without the leading dot).
16    fn file_extensions(&self) -> &[&str];
17
18    /// Parse a single source file into code units.
19    ///
20    /// `path` is the file's location (for diagnostics), `source` is the file content,
21    /// and `config` carries min-node / min-line thresholds.
22    ///
23    /// Analyzers should tag test code via [`CodeUnit::is_test`] rather than
24    /// filtering it out; the caller handles exclusion.
25    fn parse_file(
26        &self,
27        path: &Path,
28        source: &str,
29        config: &AnalysisConfig,
30    ) -> Result<Vec<CodeUnit>, Box<dyn std::error::Error + Send + Sync>>;
31
32    /// Check whether a code unit represents test code.
33    ///
34    /// The default implementation delegates to [`CodeUnit::is_test`],
35    /// which language analyzers set during parsing.
36    fn is_test_code(&self, unit: &CodeUnit) -> bool {
37        unit.is_test
38    }
39}