Skip to content

Commit c916bcf

Browse files
committed
Formatting fix
Formatting fix
1 parent bbf73a9 commit c916bcf

5 files changed

Lines changed: 393 additions & 324 deletions

File tree

rustfmt.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
max_width = 140
2+
#use_small_heuristics = "Max"

src/config.rs

Lines changed: 71 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
use crate::{ErrorMode, MemoryReserve};
2+
use serde::{Deserialize, Serialize};
3+
use std::collections::HashMap;
14
use std::fs;
25
use std::path::Path;
3-
use std::collections::HashMap;
4-
use serde::{Deserialize, Serialize};
5-
use crate::{MemoryReserve, ErrorMode};
66

77
// Application constants
88
pub const APP_NAME: &str = "Test Memory R";
@@ -105,28 +105,27 @@ pub struct LegacyTest {
105105

106106
impl ModernConfig {
107107
pub fn load_from_file(path: &str) -> Result<Self, String> {
108-
let content = fs::read_to_string(path)
109-
.map_err(|e| format!("Failed to read config file: {}", e))?;
110-
111-
let config: ModernConfig = serde_json::from_str(&content)
112-
.map_err(|e| format!("Failed to parse JSON config: {}", e))?;
113-
108+
let content = fs::read_to_string(path).map_err(|e| format!("Failed to read config file: {}", e))?;
109+
110+
let config: ModernConfig = serde_json::from_str(&content).map_err(|e| format!("Failed to parse JSON config: {}", e))?;
111+
114112
// Validate config version compatibility
115113
match config.config_format_version.as_str() {
116114
"2.0" => Ok(config),
117115
"1.0" => Err("Config format version 1.0 detected - please use legacy .cfg format or upgrade to v2.0".to_string()),
118-
version => Err(format!("Unsupported config format version '{}' - TMR {} supports v2.0", version, APP_VERSION)),
116+
version => Err(format!(
117+
"Unsupported config format version '{}' - TMR {} supports v2.0",
118+
version, APP_VERSION
119+
)),
119120
}
120121
}
121-
122+
122123
pub fn save_to_file(&self, path: &str) -> Result<(), String> {
123-
let json = serde_json::to_string_pretty(self)
124-
.map_err(|e| format!("Failed to serialize config: {}", e))?;
125-
126-
fs::write(path, json)
127-
.map_err(|e| format!("Failed to write config file: {}", e))
124+
let json = serde_json::to_string_pretty(self).map_err(|e| format!("Failed to serialize config: {}", e))?;
125+
126+
fs::write(path, json).map_err(|e| format!("Failed to write config file: {}", e))
128127
}
129-
128+
130129
// Convert to runtime configuration
131130
pub fn to_memory_reserve(&self) -> MemoryReserve {
132131
match self.system.memory_reserve.reserve_type.as_str() {
@@ -136,15 +135,15 @@ impl ModernConfig {
136135
_ => MemoryReserve::PercentFree(10.0), // default
137136
}
138137
}
139-
138+
140139
pub fn to_error_mode(&self) -> ErrorMode {
141140
match self.system.error_mode.as_str() {
142141
"halt" | "stop" => ErrorMode::Halt,
143142
"panic" | "debug" => ErrorMode::Panic,
144143
_ => ErrorMode::Log, // default
145144
}
146145
}
147-
146+
148147
pub fn create_demo_config() -> Self {
149148
ModernConfig {
150149
config_format_version: CONFIG_VERSION.to_string(),
@@ -258,44 +257,41 @@ impl ModernConfig {
258257

259258
impl LegacyConfig {
260259
pub fn load_from_file(path: &str) -> Result<Self, String> {
261-
let content = fs::read_to_string(path)
262-
.map_err(|e| format!("Failed to read legacy config file: {}", e))?;
263-
260+
let content = fs::read_to_string(path).map_err(|e| format!("Failed to read legacy config file: {}", e))?;
261+
264262
Self::parse_legacy_format(&content)
265263
}
266-
264+
267265
fn parse_legacy_format(content: &str) -> Result<Self, String> {
268266
let mut sections: HashMap<String, HashMap<String, String>> = HashMap::new();
269267
let mut current_section = String::new();
270-
268+
271269
for line in content.lines() {
272270
let line = line.trim();
273271
if line.is_empty() || line.starts_with('#') {
274272
continue;
275273
}
276-
274+
277275
if line.starts_with('[') && line.ends_with(']') {
278-
current_section = line[1..line.len()-1].to_string();
276+
current_section = line[1..line.len() - 1].to_string();
279277
sections.insert(current_section.clone(), HashMap::new());
280278
} else if let Some(eq_pos) = line.find('=') {
281279
let key = line[..eq_pos].trim().to_string();
282-
let value = line[eq_pos+1..].trim().to_string();
280+
let value = line[eq_pos + 1..].trim().to_string();
283281
if let Some(section) = sections.get_mut(&current_section) {
284282
section.insert(key, value);
285283
}
286284
}
287285
}
288-
286+
289287
// Parse main section
290-
let main = sections.get("Main Section")
291-
.ok_or("Missing [Main Section]")?;
292-
293-
let test_sequence = main.get("Test Sequence")
294-
.map(|s| s.split(',')
295-
.filter_map(|n| n.trim().parse::<u32>().ok())
296-
.collect())
288+
let main = sections.get("Main Section").ok_or("Missing [Main Section]")?;
289+
290+
let test_sequence = main
291+
.get("Test Sequence")
292+
.map(|s| s.split(',').filter_map(|n| n.trim().parse::<u32>().ok()).collect())
297293
.unwrap_or_default();
298-
294+
299295
let main_section = LegacyMainSection {
300296
config_name: main.get("Config Name").unwrap_or(&"Unknown".to_string()).clone(),
301297
config_author: main.get("Config Author").unwrap_or(&"Unknown".to_string()).clone(),
@@ -305,21 +301,22 @@ impl LegacyConfig {
305301
cycles: main.get("Cycles").and_then(|s| s.parse().ok()).unwrap_or(1),
306302
test_sequence,
307303
};
308-
304+
309305
// Parse memory setup
310-
let memory = sections.get("Global Memory Setup")
311-
.ok_or("Missing [Global Memory Setup]")?;
312-
306+
let memory = sections.get("Global Memory Setup").ok_or("Missing [Global Memory Setup]")?;
307+
313308
let memory_setup = LegacyMemorySetup {
314-
testing_window_size_mb: memory.get("Testing Window Size (Mb)")
315-
.and_then(|s| s.parse().ok()).unwrap_or(880),
316-
reserved_memory_mb: memory.get("Reserved Memory for Windows (Mb)")
317-
.and_then(|s| s.parse().ok()).unwrap_or(128),
309+
testing_window_size_mb: memory.get("Testing Window Size (Mb)").and_then(|s| s.parse().ok()).unwrap_or(880),
310+
reserved_memory_mb: memory
311+
.get("Reserved Memory for Windows (Mb)")
312+
.and_then(|s| s.parse().ok())
313+
.unwrap_or(128),
318314
};
319-
315+
320316
// Parse tests
321317
let mut tests = Vec::new();
322-
for i in 0..=15 { // Legacy configs typically have Test0-Test15
318+
for i in 0..=15 {
319+
// Legacy configs typically have Test0-Test15
323320
let test_section = format!("Test{}", i);
324321
if let Some(test) = sections.get(&test_section) {
325322
let legacy_test = LegacyTest {
@@ -328,10 +325,12 @@ impl LegacyConfig {
328325
time_percent: test.get("Time (%)").and_then(|s| s.parse().ok()).unwrap_or(100),
329326
function: test.get("Function").unwrap_or(&"Unknown".to_string()).clone(),
330327
pattern_mode: test.get("Pattern Mode").and_then(|s| s.parse().ok()).unwrap_or(0),
331-
pattern_param0: test.get("Pattern Param0")
328+
pattern_param0: test
329+
.get("Pattern Param0")
332330
.and_then(|s| u64::from_str_radix(s.trim_start_matches("0x"), 16).ok())
333331
.unwrap_or(0),
334-
pattern_param1: test.get("Pattern Param1")
332+
pattern_param1: test
333+
.get("Pattern Param1")
335334
.and_then(|s| u64::from_str_radix(s.trim_start_matches("0x"), 16).ok())
336335
.unwrap_or(0),
337336
parameter: test.get("Parameter").and_then(|s| s.parse().ok()).unwrap_or(0),
@@ -340,20 +339,21 @@ impl LegacyConfig {
340339
tests.push(legacy_test);
341340
}
342341
}
343-
342+
344343
Ok(LegacyConfig {
345344
main_section,
346345
memory_setup,
347346
tests,
348347
})
349348
}
350-
349+
351350
// Convert legacy config to modern config v2.0
352351
pub fn to_modern_config(&self) -> ModernConfig {
353352
let memory_reserve = if self.memory_setup.testing_window_size_mb > 0 {
354353
// Convert testing window size to percentage (rough approximation)
355-
let estimated_percent = (self.memory_setup.reserved_memory_mb as f64 /
356-
(self.memory_setup.testing_window_size_mb + self.memory_setup.reserved_memory_mb) as f64) * 100.0;
354+
let estimated_percent = (self.memory_setup.reserved_memory_mb as f64
355+
/ (self.memory_setup.testing_window_size_mb + self.memory_setup.reserved_memory_mb) as f64)
356+
* 100.0;
357357
MemoryReserveConfig {
358358
reserve_type: "percent".to_string(),
359359
value: estimated_percent.max(5.0).min(50.0), // Clamp to reasonable range
@@ -364,21 +364,27 @@ impl LegacyConfig {
364364
value: 10.0,
365365
}
366366
};
367-
368-
let test_sequence = self.tests.iter()
367+
368+
let test_sequence = self
369+
.tests
370+
.iter()
369371
.filter(|t| t.enabled)
370372
.map(|test| TestConfig {
371373
enabled: true,
372374
function: Self::map_legacy_function(&test.function),
373375
time_percent: test.time_percent,
374-
block_size_mb: if test.test_block_size_mb > 0 { Some(test.test_block_size_mb) } else { None },
376+
block_size_mb: if test.test_block_size_mb > 0 {
377+
Some(test.test_block_size_mb)
378+
} else {
379+
None
380+
},
375381
pattern_mode: Some(test.pattern_mode),
376382
pattern_param0: Some(test.pattern_param0),
377383
pattern_param1: Some(test.pattern_param1),
378384
parameter: Some(test.parameter),
379385
})
380386
.collect();
381-
387+
382388
ModernConfig {
383389
config_format_version: CONFIG_VERSION.to_string(),
384390
application_name: format!("{} ({})", APP_NAME, APP_SHORT_NAME),
@@ -403,7 +409,7 @@ impl LegacyConfig {
403409
test_sequence,
404410
}
405411
}
406-
412+
407413
// Map legacy function names to modern equivalents
408414
fn map_legacy_function(legacy_name: &str) -> String {
409415
match legacy_name {
@@ -426,7 +432,7 @@ pub fn load_config(path: &str) -> Result<ModernConfig, String> {
426432
if !Path::new(path).exists() {
427433
return Err(format!("Config file does not exist: {}", path));
428434
}
429-
435+
430436
// Try to detect format by file extension or content
431437
if path.ends_with(".json") {
432438
ModernConfig::load_from_file(path)
@@ -436,11 +442,10 @@ pub fn load_config(path: &str) -> Result<ModernConfig, String> {
436442
Ok(legacy.to_modern_config())
437443
} else {
438444
// Try JSON first, then legacy
439-
ModernConfig::load_from_file(path)
440-
.or_else(|_| {
441-
let legacy = LegacyConfig::load_from_file(path)?;
442-
Ok(legacy.to_modern_config())
443-
})
445+
ModernConfig::load_from_file(path).or_else(|_| {
446+
let legacy = LegacyConfig::load_from_file(path)?;
447+
Ok(legacy.to_modern_config())
448+
})
444449
}
445450
}
446451

@@ -449,10 +454,10 @@ pub fn create_demo_configs() -> Result<(), String> {
449454
// Create modern demo config
450455
let modern_config = ModernConfig::create_demo_config();
451456
modern_config.save_to_file("demo_modern_v2.json")?;
452-
457+
453458
println!("✅ Created demo_modern_v2.json - Modern configuration format v2.0");
454459
println!(" Features: JSON format, version tracking, full parameter control, SIMD tests");
455460
println!(" Compatible with: {} v{}", APP_NAME, APP_VERSION);
456-
461+
457462
Ok(())
458-
}
463+
}

0 commit comments

Comments
 (0)