Skip to main content

cargo_coupling/balance/
issue_type.rs

1/// Types of coupling problems
2#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
3pub enum IssueType {
4    /// Strong coupling spanning a long distance
5    GlobalComplexity,
6    /// Strong coupling to a frequently changing component
7    CascadingChangeRisk,
8    /// Intrusive coupling across boundaries (field/internals access)
9    InappropriateIntimacy,
10    /// A module with too many dependencies
11    HighEfferentCoupling,
12    /// A module that too many others depend on
13    HighAfferentCoupling,
14    /// Weak coupling where stronger might be appropriate
15    UnnecessaryAbstraction,
16    /// Circular dependency detected
17    CircularDependency,
18    /// Strong temporal co-change without an explicit code dependency
19    HiddenCoupling,
20    /// Supporting or generic module changing more often than expected
21    AccidentalVolatility,
22    /// Direct coupling to a third-party crate is spread across many modules
23    ScatteredExternalCoupling,
24
25    // === APOSD-inspired issues (A Philosophy of Software Design) ===
26    /// Module with interface complexity close to implementation complexity
27    ShallowModule,
28    /// Method that only delegates to another method without adding value
29    PassThroughMethod,
30    /// Module requiring too much knowledge to understand/modify
31    HighCognitiveLoad,
32
33    // === Khononov/Rust-specific issues ===
34    /// Module with too many functions, types, or implementations
35    GodModule,
36    /// Public fields exposed to external modules (should use getters/methods)
37    PublicFieldExposure,
38    /// Functions with too many primitive parameters (consider newtype)
39    PrimitiveObsession,
40}
41
42impl std::fmt::Display for IssueType {
43    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44        match self {
45            IssueType::GlobalComplexity => write!(f, "Global Complexity"),
46            IssueType::CascadingChangeRisk => write!(f, "Cascading Change Risk"),
47            IssueType::InappropriateIntimacy => write!(f, "Inappropriate Intimacy"),
48            IssueType::HighEfferentCoupling => write!(f, "High Efferent Coupling"),
49            IssueType::HighAfferentCoupling => write!(f, "High Afferent Coupling"),
50            IssueType::UnnecessaryAbstraction => write!(f, "Unnecessary Abstraction"),
51            IssueType::CircularDependency => write!(f, "Circular Dependency"),
52            IssueType::HiddenCoupling => write!(f, "Hidden Coupling"),
53            IssueType::AccidentalVolatility => write!(f, "Accidental Volatility"),
54            IssueType::ScatteredExternalCoupling => write!(f, "Scattered External Coupling"),
55            // APOSD-inspired
56            IssueType::ShallowModule => write!(f, "Shallow Module"),
57            IssueType::PassThroughMethod => write!(f, "Pass-Through Method"),
58            IssueType::HighCognitiveLoad => write!(f, "High Cognitive Load"),
59            // Khononov/Rust-specific
60            IssueType::GodModule => write!(f, "God Module"),
61            IssueType::PublicFieldExposure => write!(f, "Public Field Exposure"),
62            IssueType::PrimitiveObsession => write!(f, "Primitive Obsession"),
63        }
64    }
65}
66
67impl IssueType {
68    /// Get a detailed description of what this issue type means
69    pub fn description(&self) -> &'static str {
70        match self {
71            IssueType::GlobalComplexity => {
72                "Strong coupling to distant components increases cognitive load and makes the system harder to understand and modify."
73            }
74            IssueType::CascadingChangeRisk => {
75                "Strongly coupling to volatile components means changes will cascade through the system, requiring updates in many places."
76            }
77            IssueType::InappropriateIntimacy => {
78                "Direct access to internal details (fields, private methods) across module boundaries violates encapsulation."
79            }
80            IssueType::HighEfferentCoupling => {
81                "A module depending on too many others is fragile and hard to test. Changes anywhere affect this module."
82            }
83            IssueType::HighAfferentCoupling => {
84                "A module that many others depend on is hard to change. Any modification risks breaking dependents."
85            }
86            IssueType::UnnecessaryAbstraction => {
87                "Using abstract interfaces for closely-related stable components may add complexity without benefit."
88            }
89            IssueType::CircularDependency => {
90                "Circular dependencies make it impossible to understand, test, or modify components in isolation."
91            }
92            IssueType::HiddenCoupling => {
93                "Files frequently change together without an explicit code dependency. This suggests implicit shared knowledge or a missing abstraction."
94            }
95            IssueType::AccidentalVolatility => {
96                "A supporting or generic subdomain changes frequently despite being expected to be stable. This suggests churn from design or ownership issues rather than essential business volatility."
97            }
98            IssueType::ScatteredExternalCoupling => {
99                "A third-party crate is used directly from many internal modules, spreading upgrade and API-change risk across code you control."
100            }
101            // APOSD-inspired descriptions
102            IssueType::ShallowModule => {
103                "Interface complexity is close to implementation complexity. The module doesn't hide enough complexity behind a simple interface. (APOSD: Deep vs Shallow Modules)"
104            }
105            IssueType::PassThroughMethod => {
106                "Method only delegates to another method without adding significant functionality. Indicates unclear responsibility division. (APOSD: Pass-Through Methods)"
107            }
108            IssueType::HighCognitiveLoad => {
109                "Module requires too much knowledge to understand and modify. Too many public APIs, dependencies, or complex type signatures. (APOSD: Cognitive Load)"
110            }
111            // Khononov/Rust-specific descriptions
112            IssueType::GodModule => {
113                "Module has too many responsibilities - too many functions, types, or implementations. Consider splitting into focused, cohesive modules. (SRP violation)"
114            }
115            IssueType::PublicFieldExposure => {
116                "Struct has public fields accessed from other modules. Consider using getter methods to reduce coupling and allow future implementation changes."
117            }
118            IssueType::PrimitiveObsession => {
119                "Function has many primitive parameters of the same type. Consider using newtype pattern (e.g., `struct UserId(u64)`) for type safety and clarity."
120            }
121        }
122    }
123
124    /// Get a Japanese description of what this issue type means.
125    pub fn description_japanese(&self) -> &'static str {
126        match self {
127            IssueType::GlobalComplexity => {
128                "遠いコンポーネントへの強い結合は認知負荷を高め、理解や変更を難しくします。"
129            }
130            IssueType::CascadingChangeRisk => {
131                "頻繁に変わるコンポーネントへ強く結合すると、変更がシステム全体に波及しやすくなります。"
132            }
133            IssueType::InappropriateIntimacy => {
134                "モジュール境界を越えた内部詳細への直接アクセスはカプセル化を損ないます。"
135            }
136            IssueType::HighEfferentCoupling => {
137                "多くのモジュールに依存するモジュールは壊れやすく、テストも難しくなります。"
138            }
139            IssueType::HighAfferentCoupling => {
140                "多くのモジュールから依存されるモジュールは変更しづらく、依存元を壊すリスクがあります。"
141            }
142            IssueType::UnnecessaryAbstraction => {
143                "近く安定したコンポーネントに抽象インターフェースを使うと、利益より複雑さが増える場合があります。"
144            }
145            IssueType::CircularDependency => {
146                "循環依存はコンポーネントを単独で理解、テスト、変更することを難しくします。"
147            }
148            IssueType::HiddenCoupling => {
149                "明示的なコード依存がないのにファイルが頻繁に一緒に変わっています。暗黙の知識や不足した抽象化を示している可能性があります。"
150            }
151            IssueType::AccidentalVolatility => {
152                "安定しているはずの支援/汎用サブドメインが頻繁に変更されています。設計や所有権の問題によるチャーンの可能性があります。"
153            }
154            IssueType::ScatteredExternalCoupling => {
155                "サードパーティクレートが多くの内部モジュールから直接使われており、更新やAPI変更のリスクが広がっています。"
156            }
157            IssueType::ShallowModule => {
158                "インターフェースの複雑さが実装の複雑さに近く、単純なインターフェースの背後に十分な複雑さを隠せていません。"
159            }
160            IssueType::PassThroughMethod => {
161                "メソッドが価値を追加せず別メソッドへ委譲しており、責務分担が曖昧な可能性があります。"
162            }
163            IssueType::HighCognitiveLoad => {
164                "公開API、依存、複雑な型シグネチャが多く、理解や変更に必要な知識が多すぎます。"
165            }
166            IssueType::GodModule => {
167                "関数、型、実装が多すぎて責務が集中しています。焦点の絞られたモジュールへの分割を検討してください。"
168            }
169            IssueType::PublicFieldExposure => {
170                "構造体の公開フィールドが他モジュールから使われています。getterなどで結合を弱めることを検討してください。"
171            }
172            IssueType::PrimitiveObsession => {
173                "同じプリミティブ型の引数が多すぎます。newtypeパターンで型安全性と明確さを高めることを検討してください。"
174            }
175        }
176    }
177}