monsoon-frontend 0.2.2

Native GUI frontend for the Monsoon NES emulator, built with egui
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
//! Persistent configuration and file storage utilities.
//!
//! This module provides:
//! - Directory management using the storage abstraction for cross-platform
//!   paths
//! - Generic async file read/write operations to avoid blocking the UI thread
//! - TOML-based configuration persistence for application settings
//! - Helper functions for saving files to data and cache directories

use std::collections::HashSet;
use std::ffi::OsStr;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::sync::OnceLock;
use std::{fs, thread};

use crossbeam_channel::{Receiver, bounded};
use directories::ProjectDirs;
use monsoon_core::emulation::palette_util::RgbPalette;
use monsoon_core::emulation::ppu_util::EmulatorFetchable;
use monsoon_core::emulation::screen_renderer::{NoneRenderer, ScreenRenderer, create_renderer};
use serde::{Deserialize, Serialize};

use crate::frontend::egui::config::{
    AppConfig, AppSpeed, ConsoleConfig, DebugSpeed, KeybindingsConfig, SpeedConfig, UserConfig,
    ViewConfig,
};
use crate::frontend::egui_frontend::get_all_renderers;
use crate::frontend::storage;
use crate::frontend::storage::{Storage, StorageKey};

/// Application identifier used for directory paths
const APP_QUALIFIER: &str = "com";
const APP_ORGANIZATION: &str = "Lightsong";
const APP_NAME: &str = "MonsoonEmulator";

/// Singleton for project directories.
///
/// This uses a `OnceLock` to lazily initialize the project directories on first
/// access. The singleton pattern is used here because:
/// 1. Directory paths are determined by the OS and don't change during runtime
/// 2. Creating `ProjectDirs` multiple times is wasteful
/// 3. All parts of the application need consistent paths for config/data/cache
///
/// Note: The directories crate returns `Option<ProjectDirs>` because on some
/// platforms (like Linux without a proper home directory) it may fail to
/// determine paths.
static PROJECT_DIRS: OnceLock<Option<ProjectDirs>> = OnceLock::new();

/// Get the project directories (lazily initialized)
fn get_project_dirs() -> Option<&'static ProjectDirs> {
    PROJECT_DIRS
        .get_or_init(|| ProjectDirs::from(APP_QUALIFIER, APP_ORGANIZATION, APP_NAME))
        .as_ref()
}

/// Get the config directory path, creating it if necessary
pub fn get_config_dir() -> Option<PathBuf> {
    let dirs = get_project_dirs()?;
    let config_dir = dirs.config_dir();
    if !config_dir.exists() {
        fs::create_dir_all(config_dir).ok()?;
    }
    Some(config_dir.to_path_buf())
}

/// Get the data directory path, creating it if necessary
pub fn get_data_dir() -> Option<PathBuf> {
    let dirs = get_project_dirs()?;
    let data_dir = dirs.data_dir();
    if !data_dir.exists() {
        fs::create_dir_all(data_dir).ok()?;
    }
    Some(data_dir.to_path_buf())
}

/// Get the cache directory path, creating it if necessary
pub fn get_cache_dir() -> Option<PathBuf> {
    let dirs = get_project_dirs()?;
    let cache_dir = dirs.cache_dir();
    if !cache_dir.exists() {
        fs::create_dir_all(cache_dir).ok()?;
    }
    Some(cache_dir.to_path_buf())
}

/// Get the path to a file in the config directory
pub fn get_config_file_path(filename: &str) -> Option<PathBuf> {
    get_config_dir().map(|dir| dir.join(filename))
}

/// Get the path to a file in the data directory
pub fn get_data_file_path(filename: &str) -> Option<PathBuf> {
    get_data_dir().map(|dir| dir.join(filename))
}

/// Get the path to a file in the cache directory
pub fn get_cache_file_path(filename: &str) -> Option<PathBuf> {
    get_cache_dir().map(|dir| dir.join(filename))
}

// ============================================================================
// Async File Operations
// ============================================================================

/// Result type for async file operations
pub enum AsyncFileResult {
    /// File read successfully with contents
    ReadSuccess(Vec<u8>),
    /// File written successfully
    WriteSuccess,
    /// Operation failed with error message
    Error(String),
}

/// Read a file asynchronously, returning the result through a channel.
///
/// This spawns a new thread for each operation to avoid blocking the UI thread
/// when reading potentially large files. The result is returned through a
/// crossbeam channel.
///
/// Note: For frequent small file operations, consider batching requests or
/// using the synchronous `read_file_sync` if blocking is acceptable. Thread
/// spawning has overhead that may not be justified for very small files.
///
/// # Usage
/// ```ignore
/// let rx = read_file_async(path);
/// // Later, in the UI update loop:
/// if let Ok(result) = rx.try_recv() {
///     match result {
///         AsyncFileResult::ReadSuccess(data) => { /* use data */ }
///         AsyncFileResult::Error(e) => { /* handle error */ }
///         _ => {}
///     }
/// }
/// ```
pub fn read_file_async(path: PathBuf) -> Receiver<AsyncFileResult> {
    let (tx, rx) = bounded(1);
    thread::spawn(move || {
        let result = read_file_sync(&path);
        let _ = tx.send(result);
    });
    rx
}

/// Write data to a file asynchronously, returning the result through a channel.
///
/// This spawns a new thread for each operation to avoid blocking the UI thread
/// when writing files. The result is returned through a crossbeam channel.
///
/// Note: For operations that must complete before proceeding (like saving
/// config on exit), use the synchronous `save_config` function instead.
pub fn write_file_async(
    path: PathBuf,
    data: Vec<u8>,
    overwrite: bool,
) -> Receiver<AsyncFileResult> {
    let (tx, rx) = bounded(1);
    thread::spawn(move || {
        let result = write_file_sync(&path, &data, overwrite);
        let _ = tx.send(result);
    });
    rx
}

/// Read a file synchronously (internal helper)
fn read_file_sync(path: &Path) -> AsyncFileResult {
    match fs::File::open(path) {
        Ok(mut file) => {
            let mut contents = Vec::new();
            match file.read_to_end(&mut contents) {
                Ok(_) => AsyncFileResult::ReadSuccess(contents),
                Err(e) => AsyncFileResult::Error(format!("Failed to read file: {}", e)),
            }
        }
        Err(e) => AsyncFileResult::Error(format!("Failed to open file: {}", e)),
    }
}

/// Write data to a file synchronously (internal helper)
fn write_file_sync(path: &Path, data: &[u8], overwrite: bool) -> AsyncFileResult {
    // Ensure parent directory exists
    if let Some(parent) = path.parent()
        && !parent.exists()
        && let Err(e) = fs::create_dir_all(parent)
    {
        return AsyncFileResult::Error(format!("Failed to create directory: {}", e));
    }

    if path.exists() && !overwrite {
        let copy = path
            .file_stem()
            .map(extract)
            .map(|s| s.parse::<u8>().unwrap_or(0))
            .unwrap_or(0);

        let offset = if copy == 0 { 0 } else { 2 };

        let path = append_to_filename(path, format!("_{}", copy + 1).as_str(), offset);
        write_file_sync(&path, data, overwrite)
    } else {
        match fs::File::create(path) {
            Ok(mut file) => match file.write_all(data) {
                Ok(_) => AsyncFileResult::WriteSuccess,
                Err(e) => AsyncFileResult::Error(format!("Failed to write file: {}", e)),
            },
            Err(e) => AsyncFileResult::Error(format!("Failed to create file: {}", e)),
        }
    }
}

fn extract(f: &OsStr) -> String {
    f.to_string_lossy()
        .split('_')
        .next_back()
        .unwrap_or("0")
        .to_string()
}

/// Append a suffix to a filename before the extension
fn append_to_filename(path: &Path, suffix: &str, strip_chars: usize) -> PathBuf {
    let stem = path.file_stem().unwrap_or_default().to_string_lossy();
    let ext = path.extension().map(|e| e.to_string_lossy().to_string());

    // Strip the specified number of characters from the end of the stem
    let trimmed_stem = if strip_chars > 0 && stem.len() >= strip_chars {
        &stem[..stem.len() - strip_chars]
    } else {
        &stem
    };

    let new_filename = match ext {
        Some(e) => format!("{}{}.{}", trimmed_stem, suffix, e),
        None => format!("{}{}", trimmed_stem, suffix),
    };

    path.with_file_name(new_filename)
}

/// Save data to the data directory asynchronously
pub fn save_to_data_dir(
    filename: &str,
    data: Vec<u8>,
    overwrite: bool,
) -> Option<Receiver<AsyncFileResult>> {
    let path = get_data_file_path(filename)?;
    Some(write_file_async(path, data, overwrite))
}

/// Save data to the cache directory asynchronously
pub fn save_to_cache_dir(
    filename: &str,
    data: Vec<u8>,
    overwrite: bool,
) -> Option<Receiver<AsyncFileResult>> {
    let path = get_cache_file_path(filename)?;
    Some(write_file_async(path, data, overwrite))
}

/// Read data from the data directory asynchronously
pub fn read_from_data_dir(filename: &str) -> Option<Receiver<AsyncFileResult>> {
    let path = get_data_file_path(filename)?;
    if path.exists() {
        Some(read_file_async(path))
    } else {
        None
    }
}

/// Read data from the cache directory asynchronously
pub fn read_from_cache_dir(filename: &str) -> Option<Receiver<AsyncFileResult>> {
    let path = get_cache_file_path(filename)?;
    if path.exists() {
        Some(read_file_async(path))
    } else {
        None
    }
}

// ============================================================================
// Configuration Persistence (TOML format)
// ============================================================================

/// Persistent configuration structure that can be serialized to TOML.
///
/// Uses `RendererKind` for runtime-switchable renderer persistence.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct PersistentConfig {
    pub user_config: PersistentUserConfig,
    pub view_config: PersistentViewConfig,
    pub speed_config: PersistentSpeedConfig,
    pub console_config: PersistentConsoleConfig,
    #[serde(default)]
    pub keybindings: KeybindingsConfig,
}

impl From<&AppConfig> for PersistentConfig {
    fn from(value: &AppConfig) -> Self {
        Self {
            user_config: (&value.user_config).into(),
            view_config: (&value.view_config).into(),
            speed_config: (&value.speed_config).into(),
            console_config: (&value.console_config).into(),
            keybindings: value.keybindings.clone(),
        }
    }
}

impl From<&PersistentConfig> for AppConfig {
    fn from(value: &PersistentConfig) -> Self {
        Self {
            view_config: (&value.view_config).into(),
            speed_config: (&value.speed_config).into(),
            auto_pause_state: Default::default(),
            user_config: (&value.user_config).into(),
            console_config: (&value.console_config).into(),
            pending_dialogs: Default::default(),
            keybindings: value.keybindings.clone(),
        }
    }
}

/// Persistent View configuration with RendererKind for runtime switching.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PersistentViewConfig {
    pub show_palette: bool,
    pub show_pattern_table: bool,
    pub show_nametable: bool,
    pub required_debug_fetches: HashSet<PersistentEmulatorFetchable>,
    /// The serialized renderer state. When present, the renderer is restored
    /// from this. When absent (e.g., first run), a default renderer is
    /// created.
    #[serde(default)]
    pub renderer: String,
}

impl Default for PersistentViewConfig {
    fn default() -> Self {
        Self {
            show_palette: false,
            show_pattern_table: false,
            show_nametable: false,
            required_debug_fetches: HashSet::new(),
            renderer: NoneRenderer::new().get_id().to_string(),
        }
    }
}

impl From<&ViewConfig> for PersistentViewConfig {
    fn from(config: &ViewConfig) -> Self {
        Self {
            show_palette: config.show_palette,
            show_pattern_table: config.show_pattern_table,
            show_nametable: config.show_nametable,
            required_debug_fetches: config
                .required_debug_fetches
                .iter()
                .map(|f| f.into())
                .collect(),
            renderer: config.renderer.get_id().to_string(),
        }
    }
}

impl From<&PersistentViewConfig> for ViewConfig {
    fn from(config: &PersistentViewConfig) -> Self {
        // If renderer was persisted, use it; otherwise create a default
        let renderer = create_renderer(Some(config.renderer.as_str()), get_all_renderers());

        Self {
            palette_rgb_data: RgbPalette::default(),
            show_nametable: config.show_nametable,
            show_palette: config.show_palette,
            show_pattern_table: config.show_pattern_table,
            required_debug_fetches: Default::default(),
            renderer,
        }
    }
}
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Hash)]
pub enum PersistentEmulatorFetchable {
    Palettes,
    Tiles,
    Nametables,
    Sprites,
    SoamSprites,
}

impl From<&EmulatorFetchable> for PersistentEmulatorFetchable {
    fn from(fetchable: &EmulatorFetchable) -> Self {
        match fetchable {
            EmulatorFetchable::Palettes(_) => PersistentEmulatorFetchable::Palettes,
            EmulatorFetchable::Tiles(_) => PersistentEmulatorFetchable::Tiles,
            EmulatorFetchable::Nametables(_) => PersistentEmulatorFetchable::Nametables,
            EmulatorFetchable::Sprites(_) => PersistentEmulatorFetchable::Sprites,
            EmulatorFetchable::SoamSprites(_) => PersistentEmulatorFetchable::SoamSprites,
        }
    }
}

impl From<PersistentEmulatorFetchable> for EmulatorFetchable {
    fn from(fetchable: PersistentEmulatorFetchable) -> Self {
        match fetchable {
            PersistentEmulatorFetchable::Palettes => EmulatorFetchable::Palettes(None),
            PersistentEmulatorFetchable::Tiles => EmulatorFetchable::Tiles(None),
            PersistentEmulatorFetchable::Nametables => EmulatorFetchable::Nametables(None),
            PersistentEmulatorFetchable::Sprites => EmulatorFetchable::Sprites(None),
            PersistentEmulatorFetchable::SoamSprites => EmulatorFetchable::SoamSprites(None),
        }
    }
}

/// Persistent user configuration - stores display names instead of paths for
/// WASM compatibility
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct PersistentUserConfig {
    /// Last loaded palette filename (display only)
    pub previous_palette_name: Option<String>,
    /// Last loaded palette directory (for file picker initial directory)
    pub previous_palette_dir: Option<StorageKey>,
    /// Last loaded ROM filename (display only)
    pub previous_rom_name: Option<String>,
    /// Last loaded ROM directory (for file picker initial directory)
    pub previous_rom_dir: Option<StorageKey>,
    /// Last loaded savestate filename (display only)
    pub previous_savestate_name: Option<String>,
    /// Last loaded savestate directory (for file picker initial directory)
    pub previous_savestate_dir: Option<StorageKey>,
    /// Last saved palette directory (for file picker initial directory)
    #[serde(default)]
    pub previous_palette_save_dir: Option<StorageKey>,
    /// Last saved savestate directory (for file picker initial directory)
    #[serde(default)]
    pub previous_savestate_save_dir: Option<StorageKey>,
    pub pattern_edit_color: u8,
    #[serde(default)]
    pub debug_active_palette: usize,
}

impl From<&UserConfig> for PersistentUserConfig {
    fn from(config: &UserConfig) -> Self {
        Self {
            previous_palette_name: config.previous_palette_name.clone(),
            previous_palette_dir: config.previous_palette_load_dir.clone(),
            previous_rom_name: config.previous_rom_name.clone(),
            previous_rom_dir: config.previous_rom_load_dir.clone(),
            previous_savestate_name: config.previous_savestate_name.clone(),
            previous_savestate_dir: config.previous_savestate_load_dir.clone(),
            previous_palette_save_dir: config.previous_palette_save_dir.clone(),
            previous_savestate_save_dir: config.previous_savestate_save_dir.clone(),
            pattern_edit_color: config.pattern_edit_color,
            debug_active_palette: config.debug_active_palette,
        }
    }
}

impl From<&PersistentUserConfig> for UserConfig {
    fn from(config: &PersistentUserConfig) -> Self {
        Self {
            previous_palette_name: config.previous_palette_name.clone(),
            previous_palette_load_dir: config.previous_palette_dir.clone(),
            previous_rom_name: config.previous_rom_name.clone(),
            previous_rom_load_dir: config.previous_rom_dir.clone(),
            previous_savestate_name: config.previous_savestate_name.clone(),
            previous_savestate_load_dir: config.previous_savestate_dir.clone(),
            previous_palette_save_dir: config.previous_palette_save_dir.clone(),
            previous_savestate_save_dir: config.previous_savestate_save_dir.clone(),
            pattern_edit_color: config.pattern_edit_color,
            debug_active_palette: config.debug_active_palette,
        }
    }
}

/// Persistent speed configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PersistentSpeedConfig {
    pub app_speed: PersistentAppSpeed,
    pub debug_speed: PersistentDebugSpeed,
    pub custom_speed: u16,
    pub debug_custom_speed: u16,
}

impl Default for PersistentSpeedConfig {
    fn default() -> Self {
        Self {
            app_speed: PersistentAppSpeed::DefaultSpeed,
            debug_speed: PersistentDebugSpeed::Default,
            custom_speed: 100,
            debug_custom_speed: 10,
        }
    }
}

/// Persistent app speed enum (serializable version)
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
pub enum PersistentAppSpeed {
    #[default]
    DefaultSpeed,
    Uncapped,
    Custom,
}

impl From<AppSpeed> for PersistentAppSpeed {
    fn from(speed: AppSpeed) -> Self {
        match speed {
            AppSpeed::DefaultSpeed => PersistentAppSpeed::DefaultSpeed,
            AppSpeed::Uncapped => PersistentAppSpeed::Uncapped,
            AppSpeed::Custom => PersistentAppSpeed::Custom,
        }
    }
}

impl From<PersistentAppSpeed> for AppSpeed {
    fn from(speed: PersistentAppSpeed) -> Self {
        match speed {
            PersistentAppSpeed::DefaultSpeed => AppSpeed::DefaultSpeed,
            PersistentAppSpeed::Uncapped => AppSpeed::Uncapped,
            PersistentAppSpeed::Custom => AppSpeed::Custom,
        }
    }
}

/// Persistent debug speed enum (serializable version)
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
pub enum PersistentDebugSpeed {
    #[default]
    Default,
    InStep,
    Custom,
}

impl From<DebugSpeed> for PersistentDebugSpeed {
    fn from(speed: DebugSpeed) -> Self {
        match speed {
            DebugSpeed::DefaultSpeed => PersistentDebugSpeed::Default,
            DebugSpeed::InStep => PersistentDebugSpeed::InStep,
            DebugSpeed::Custom => PersistentDebugSpeed::Custom,
        }
    }
}

impl From<PersistentDebugSpeed> for DebugSpeed {
    fn from(speed: PersistentDebugSpeed) -> Self {
        match speed {
            PersistentDebugSpeed::Default => DebugSpeed::DefaultSpeed,
            PersistentDebugSpeed::InStep => DebugSpeed::InStep,
            PersistentDebugSpeed::Custom => DebugSpeed::Custom,
        }
    }
}

impl From<&SpeedConfig> for PersistentSpeedConfig {
    fn from(config: &SpeedConfig) -> Self {
        Self {
            app_speed: config.app_speed.into(),
            debug_speed: config.debug_speed.into(),
            custom_speed: config.custom_speed,
            debug_custom_speed: config.debug_custom_speed,
        }
    }
}

impl From<&PersistentSpeedConfig> for SpeedConfig {
    fn from(config: &PersistentSpeedConfig) -> Self {
        Self {
            app_speed: config.app_speed.into(),
            debug_speed: config.debug_speed.into(),
            is_paused: false, // Transient state - don't persist
            custom_speed: config.custom_speed,
            debug_custom_speed: config.debug_custom_speed,
        }
    }
}

/// Persistent console configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PersistentConsoleConfig {
    pub is_powered: bool,
}

impl Default for PersistentConsoleConfig {
    fn default() -> Self {
        Self {
            is_powered: true,
        }
    }
}

impl From<&ConsoleConfig> for PersistentConsoleConfig {
    fn from(config: &ConsoleConfig) -> Self {
        Self {
            is_powered: config.is_powered,
        }
    }
}

impl From<&PersistentConsoleConfig> for ConsoleConfig {
    fn from(config: &PersistentConsoleConfig) -> Self {
        Self {
            is_powered: config.is_powered,
            loaded_rom: None,
        }
    }
}

/// Load configuration from the config file asynchronously.
///
/// Returns `None` if:
/// - The config file doesn't exist (first run)
/// - The config file cannot be read or parsed
///
/// Parsing errors are logged to stderr but not treated as fatal,
/// allowing the application to start with default config if the
/// config file is malformed.
pub async fn load_config() -> Option<PersistentConfig> {
    let key = storage::config_key();
    let storage_impl = storage::get_storage();

    // Check if config exists using storage
    match storage_impl.exists(&key).await {
        Ok(false) => return None,
        Err(e) => {
            eprintln!("Failed to check if config exists: {}", e);
            return None;
        }
        Ok(true) => {}
    }

    let contents = match storage_impl.get(&key).await {
        Ok(data) => match String::from_utf8(data) {
            Ok(s) => s,
            Err(e) => {
                eprintln!("Config file is not valid UTF-8: {}", e);
                return None;
            }
        },
        Err(e) => {
            eprintln!("Failed to read config file: {}", e);
            return None;
        }
    };

    match toml::from_str(&contents) {
        Ok(config) => Some(config),
        Err(e) => {
            eprintln!("Failed to parse config file (using defaults): {}", e);
            None
        }
    }
}

/// Save configuration to the config file asynchronously
pub async fn save_config(config: &PersistentConfig) -> Result<(), String> {
    let key = storage::config_key();
    let storage_impl = storage::get_storage();

    let toml_string =
        toml::to_string_pretty(config).map_err(|e| format!("Failed to serialize config: {}", e))?;

    storage_impl
        .set(&key, toml_string.as_bytes().to_vec())
        .await
        .map_err(|e| format!("Failed to write config: {}", e))?;

    Ok(())
}

// ============================================================================
// Egui Storage Path
// ============================================================================

/// Get the path for egui's persistence storage
///
/// This is used to enable egui's built-in persistence for window layout,
/// theme, and other UI state.
pub fn get_egui_storage_path() -> Option<PathBuf> {
    get_config_dir().map(|dir| dir.join("egui_state"))
}