Skip to main content

cha_core/
model.rs

1/// Where a referenced type is defined, from the perspective of the file that
2/// uses it. Used by abstraction-boundary analyses to distinguish "own domain"
3/// types from "pulled in from a library" types.
4#[derive(Debug, Clone, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
5#[serde(rename_all = "snake_case", tag = "kind", content = "module")]
6pub enum TypeOrigin {
7    /// Declared inside the project (resolved via project-wide type registry
8    /// or an import pointing at a project-local path).
9    Local,
10    /// Imported from an external module / crate / package. Carries the module
11    /// name if known (Rust crate path root, Go module path, npm package name,
12    /// C header filename without extension). May be empty if only structure
13    /// says "external" (e.g. `#include <...>` without the header name).
14    External(String),
15    /// Built-in primitive / standard library scalar (int, bool, &str, char…).
16    Primitive,
17    /// Could not be resolved. Detection treats this as potentially external
18    /// but with lower confidence.
19    #[default]
20    Unknown,
21}
22
23/// A function parameter's (or return value's) type, with resolved origin.
24/// Produced by parsers after combining AST type text with the file's imports.
25#[derive(Debug, Clone, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
26pub struct TypeRef {
27    /// Innermost identifier after stripping references, generics, containers.
28    /// e.g. `&mut Vec<tree_sitter::Node>` → `"Node"`.
29    pub name: String,
30    /// Original source text as written, for messages and debugging.
31    /// e.g. `"&mut Vec<tree_sitter::Node>"`.
32    pub raw: String,
33    /// Where the type is declared.
34    pub origin: TypeOrigin,
35}
36
37/// Extracted function info from AST.
38#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
39pub struct FunctionInfo {
40    pub name: String,
41    pub start_line: usize,
42    pub end_line: usize,
43    /// 0-based column of the function name identifier.
44    pub name_col: usize,
45    /// 0-based end column of the function name identifier.
46    pub name_end_col: usize,
47    pub line_count: usize,
48    /// Cyclomatic complexity (1 + number of branch points).
49    pub complexity: usize,
50    /// Hash of the function body AST structure for duplicate detection.
51    pub body_hash: Option<u64>,
52    /// Whether this function is exported (pub/export).
53    pub is_exported: bool,
54    /// Number of parameters.
55    pub parameter_count: usize,
56    /// Names of external identifiers referenced in the body (for Feature Envy).
57    pub external_refs: Vec<String>,
58    /// Max method chain depth in the body (for Message Chains).
59    pub chain_depth: usize,
60    /// Number of switch/match arms (for Switch Statements).
61    pub switch_arms: usize,
62    /// Whether this function only delegates to another object's method (for Middle Man).
63    pub is_delegating: bool,
64    /// Parameter types **in declaration order**, each resolved to a TypeRef.
65    /// Preserves position (first param = index 0) so positional analyses work.
66    pub parameter_types: Vec<TypeRef>,
67    /// Number of comment lines in the function body.
68    pub comment_lines: usize,
69    /// Field names referenced in this function body (for Temporary Field).
70    pub referenced_fields: Vec<String>,
71    /// Field names checked for null/None in this function (for Null Object pattern).
72    pub null_check_fields: Vec<String>,
73    /// The field/variable name being dispatched on in switch/match (for Strategy/State).
74    pub switch_dispatch_target: Option<String>,
75    /// Number of optional parameters (for Builder pattern).
76    pub optional_param_count: usize,
77    /// Names of functions/methods called in this function body (for call graph).
78    pub called_functions: Vec<String>,
79    /// Cognitive complexity score [SonarSource 2017] — nesting-aware understandability metric.
80    pub cognitive_complexity: usize,
81}
82
83/// Extracted class/struct info from AST.
84#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
85pub struct ClassInfo {
86    pub name: String,
87    pub start_line: usize,
88    pub end_line: usize,
89    /// 0-based column of the class/struct name identifier.
90    pub name_col: usize,
91    /// 0-based end column of the class/struct name identifier.
92    pub name_end_col: usize,
93    pub method_count: usize,
94    pub line_count: usize,
95    /// Whether this class is exported.
96    pub is_exported: bool,
97    /// Number of methods that only delegate to another object.
98    pub delegating_method_count: usize,
99    /// Number of fields/properties.
100    pub field_count: usize,
101    /// Field names declared in this class.
102    pub field_names: Vec<String>,
103    /// Field types (parallel to field_names).
104    pub field_types: Vec<String>,
105    /// Whether the class has non-accessor methods (business logic).
106    pub has_behavior: bool,
107    /// Whether this is an interface or abstract class.
108    pub is_interface: bool,
109    /// Parent class/trait name (for Refused Bequest).
110    pub parent_name: Option<String>,
111    /// Number of overridden methods (for Refused Bequest).
112    pub override_count: usize,
113    /// Number of self-method calls in the longest method (for Template Method).
114    pub self_call_count: usize,
115    /// Whether the class has a listener/callback collection field.
116    pub has_listener_field: bool,
117    /// Whether the class has a notify/emit method.
118    pub has_notify_method: bool,
119}
120
121/// Extracted import info.
122#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
123pub struct ImportInfo {
124    pub source: String,
125    pub line: usize,
126    /// 0-based column of the import statement.
127    pub col: usize,
128    /// True for module declarations (e.g. Rust `mod foo;`).
129    pub is_module_decl: bool,
130}
131
132/// A comment extracted from source code by the language parser.
133#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
134pub struct CommentInfo {
135    pub text: String,
136    pub line: usize,
137}
138
139/// Unified source model produced by parsing.
140#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
141pub struct SourceModel {
142    pub language: String,
143    pub total_lines: usize,
144    pub functions: Vec<FunctionInfo>,
145    pub classes: Vec<ClassInfo>,
146    pub imports: Vec<ImportInfo>,
147    pub comments: Vec<CommentInfo>,
148    /// Type aliases: (alias, original). e.g. typedef, using, type =
149    pub type_aliases: Vec<(String, String)>,
150}