rust_diff_analyzer/classifier.rs
1// SPDX-FileCopyrightText: 2025 RAprogramm <andrey.rozanov.vl@gmail.com>
2// SPDX-License-Identifier: MIT
3
4pub mod attr_classifier;
5pub mod path_classifier;
6pub mod rules;
7
8use std::path::Path;
9
10use crate::{
11 config::Config,
12 types::{CodeType, SemanticUnit},
13};
14
15/// Classifies a semantic unit as production or test code
16///
17/// # Arguments
18///
19/// * `unit` - The semantic unit to classify
20/// * `path` - Path to the file containing the unit
21/// * `config` - Configuration
22///
23/// # Returns
24///
25/// Classification of the code
26///
27/// # Examples
28///
29/// ```
30/// use std::path::Path;
31///
32/// use rust_diff_analyzer::{
33/// classifier::classify_unit,
34/// config::Config,
35/// types::{LineSpan, SemanticUnit, SemanticUnitKind, Visibility},
36/// };
37///
38/// let unit = SemanticUnit::new(
39/// SemanticUnitKind::Function,
40/// "test_something".to_string(),
41/// Visibility::Private,
42/// LineSpan::new(1, 10),
43/// vec!["test".to_string()],
44/// );
45///
46/// let config = Config::default();
47/// let classification = classify_unit(&unit, Path::new("src/lib.rs"), &config);
48/// assert!(classification == rust_diff_analyzer::types::CodeType::Test);
49/// ```
50pub fn classify_unit(unit: &SemanticUnit, path: &Path, config: &Config) -> CodeType {
51 if config.is_build_script(path) {
52 return CodeType::BuildScript;
53 }
54
55 if path_classifier::is_example_path(path) {
56 return CodeType::Example;
57 }
58
59 if path_classifier::is_bench_path(path) {
60 return CodeType::Benchmark;
61 }
62
63 if config.is_test_path(path) {
64 return CodeType::Test;
65 }
66
67 if attr_classifier::is_bench_unit(unit) {
68 return CodeType::Benchmark;
69 }
70
71 if attr_classifier::is_test_unit(unit) {
72 return CodeType::Test;
73 }
74
75 if attr_classifier::is_in_test_module(unit) {
76 return CodeType::TestUtility;
77 }
78
79 if attr_classifier::has_test_feature(unit, config) {
80 return CodeType::TestUtility;
81 }
82
83 CodeType::Production
84}