rust_diff_analyzer/types/classification.rs
1// SPDX-FileCopyrightText: 2025 RAprogramm <andrey.rozanov.vl@gmail.com>
2// SPDX-License-Identifier: MIT
3
4use serde::{Deserialize, Serialize};
5
6/// Classification of code type
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
8pub enum CodeType {
9 /// Production code
10 Production,
11 /// Test code (marked with #[test] or in #[cfg(test)])
12 Test,
13 /// Test utility code (in test module but not a test)
14 TestUtility,
15 /// Benchmark code
16 Benchmark,
17 /// Example code
18 Example,
19 /// Build script code
20 BuildScript,
21}
22
23impl CodeType {
24 /// Returns string representation of code type
25 ///
26 /// # Returns
27 ///
28 /// A static string slice representing the code type
29 ///
30 /// # Examples
31 ///
32 /// ```
33 /// use rust_diff_analyzer::types::CodeType;
34 ///
35 /// assert_eq!(CodeType::Production.as_str(), "production");
36 /// assert_eq!(CodeType::Test.as_str(), "test");
37 /// ```
38 pub fn as_str(&self) -> &'static str {
39 match self {
40 Self::Production => "production",
41 Self::Test => "test",
42 Self::TestUtility => "test_utility",
43 Self::Benchmark => "benchmark",
44 Self::Example => "example",
45 Self::BuildScript => "build_script",
46 }
47 }
48
49 /// Checks if this is production code
50 ///
51 /// # Returns
52 ///
53 /// `true` if code type is Production
54 ///
55 /// # Examples
56 ///
57 /// ```
58 /// use rust_diff_analyzer::types::CodeType;
59 ///
60 /// assert!(CodeType::Production.is_production());
61 /// assert!(!CodeType::Test.is_production());
62 /// ```
63 pub fn is_production(&self) -> bool {
64 matches!(self, Self::Production)
65 }
66
67 /// Checks if this is any type of test code
68 ///
69 /// # Returns
70 ///
71 /// `true` if code type is Test, TestUtility, or Benchmark
72 ///
73 /// # Examples
74 ///
75 /// ```
76 /// use rust_diff_analyzer::types::CodeType;
77 ///
78 /// assert!(CodeType::Test.is_test_related());
79 /// assert!(CodeType::TestUtility.is_test_related());
80 /// assert!(CodeType::Benchmark.is_test_related());
81 /// assert!(!CodeType::Production.is_test_related());
82 /// ```
83 pub fn is_test_related(&self) -> bool {
84 matches!(self, Self::Test | Self::TestUtility | Self::Benchmark)
85 }
86}