Skip to main content

dry4rust/rust/
rust_analyzer.rs

1// Copyright (c) 2026 Matjaz Domen Pecan
2// Copyright 2026 Umberto Gotti <umberto.gotti@umbertogotti.dev>
3// Licensed under the MIT License
4// SPDX-License-Identifier: MIT
5
6//! Rust language analyzer for the `dry4rust` duplicate detection framework.
7//!
8//! This crate provides [`RustAnalyzer`], which implements the
9//! [`crate::analyzer::LanguageAnalyzer`] trait using `syn` for AST parsing
10//! and normalization.
11
12use std::path::Path;
13
14use super::parser::parse_source;
15use crate::analysis_config::AnalysisConfig;
16use crate::analyzer::LanguageAnalyzer;
17use crate::code_unit::CodeUnit;
18use std::error::Error;
19
20/// Rust language analyzer using syn for AST parsing.
21pub struct RustAnalyzer;
22
23impl RustAnalyzer {
24    #[must_use]
25    pub const fn new() -> Self {
26        Self
27    }
28}
29
30impl Default for RustAnalyzer {
31    fn default() -> Self {
32        Self::new()
33    }
34}
35
36impl LanguageAnalyzer for RustAnalyzer {
37    fn is_test_code(&self, unit: &CodeUnit) -> bool {
38        unit.is_test
39    }
40
41    fn file_extensions(&self) -> &[&str] {
42        &["rs"]
43    }
44
45    fn parse_file(
46        &self,
47        path: &Path,
48        source: &str,
49        config: &AnalysisConfig,
50    ) -> Result<Vec<CodeUnit>, Box<dyn Error + Send + Sync>> {
51        parse_source(path, source, config.min_nodes, config.min_lines)
52            .map_err(|e| -> Box<dyn Error + Send + Sync> { e.into() })
53    }
54}