Skip to main content

runmat_snapshot/
presets.rs

1//! Snapshot presets for common use cases
2//!
3//! Provides pre-configured snapshot settings optimized for different
4//! deployment scenarios and performance requirements.
5
6use crate::{CompressionAlgorithm, SnapshotConfig};
7use std::time::Duration;
8
9/// Snapshot preset configurations
10#[derive(Debug, Clone)]
11pub enum SnapshotPreset {
12    /// Fast development iteration
13    Development,
14
15    /// Production deployment
16    Production,
17
18    /// High-performance computing
19    HighPerformance,
20
21    /// Memory-constrained environments
22    LowMemory,
23
24    /// Network-optimized (minimal size)
25    NetworkOptimized,
26
27    /// Debug-friendly (maximum validation)
28    Debug,
29
30    /// Custom configuration
31    Custom(SnapshotConfig),
32}
33
34impl SnapshotPreset {
35    /// Get configuration for preset
36    pub fn config(&self) -> SnapshotConfig {
37        match self {
38            SnapshotPreset::Development => Self::development_config(),
39            SnapshotPreset::Production => Self::production_config(),
40            SnapshotPreset::HighPerformance => Self::high_performance_config(),
41            SnapshotPreset::LowMemory => Self::low_memory_config(),
42            SnapshotPreset::NetworkOptimized => Self::network_optimized_config(),
43            SnapshotPreset::Debug => Self::debug_config(),
44            SnapshotPreset::Custom(config) => config.clone(),
45        }
46    }
47
48    /// Development preset - fast build, minimal compression
49    fn development_config() -> SnapshotConfig {
50        SnapshotConfig {
51            compression_enabled: true,
52            compression_algorithm: CompressionAlgorithm::Lz4,
53            compression_level: 1, // Fast compression
54            validation_enabled: true,
55            memory_mapping_enabled: true,
56            parallel_loading: true,
57            progress_reporting: true, // Helpful during development
58            max_optimization_level: crate::OptimizationLevel::MaxPerformance,
59            max_cache_size: 64 * 1024 * 1024, // 64MB
60            cache_eviction_policy: crate::CacheEvictionPolicy::LeastRecentlyUsed,
61        }
62    }
63
64    /// Production preset - balanced performance and size
65    fn production_config() -> SnapshotConfig {
66        SnapshotConfig {
67            compression_enabled: true,
68            compression_algorithm: CompressionAlgorithm::Auto,
69            compression_level: 6, // Balanced compression
70            validation_enabled: true,
71            memory_mapping_enabled: true,
72            parallel_loading: true,
73            progress_reporting: false, // No progress in production
74            max_optimization_level: crate::OptimizationLevel::MaxPerformance,
75            max_cache_size: 128 * 1024 * 1024, // 128MB
76            cache_eviction_policy: crate::CacheEvictionPolicy::Adaptive,
77        }
78    }
79
80    /// High-performance preset - optimized for speed
81    fn high_performance_config() -> SnapshotConfig {
82        SnapshotConfig {
83            compression_enabled: true,
84            compression_algorithm: CompressionAlgorithm::Lz4,
85            compression_level: 1,      // Fastest decompression
86            validation_enabled: false, // Skip validation for speed
87            memory_mapping_enabled: true,
88            parallel_loading: true,
89            progress_reporting: false,
90            max_optimization_level: crate::OptimizationLevel::MaxPerformance,
91            max_cache_size: 256 * 1024 * 1024, // 256MB - more cache
92            cache_eviction_policy: crate::CacheEvictionPolicy::LeastRecentlyUsed,
93        }
94    }
95
96    /// Low-memory preset - minimal memory usage
97    fn low_memory_config() -> SnapshotConfig {
98        SnapshotConfig {
99            compression_enabled: true,
100            compression_algorithm: CompressionAlgorithm::Zstd,
101            compression_level: 9, // Maximum compression
102            validation_enabled: true,
103            memory_mapping_enabled: false, // Avoid memory mapping
104            parallel_loading: false,       // Reduce memory overhead
105            progress_reporting: false,
106            max_optimization_level: crate::OptimizationLevel::MaxPerformance,
107            max_cache_size: 16 * 1024 * 1024, // 16MB - minimal cache
108            cache_eviction_policy: crate::CacheEvictionPolicy::TimeToLive(Duration::from_secs(60)),
109        }
110    }
111
112    /// Network-optimized preset - smallest possible size
113    fn network_optimized_config() -> SnapshotConfig {
114        SnapshotConfig {
115            compression_enabled: true,
116            compression_algorithm: CompressionAlgorithm::Zstd,
117            compression_level: 9, // Maximum compression
118            validation_enabled: true,
119            memory_mapping_enabled: true,
120            parallel_loading: true,
121            progress_reporting: false,
122            max_optimization_level: crate::OptimizationLevel::MaxPerformance,
123            max_cache_size: 32 * 1024 * 1024, // 32MB
124            cache_eviction_policy: crate::CacheEvictionPolicy::LeastFrequentlyUsed,
125        }
126    }
127
128    /// Debug preset - maximum validation and reporting
129    fn debug_config() -> SnapshotConfig {
130        SnapshotConfig {
131            compression_enabled: false, // No compression for easier debugging
132            compression_algorithm: CompressionAlgorithm::None,
133            compression_level: 0,
134            validation_enabled: true,
135            memory_mapping_enabled: false, // Easier to debug without mmap
136            parallel_loading: false,       // Sequential for easier debugging
137            progress_reporting: true,      // Detailed progress
138            max_optimization_level: crate::OptimizationLevel::MaxPerformance,
139            max_cache_size: 128 * 1024 * 1024, // 128MB
140            cache_eviction_policy: crate::CacheEvictionPolicy::LeastRecentlyUsed,
141        }
142    }
143
144    /// Get all available presets
145    pub fn all_presets() -> Vec<SnapshotPreset> {
146        vec![
147            SnapshotPreset::Development,
148            SnapshotPreset::Production,
149            SnapshotPreset::HighPerformance,
150            SnapshotPreset::LowMemory,
151            SnapshotPreset::NetworkOptimized,
152            SnapshotPreset::Debug,
153        ]
154    }
155
156    /// Get preset by name
157    pub fn from_name(name: &str) -> Option<SnapshotPreset> {
158        match name.to_lowercase().as_str() {
159            "development" | "dev" => Some(SnapshotPreset::Development),
160            "production" | "prod" => Some(SnapshotPreset::Production),
161            "high-performance" | "highperf" | "performance" => {
162                Some(SnapshotPreset::HighPerformance)
163            }
164            "low-memory" | "lowmem" | "minimal" => Some(SnapshotPreset::LowMemory),
165            "network-optimized" | "network" | "small" => Some(SnapshotPreset::NetworkOptimized),
166            "debug" => Some(SnapshotPreset::Debug),
167            _ => None,
168        }
169    }
170
171    /// Get preset name
172    pub fn name(&self) -> &'static str {
173        match self {
174            SnapshotPreset::Development => "Development",
175            SnapshotPreset::Production => "Production",
176            SnapshotPreset::HighPerformance => "High-Performance",
177            SnapshotPreset::LowMemory => "Low-Memory",
178            SnapshotPreset::NetworkOptimized => "Network-Optimized",
179            SnapshotPreset::Debug => "Debug",
180            SnapshotPreset::Custom(_) => "Custom",
181        }
182    }
183
184    /// Get preset description
185    pub fn description(&self) -> &'static str {
186        match self {
187            SnapshotPreset::Development => {
188                "Fast build times with minimal compression, ideal for development iteration"
189            }
190            SnapshotPreset::Production => {
191                "Balanced performance and size, recommended for production deployments"
192            }
193            SnapshotPreset::HighPerformance => {
194                "Optimized for fastest loading times, minimal validation overhead"
195            }
196            SnapshotPreset::LowMemory => {
197                "Minimal memory usage with maximum compression for constrained environments"
198            }
199            SnapshotPreset::NetworkOptimized => {
200                "Smallest possible file size for network distribution"
201            }
202            SnapshotPreset::Debug => "Maximum validation and debugging information, no compression",
203            SnapshotPreset::Custom(_) => "Custom configuration with user-specified settings",
204        }
205    }
206
207    /// Get expected characteristics
208    pub fn characteristics(&self) -> PresetCharacteristics {
209        match self {
210            SnapshotPreset::Development => PresetCharacteristics {
211                build_time: BuildTime::Fast,
212                load_time: LoadTime::Fast,
213                file_size: FileSize::Medium,
214                memory_usage: MemoryUsage::Medium,
215                validation_level: ValidationLevel::Standard,
216                debugging_friendly: true,
217            },
218            SnapshotPreset::Production => PresetCharacteristics {
219                build_time: BuildTime::Medium,
220                load_time: LoadTime::Fast,
221                file_size: FileSize::Small,
222                memory_usage: MemoryUsage::Medium,
223                validation_level: ValidationLevel::Standard,
224                debugging_friendly: false,
225            },
226            SnapshotPreset::HighPerformance => PresetCharacteristics {
227                build_time: BuildTime::Fast,
228                load_time: LoadTime::VeryFast,
229                file_size: FileSize::Medium,
230                memory_usage: MemoryUsage::High,
231                validation_level: ValidationLevel::Minimal,
232                debugging_friendly: false,
233            },
234            SnapshotPreset::LowMemory => PresetCharacteristics {
235                build_time: BuildTime::Slow,
236                load_time: LoadTime::Medium,
237                file_size: FileSize::VerySmall,
238                memory_usage: MemoryUsage::VeryLow,
239                validation_level: ValidationLevel::Standard,
240                debugging_friendly: false,
241            },
242            SnapshotPreset::NetworkOptimized => PresetCharacteristics {
243                build_time: BuildTime::Slow,
244                load_time: LoadTime::Medium,
245                file_size: FileSize::VerySmall,
246                memory_usage: MemoryUsage::Low,
247                validation_level: ValidationLevel::Standard,
248                debugging_friendly: false,
249            },
250            SnapshotPreset::Debug => PresetCharacteristics {
251                build_time: BuildTime::Medium,
252                load_time: LoadTime::Medium,
253                file_size: FileSize::Large,
254                memory_usage: MemoryUsage::Medium,
255                validation_level: ValidationLevel::Comprehensive,
256                debugging_friendly: true,
257            },
258            SnapshotPreset::Custom(_) => PresetCharacteristics {
259                build_time: BuildTime::Medium,
260                load_time: LoadTime::Medium,
261                file_size: FileSize::Medium,
262                memory_usage: MemoryUsage::Medium,
263                validation_level: ValidationLevel::Standard,
264                debugging_friendly: false,
265            },
266        }
267    }
268}
269
270/// Preset characteristics for comparison
271#[derive(Debug, Clone)]
272pub struct PresetCharacteristics {
273    pub build_time: BuildTime,
274    pub load_time: LoadTime,
275    pub file_size: FileSize,
276    pub memory_usage: MemoryUsage,
277    pub validation_level: ValidationLevel,
278    pub debugging_friendly: bool,
279}
280
281/// Build time characteristics
282#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
283pub enum BuildTime {
284    VeryFast,
285    Fast,
286    Medium,
287    Slow,
288    VerySlow,
289}
290
291/// Load time characteristics
292#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
293pub enum LoadTime {
294    VeryFast,
295    Fast,
296    Medium,
297    Slow,
298    VerySlow,
299}
300
301/// File size characteristics
302#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
303pub enum FileSize {
304    VerySmall,
305    Small,
306    Medium,
307    Large,
308    VeryLarge,
309}
310
311/// Memory usage characteristics
312#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
313pub enum MemoryUsage {
314    VeryLow,
315    Low,
316    Medium,
317    High,
318    VeryHigh,
319}
320
321/// Validation level
322#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
323pub enum ValidationLevel {
324    None,
325    Minimal,
326    Standard,
327    Comprehensive,
328    Exhaustive,
329}
330
331impl std::fmt::Display for BuildTime {
332    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
333        match self {
334            BuildTime::VeryFast => write!(f, "Very Fast"),
335            BuildTime::Fast => write!(f, "Fast"),
336            BuildTime::Medium => write!(f, "Medium"),
337            BuildTime::Slow => write!(f, "Slow"),
338            BuildTime::VerySlow => write!(f, "Very Slow"),
339        }
340    }
341}
342
343impl std::fmt::Display for LoadTime {
344    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
345        match self {
346            LoadTime::VeryFast => write!(f, "Very Fast"),
347            LoadTime::Fast => write!(f, "Fast"),
348            LoadTime::Medium => write!(f, "Medium"),
349            LoadTime::Slow => write!(f, "Slow"),
350            LoadTime::VerySlow => write!(f, "Very Slow"),
351        }
352    }
353}
354
355impl std::fmt::Display for FileSize {
356    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
357        match self {
358            FileSize::VerySmall => write!(f, "Very Small"),
359            FileSize::Small => write!(f, "Small"),
360            FileSize::Medium => write!(f, "Medium"),
361            FileSize::Large => write!(f, "Large"),
362            FileSize::VeryLarge => write!(f, "Very Large"),
363        }
364    }
365}
366
367impl std::fmt::Display for MemoryUsage {
368    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
369        match self {
370            MemoryUsage::VeryLow => write!(f, "Very Low"),
371            MemoryUsage::Low => write!(f, "Low"),
372            MemoryUsage::Medium => write!(f, "Medium"),
373            MemoryUsage::High => write!(f, "High"),
374            MemoryUsage::VeryHigh => write!(f, "Very High"),
375        }
376    }
377}
378
379impl std::fmt::Display for ValidationLevel {
380    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
381        match self {
382            ValidationLevel::None => write!(f, "None"),
383            ValidationLevel::Minimal => write!(f, "Minimal"),
384            ValidationLevel::Standard => write!(f, "Standard"),
385            ValidationLevel::Comprehensive => write!(f, "Comprehensive"),
386            ValidationLevel::Exhaustive => write!(f, "Exhaustive"),
387        }
388    }
389}
390
391#[cfg(test)]
392mod tests {
393    use super::*;
394
395    #[test]
396    fn test_preset_from_name() {
397        assert!(matches!(
398            SnapshotPreset::from_name("development"),
399            Some(SnapshotPreset::Development)
400        ));
401        assert!(matches!(
402            SnapshotPreset::from_name("prod"),
403            Some(SnapshotPreset::Production)
404        ));
405        assert!(matches!(
406            SnapshotPreset::from_name("highperf"),
407            Some(SnapshotPreset::HighPerformance)
408        ));
409        assert!(SnapshotPreset::from_name("invalid").is_none());
410    }
411
412    #[test]
413    fn test_preset_names() {
414        assert_eq!(SnapshotPreset::Development.name(), "Development");
415        assert_eq!(SnapshotPreset::Production.name(), "Production");
416        assert_eq!(SnapshotPreset::Debug.name(), "Debug");
417    }
418
419    #[test]
420    fn test_preset_configs() {
421        let dev_config = SnapshotPreset::Development.config();
422        assert!(dev_config.progress_reporting);
423        assert_eq!(dev_config.compression_level, 1);
424
425        let prod_config = SnapshotPreset::Production.config();
426        assert!(!prod_config.progress_reporting);
427        assert_eq!(prod_config.compression_level, 6);
428
429        let debug_config = SnapshotPreset::Debug.config();
430        assert!(!debug_config.compression_enabled);
431        assert!(debug_config.validation_enabled);
432    }
433
434    #[test]
435    fn test_preset_characteristics() {
436        let high_perf = SnapshotPreset::HighPerformance;
437        let chars = high_perf.characteristics();
438
439        assert_eq!(chars.load_time, LoadTime::VeryFast);
440        assert_eq!(chars.memory_usage, MemoryUsage::High);
441        assert_eq!(chars.validation_level, ValidationLevel::Minimal);
442
443        let low_mem = SnapshotPreset::LowMemory;
444        let chars = low_mem.characteristics();
445
446        assert_eq!(chars.memory_usage, MemoryUsage::VeryLow);
447        assert_eq!(chars.file_size, FileSize::VerySmall);
448    }
449
450    #[test]
451    fn test_all_presets() {
452        let presets = SnapshotPreset::all_presets();
453        assert_eq!(presets.len(), 6);
454
455        // Ensure all presets have unique names
456        let names: std::collections::HashSet<_> = presets.iter().map(|p| p.name()).collect();
457        assert_eq!(names.len(), 6);
458    }
459
460    #[test]
461    fn test_characteristic_ordering() {
462        assert!(BuildTime::VeryFast < BuildTime::Fast);
463        assert!(LoadTime::Fast < LoadTime::Medium);
464        assert!(FileSize::Small < FileSize::Large);
465        assert!(MemoryUsage::Low < MemoryUsage::High);
466        assert!(ValidationLevel::Minimal < ValidationLevel::Standard);
467    }
468}