debtmap/core/
traits.rs

1//! Core trait definitions for clean module boundaries
2//!
3//! This module contains all shared trait definitions that establish
4//! clear contracts between different parts of the codebase.
5
6use anyhow::Result;
7use serde::{Deserialize, Serialize};
8use std::path::Path;
9
10/// Core analyzer trait for all language analyzers
11pub trait Analyzer: Send + Sync {
12    /// The input type this analyzer processes
13    type Input;
14    /// The output type this analyzer produces
15    type Output;
16
17    /// Analyze the given input and produce output
18    fn analyze(&self, input: Self::Input) -> Result<Self::Output>;
19
20    /// Get the name of this analyzer for reporting
21    fn name(&self) -> &str;
22}
23
24/// Trait for scoring technical debt items
25pub trait Scorer: Send + Sync {
26    /// The item type to be scored
27    type Item;
28
29    /// Calculate a score for the given item
30    fn score(&self, item: &Self::Item) -> f64;
31
32    /// Get a description of the scoring methodology
33    fn methodology(&self) -> &str;
34}
35
36/// File system operations abstraction
37pub trait FileSystem: Send + Sync {
38    /// Read file contents as string
39    fn read_file(&self, path: &Path) -> Result<String>;
40
41    /// Write string contents to file
42    fn write_file(&self, path: &Path, contents: &str) -> Result<()>;
43
44    /// Check if a path exists
45    fn exists(&self, path: &Path) -> bool;
46
47    /// List all files matching a pattern
48    fn glob(&self, pattern: &str) -> Result<Vec<std::path::PathBuf>>;
49}
50
51/// Parser trait for language-specific parsing
52pub trait Parser: Send + Sync {
53    /// The AST type produced by this parser
54    type Ast;
55
56    /// Parse source code into an AST
57    fn parse(&self, source: &str, path: &Path) -> Result<Self::Ast>;
58
59    /// Get the language this parser handles
60    fn language(&self) -> &str;
61}
62
63/// Complexity calculator trait
64pub trait ComplexityCalculator: Send + Sync {
65    /// The input type for complexity calculation
66    type Input;
67
68    /// Calculate cyclomatic complexity
69    fn cyclomatic_complexity(&self, input: &Self::Input) -> u32;
70
71    /// Calculate cognitive complexity
72    fn cognitive_complexity(&self, input: &Self::Input) -> u32;
73
74    /// Calculate halstead metrics
75    fn halstead_metrics(&self, input: &Self::Input) -> HalsteadMetrics;
76}
77
78/// Halstead metrics
79#[derive(Debug, Clone, Serialize, Deserialize)]
80pub struct HalsteadMetrics {
81    pub volume: f64,
82    pub difficulty: f64,
83    pub effort: f64,
84    pub time: f64,
85    pub bugs: f64,
86}
87
88/// Report formatter trait
89pub trait Formatter: Send + Sync {
90    /// The report type to format
91    type Report;
92
93    /// Format a report as string
94    fn format(&self, report: &Self::Report) -> Result<String>;
95
96    /// Get the output format name (json, markdown, etc)
97    fn format_name(&self) -> &str;
98}
99
100/// Configuration provider trait
101pub trait ConfigProvider: Send + Sync {
102    /// Get configuration value by key
103    fn get(&self, key: &str) -> Option<String>;
104
105    /// Set configuration value
106    fn set(&mut self, key: String, value: String);
107
108    /// Load configuration from file
109    fn load_from_file(&self, path: &Path) -> Result<()>;
110}
111
112/// Detector trait for pattern detection
113pub trait Detector: Send + Sync {
114    /// The context type this detector analyzes
115    type Context;
116    /// The detection result type
117    type Detection;
118
119    /// Detect patterns in the given context
120    fn detect(&self, context: &Self::Context) -> Vec<Self::Detection>;
121
122    /// Get detector confidence level
123    fn confidence(&self) -> f64;
124}
125
126/// Priority calculator trait
127pub trait PriorityCalculator: Send + Sync {
128    /// The item type to prioritize
129    type Item;
130
131    /// Calculate priority score (0.0 to 1.0)
132    fn calculate_priority(&self, item: &Self::Item) -> f64;
133
134    /// Get factors that influenced the priority
135    fn get_factors(&self, item: &Self::Item) -> Vec<PriorityFactor>;
136}
137
138/// Priority factor
139#[derive(Debug, Clone, Serialize, Deserialize)]
140pub struct PriorityFactor {
141    pub name: String,
142    pub weight: f64,
143    pub value: f64,
144    pub description: String,
145}
146
147/// Refactoring opportunity detector
148pub trait RefactoringDetector: Send + Sync {
149    /// The code context to analyze
150    type Context;
151    /// The refactoring opportunity type
152    type Opportunity;
153
154    /// Detect refactoring opportunities
155    fn detect_opportunities(&self, context: &Self::Context) -> Vec<Self::Opportunity>;
156
157    /// Estimate effort for a refactoring
158    fn estimate_effort(&self, opportunity: &Self::Opportunity) -> EffortEstimate;
159}
160
161/// Effort estimate for refactoring
162#[derive(Debug, Clone, Serialize, Deserialize)]
163pub struct EffortEstimate {
164    pub hours: f64,
165    pub complexity: String,
166    pub risk_level: String,
167    pub confidence: f64,
168}
169
170/// Test analyzer trait
171pub trait TestAnalyzer: Send + Sync {
172    /// The test suite type
173    type TestSuite;
174
175    /// Analyze test coverage
176    fn analyze_coverage(&self, suite: &Self::TestSuite) -> CoverageReport;
177
178    /// Detect test smells
179    fn detect_test_smells(&self, suite: &Self::TestSuite) -> Vec<TestSmell>;
180}
181
182/// Coverage report
183#[derive(Debug, Clone, Serialize, Deserialize)]
184pub struct CoverageReport {
185    pub line_coverage: f64,
186    pub branch_coverage: f64,
187    pub function_coverage: f64,
188    pub uncovered_lines: Vec<usize>,
189}
190
191/// Test smell detection
192#[derive(Debug, Clone, Serialize, Deserialize)]
193pub struct TestSmell {
194    pub smell_type: String,
195    pub location: String,
196    pub severity: String,
197    pub suggestion: String,
198}
199
200/// Repository trait for data persistence
201pub trait Repository<T>: Send + Sync {
202    /// Save an entity
203    fn save(&mut self, entity: T) -> Result<()>;
204
205    /// Find entity by id
206    fn find_by_id(&self, id: &str) -> Option<T>;
207
208    /// Find all entities
209    fn find_all(&self) -> Vec<T>;
210
211    /// Delete entity by id
212    fn delete(&mut self, id: &str) -> Result<()>;
213}
214
215/// Event publisher trait for decoupled communication
216pub trait EventPublisher: Send + Sync {
217    /// The event type to publish
218    type Event;
219
220    /// Publish an event
221    fn publish(&self, event: Self::Event) -> Result<()>;
222}
223
224/// Event subscriber trait
225pub trait EventSubscriber: Send + Sync {
226    /// The event type to subscribe to
227    type Event;
228
229    /// Handle an event
230    fn handle(&mut self, event: Self::Event) -> Result<()>;
231}