Skip to main content

memlink_runtime/
profile.rs

1//! Module profile - configuration and metadata for loaded modules.
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct ModuleProfile {
7    pub name: String,
8    pub baseline_memory_mb: u64,
9    pub max_memory_mb: u64,
10    #[serde(default)]
11    pub version: Option<String>,
12    #[serde(default)]
13    pub description: Option<String>,
14}
15
16impl Default for ModuleProfile {
17    fn default() -> Self {
18        ModuleProfile {
19            name: "unknown".to_string(),
20            baseline_memory_mb: 0,
21            max_memory_mb: 64,
22            version: None,
23            description: None,
24        }
25    }
26}
27
28impl ModuleProfile {
29    pub fn new(name: impl Into<String>) -> Self {
30        ModuleProfile {
31            name: name.into(),
32            ..Default::default()
33        }
34    }
35
36    pub fn with_memory_limits(mut self, baseline_mb: u64, max_mb: u64) -> Self {
37        self.baseline_memory_mb = baseline_mb;
38        self.max_memory_mb = max_mb;
39        self
40    }
41
42    pub fn with_version(mut self, version: impl Into<String>) -> Self {
43        self.version = Some(version.into());
44        self
45    }
46
47    pub fn with_description(mut self, description: impl Into<String>) -> Self {
48        self.description = Some(description.into());
49        self
50    }
51
52    pub fn memory_usage_ratio(&self, current_mb: u64) -> f32 {
53        if self.max_memory_mb == 0 {
54            0.0
55        } else {
56            current_mb as f32 / self.max_memory_mb as f32
57        }
58    }
59
60    pub fn is_over_memory_limit(&self, current_mb: u64) -> bool {
61        current_mb > self.max_memory_mb
62    }
63}