use serde::{Deserialize, Serialize};
use std::collections::HashMap;
pub use super::allocation::{ImpactLevel, RiskDistribution};
#[derive(Debug, Clone, Serialize)]
pub struct ScopeInfo {
pub name: String,
pub parent: Option<String>,
pub children: Vec<String>,
pub depth: usize,
pub variables: Vec<String>,
pub total_memory: usize,
pub peak_memory: usize,
pub allocation_count: usize,
pub lifetime_start: Option<u64>,
pub lifetime_end: Option<u64>,
pub is_active: bool,
pub start_time: u64,
pub end_time: Option<u64>,
pub memory_usage: usize,
pub child_scopes: Vec<String>,
pub parent_scope: Option<String>,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct ScopeHierarchy {
pub root_scopes: Vec<String>,
pub scope_tree: HashMap<String, ScopeInfo>,
pub max_depth: usize,
pub total_scopes: usize,
pub relationships: HashMap<String, Vec<String>>,
pub depth_map: HashMap<String, usize>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TypeLifecyclePattern {
pub type_name: String,
pub average_lifetime_ms: f64,
pub typical_size: usize,
pub growth_pattern: String,
pub risk_level: String,
pub instance_count: usize,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum GrowthReason {
Initial,
Expansion,
Reallocation,
Optimization,
UserRequested,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum AllocationEventType {
Allocate,
Deallocate,
Reallocate,
Move,
Borrow,
Return,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ScopeEventType {
Enter,
Exit,
Create,
Destroy,
}
#[derive(Debug, Clone, Serialize)]
pub struct GrowthEvent {
pub timestamp: u64,
pub old_size: usize,
pub new_size: usize,
pub growth_factor: f64,
pub reason: GrowthReason,
pub var_name: String,
}
#[derive(Debug, Clone, Serialize)]
pub struct BorrowEvent {
pub timestamp: u64,
pub ptr: usize,
pub borrow_type: String,
pub duration_ms: u64,
pub var_name: String,
}
#[derive(Debug, Clone, Serialize)]
pub struct MoveEvent {
pub timestamp: u64,
pub from_ptr: usize,
pub to_ptr: usize,
pub size: usize,
pub var_name: String,
}
#[derive(Debug, Clone, Serialize)]
pub struct VariableRelationship {
pub source_var: String,
pub target_var: String,
pub relationship_type: String,
pub strength: f64,
}
#[derive(Debug, Clone, Serialize)]
pub struct PotentialLeak {
pub ptr: usize,
pub size: usize,
pub age_ms: u64,
pub var_name: Option<String>,
pub type_name: Option<String>,
pub severity: String,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct ScopeAnalysis {
pub total_scopes: usize,
pub active_scopes: usize,
pub max_depth: usize,
pub average_lifetime: f64,
pub memory_efficiency: f64,
pub scopes: Vec<ScopeInfo>,
pub scope_hierarchy: ScopeHierarchy,
pub cross_scope_references: Vec<String>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ScopeLifecycleMetrics {
pub scope_name: String,
pub variable_count: usize,
pub average_lifetime_ms: f64,
pub total_memory_usage: usize,
pub peak_memory_usage: usize,
pub allocation_frequency: f64,
pub deallocation_efficiency: f64,
pub completed_allocations: usize,
pub memory_growth_events: usize,
pub peak_concurrent_variables: usize,
pub memory_efficiency_ratio: f64,
pub ownership_transfer_events: usize,
pub fragmentation_score: f64,
pub instant_allocations: usize,
pub short_term_allocations: usize,
pub medium_term_allocations: usize,
pub long_term_allocations: usize,
pub suspected_leaks: usize,
pub risk_distribution: RiskDistribution,
pub scope_metrics: Vec<ScopeLifecycleMetrics>,
pub type_lifecycle_patterns: Vec<TypeLifecyclePattern>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_risk_distribution_default() {
let risk = RiskDistribution::default();
assert_eq!(risk.low_risk, 0);
assert_eq!(risk.medium_risk, 0);
assert_eq!(risk.high_risk, 0);
assert_eq!(risk.critical_risk, 0);
}
#[test]
fn test_allocation_event_type_variants() {
let events = vec![
AllocationEventType::Allocate,
AllocationEventType::Deallocate,
AllocationEventType::Reallocate,
AllocationEventType::Move,
AllocationEventType::Borrow,
AllocationEventType::Return,
];
for event in events {
assert!(!format!("{event:?}").is_empty());
}
}
#[test]
fn test_scope_event_type_variants() {
let events = vec![
ScopeEventType::Enter,
ScopeEventType::Exit,
ScopeEventType::Create,
ScopeEventType::Destroy,
];
for event in events {
assert!(!format!("{event:?}").is_empty());
}
}
#[test]
fn test_growth_reason_variants() {
let reasons = vec![
GrowthReason::Initial,
GrowthReason::Expansion,
GrowthReason::Reallocation,
GrowthReason::Optimization,
GrowthReason::UserRequested,
];
for reason in reasons {
assert!(!format!("{reason:?}").is_empty());
}
}
}