Skip to main content

memscope_rs/analysis_engine/
analyzer.rs

1//! Analyzer trait for pluggable analyzers
2//!
3//! This module defines the Analyzer trait that all analyzers must implement.
4
5use crate::snapshot::MemorySnapshot;
6use serde::{Deserialize, Serialize};
7use std::fmt;
8
9/// Analysis result
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct AnalysisResult {
12    /// Name of the analyzer
13    pub analyzer_name: String,
14    /// Number of issues found
15    pub issue_count: usize,
16    /// Severity level
17    pub severity: Severity,
18    /// Description of the result
19    pub description: String,
20    /// Detailed findings
21    pub findings: Vec<Finding>,
22}
23
24/// Individual finding from an analysis
25#[derive(Debug, Clone, Serialize, Deserialize)]
26pub struct Finding {
27    /// Type of issue
28    pub issue_type: String,
29    /// Description of the issue
30    pub description: String,
31    /// Memory pointer address (if applicable)
32    pub ptr: Option<usize>,
33    /// Size in bytes (if applicable)
34    pub size: Option<usize>,
35    /// Additional context
36    pub context: String,
37}
38
39/// Severity level of an issue
40#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
41pub enum Severity {
42    /// Informational only
43    Info,
44    /// Warning
45    Warning,
46    /// Error
47    Error,
48    /// Critical
49    Critical,
50}
51
52impl fmt::Display for Severity {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        match self {
55            Severity::Info => write!(f, "Info"),
56            Severity::Warning => write!(f, "Warning"),
57            Severity::Error => write!(f, "Error"),
58            Severity::Critical => write!(f, "Critical"),
59        }
60    }
61}
62
63/// Analyzer trait for pluggable analysis modules
64///
65/// All analyzers must implement this trait to be used with the AnalysisEngine.
66pub trait Analyzer: Send + Sync {
67    /// Get the name of this analyzer
68    fn name(&self) -> &str;
69
70    /// Analyze a memory snapshot and return results
71    ///
72    /// # Arguments
73    /// * `snapshot` - The memory snapshot to analyze
74    fn analyze(&self, snapshot: &MemorySnapshot) -> AnalysisResult;
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80
81    struct DummyAnalyzer;
82
83    impl Analyzer for DummyAnalyzer {
84        fn name(&self) -> &str {
85            "dummy"
86        }
87
88        fn analyze(&self, snapshot: &MemorySnapshot) -> AnalysisResult {
89            AnalysisResult {
90                analyzer_name: "dummy".to_string(),
91                issue_count: snapshot.active_count(),
92                severity: Severity::Info,
93                description: "Dummy analysis".to_string(),
94                findings: vec![],
95            }
96        }
97    }
98
99    #[test]
100    fn test_analyzer_trait() {
101        let analyzer = DummyAnalyzer;
102        assert_eq!(analyzer.name(), "dummy");
103
104        let snapshot = MemorySnapshot::new();
105        let result = analyzer.analyze(&snapshot);
106        assert_eq!(result.analyzer_name, "dummy");
107    }
108}