issun_analyzer/
types.rs

1//! Core types for analysis results
2
3use serde::{Deserialize, Serialize};
4
5/// Event subscription information (EventReader<E> usage)
6#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
7pub struct EventSubscription {
8    /// Name of the struct that reads this event
9    pub subscriber: String,
10    /// Event type being subscribed to
11    pub event_type: String,
12    /// Source file path
13    pub file_path: String,
14    /// Line number where the field is defined
15    pub line: usize,
16}
17
18/// Event publication information (EventBus::publish<E>() calls)
19#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
20pub struct EventPublication {
21    /// Name of the function/method that publishes this event
22    pub publisher: String,
23    /// Event type being published
24    pub event_type: String,
25    /// Source file path
26    pub file_path: String,
27    /// Line number where publish is called
28    pub line: usize,
29}
30
31/// Complete analysis result for a single file
32#[derive(Debug, Clone, Serialize, Deserialize)]
33pub struct FileAnalysis {
34    /// File path
35    pub path: String,
36    /// Event subscriptions found in this file
37    pub subscriptions: Vec<EventSubscription>,
38    /// Event publications found in this file
39    pub publications: Vec<EventPublication>,
40}
41
42/// System information extracted from code
43#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
44pub struct SystemInfo {
45    /// System struct name
46    pub name: String,
47    /// Module path (e.g., "plugin::combat::system")
48    pub module_path: String,
49    /// Source file path
50    pub file_path: String,
51    /// Event types this system subscribes to
52    pub subscribes: Vec<String>,
53    /// Event types this system publishes
54    pub publishes: Vec<String>,
55    /// Hook traits used by this system
56    pub hooks: Vec<String>,
57    /// State types accessed by this system
58    pub states: Vec<String>,
59}
60
61/// Hook method category based on naming convention
62#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
63pub enum HookCategory {
64    /// Notification hooks: on_* methods (void return)
65    Notification,
66    /// Validation hooks: can_* methods (Result return)
67    Validation,
68    /// Lifecycle hooks: before_*/after_* methods
69    Lifecycle,
70    /// Calculation hooks: calculate_* methods
71    Calculation,
72    /// Generation hooks: generate_* methods
73    Generation,
74    /// Other/Unknown category
75    Other,
76}
77
78/// Hook method information
79#[derive(Debug, Clone, Serialize, Deserialize)]
80pub struct HookMethod {
81    /// Method name
82    pub name: String,
83    /// Method category
84    pub category: HookCategory,
85    /// Parameter types (simplified representation)
86    pub params: Vec<String>,
87    /// Return type (simplified representation)
88    pub return_type: String,
89    /// Whether this method has a default implementation
90    pub has_default_impl: bool,
91}
92
93/// Hook trait information
94#[derive(Debug, Clone, Serialize, Deserialize)]
95pub struct HookInfo {
96    /// Trait name
97    pub trait_name: String,
98    /// Module path
99    pub module_path: String,
100    /// Source file path
101    pub file_path: String,
102    /// Methods defined in this trait
103    pub methods: Vec<HookMethod>,
104}
105
106/// Hook call site information
107#[derive(Debug, Clone, Serialize, Deserialize)]
108pub struct HookCall {
109    /// Hook trait being called
110    pub hook_trait: String,
111    /// Method name being called
112    pub method_name: String,
113    /// Caller (function/method name)
114    pub caller: String,
115    /// Source file path
116    pub file_path: String,
117    /// Line number (placeholder: 0)
118    pub line: usize,
119}
120
121/// Plugin information inferred from directory structure
122#[derive(Debug, Clone, Serialize, Deserialize)]
123pub struct PluginInfo {
124    /// Plugin name (e.g., "combat")
125    pub name: String,
126    /// Plugin directory path
127    pub path: String,
128    /// System implementation (if found)
129    pub system: Option<SystemInfo>,
130    /// Hook trait names defined in this plugin
131    pub hooks: Vec<String>,
132    /// Event types defined in this plugin
133    pub events: Vec<String>,
134    /// Detailed hook information (if analyzed)
135    pub hook_details: Vec<HookInfo>,
136}
137
138/// Complete analysis result for a project
139#[derive(Debug, Clone, Serialize, Deserialize)]
140pub struct AnalysisResult {
141    /// All analyzed files
142    pub files: Vec<FileAnalysis>,
143    /// Detected systems
144    pub systems: Vec<SystemInfo>,
145    /// Detected plugins
146    pub plugins: Vec<PluginInfo>,
147}
148
149impl AnalysisResult {
150    /// Create a new empty analysis result
151    pub fn new() -> Self {
152        Self {
153            files: Vec::new(),
154            systems: Vec::new(),
155            plugins: Vec::new(),
156        }
157    }
158
159    /// Add a file analysis result
160    pub fn add_file(&mut self, analysis: FileAnalysis) {
161        self.files.push(analysis);
162    }
163
164    /// Add a system
165    pub fn add_system(&mut self, system: SystemInfo) {
166        self.systems.push(system);
167    }
168
169    /// Add a plugin
170    pub fn add_plugin(&mut self, plugin: PluginInfo) {
171        self.plugins.push(plugin);
172    }
173
174    /// Get all event subscriptions across all files
175    pub fn all_subscriptions(&self) -> Vec<&EventSubscription> {
176        self.files
177            .iter()
178            .flat_map(|f| f.subscriptions.iter())
179            .collect()
180    }
181
182    /// Get all event publications across all files
183    pub fn all_publications(&self) -> Vec<&EventPublication> {
184        self.files
185            .iter()
186            .flat_map(|f| f.publications.iter())
187            .collect()
188    }
189
190    /// Get all unique event types
191    pub fn event_types(&self) -> Vec<String> {
192        let mut types: Vec<String> = self
193            .all_subscriptions()
194            .iter()
195            .map(|s| s.event_type.clone())
196            .chain(self.all_publications().iter().map(|p| p.event_type.clone()))
197            .collect();
198
199        types.sort();
200        types.dedup();
201        types
202    }
203}
204
205impl Default for AnalysisResult {
206    fn default() -> Self {
207        Self::new()
208    }
209}