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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
//! Cross-platform storage abstraction for native and WASM environments.
//!
//! This module provides a unified interface for persistent storage that works
//! across:
//! - **Native**: Uses the file system via the `directories` crate for
//!   OS-appropriate paths
//! - **WASM**: Uses IndexedDB via `rexie` for structured data storage
//!
//! # Architecture
//!
//! The storage system is built around three main concepts:
//!
//! 1. **StorageKey**: Identifies what data is being stored (config, saves,
//!    palettes, etc.)
//! 2. **Storage trait**: Async interface for get/set/delete/list operations
//! 3. **Platform-specific implementations**: NativeStorage and WasmStorage
//!
//! # Usage
//!
//! ```ignore
//! // Get the platform-appropriate storage instance
//! let storage = get_storage();
//!
//! // Save some data
//! storage.set("saves/my_game/quicksave.sav", data).await?;
//!
//! // Read it back
//! let data = storage.get("saves/my_game/quicksave.sav").await?;
//!
//! // List all saves for a game
//! let saves = storage.list("saves/my_game/").await?;
//! ```
//!
//! # WASM Considerations
//!
//! On WASM, storage has different characteristics:
//! - **localStorage**: ~5MB limit, synchronous, string-only (not suitable for
//!   binary data)
//! - **IndexedDB**: Larger storage (~50MB+), async, supports binary data
//!   (recommended)
//!
//! This module uses IndexedDB for WASM to support save states and other binary
//! data.

use std::fmt::Display;
use std::path::PathBuf;

use async_trait::async_trait;
use serde::{Deserialize, Serialize};

/// Type alias for async storage results
pub type StorageResult<T> = Result<T, StorageError>;

/// Errors that can occur during storage operations
#[derive(Debug, Clone)]
pub enum StorageError {
    /// The requested key was not found
    NotFound,
    /// Failed to read data
    ReadError(String),
    /// Failed to write data
    WriteError(String),
    /// Failed to delete data
    DeleteError(String),
    /// Storage is not available on this platform
    NotAvailable,
    /// Serialization/deserialization error
    SerializationError(String),
    /// IndexedDB specific error (WASM only)
    #[cfg(target_arch = "wasm32")]
    IndexedDbError(String),
}

impl Display for StorageError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            StorageError::NotFound => write!(f, "Key not found"),
            StorageError::ReadError(e) => write!(f, "Read error: {}", e),
            StorageError::WriteError(e) => write!(f, "Write error: {}", e),
            StorageError::DeleteError(e) => write!(f, "Delete error: {}", e),
            StorageError::NotAvailable => write!(f, "Storage not available"),
            StorageError::SerializationError(e) => write!(f, "Serialization error: {}", e),
            #[cfg(target_arch = "wasm32")]
            StorageError::IndexedDbError(e) => write!(f, "IndexedDB error: {}", e),
        }
    }
}

impl std::error::Error for StorageError {}

/// Metadata about a stored item
#[derive(Debug, Clone)]
pub struct StorageMetadata {
    /// The key/path of the item
    pub key: StorageKey,
}

/// Storage categories for organizing data
///
/// These categories help organize data and may map to different directories
/// on native platforms or different IndexedDB object stores on WASM.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub enum StorageCategory {
    /// Application configuration (config.toml, keybindings, etc.)
    Config,
    /// User data (save states, quicksaves, autosaves)
    Data,
    /// Cached data that can be regenerated (thumbnails, compiled shaders)
    Cache,
    /// Data not managed by Monsoon, that still needs to be addressed via
    /// storage keys
    Root,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub struct StorageKey {
    pub category: StorageCategory,
    pub sub_path: String,
}

impl Display for StorageKey {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}/{}", self.category.prefix(), self.sub_path)
    }
}

impl From<&String> for StorageKey {
    fn from(value: &String) -> Self {
        let (prefix, mut sub) = value.split_once("/").unzip();
        let valid_prefixes = [
            StorageCategory::Config,
            StorageCategory::Data,
            StorageCategory::Cache,
        ];

        let category = if let Some(prefix) = prefix {
            valid_prefixes
                .iter()
                .find(|p| p.prefix() == prefix)
                .unwrap_or(&StorageCategory::Root)
        } else {
            sub = Some(value.as_str());
            &StorageCategory::Root
        };

        StorageKey {
            category: *category,
            sub_path: sub.unwrap_or("").to_string(),
        }
    }
}

impl StorageCategory {
    /// Get the string prefix for this category
    pub fn prefix(&self) -> &'static str {
        match self {
            StorageCategory::Config => "config",
            StorageCategory::Data => "data",
            StorageCategory::Cache => "cache",
            StorageCategory::Root => "/",
        }
    }
}

/// Async storage interface that works across platforms.
///
/// All operations are async to support both native (thread-based) and
/// WASM (Promise-based) implementations.
///
/// Note: On WASM, futures don't need to be Send since JavaScript is
/// single-threaded.
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
pub trait Storage: Send + Sync {
    /// Get data by key
    async fn get(&self, key: &StorageKey) -> StorageResult<Vec<u8>>;

    /// Set data for a key
    async fn set(&self, key: &StorageKey, data: Vec<u8>) -> StorageResult<()>;

    /// Delete data by key
    async fn delete(&self, key: &StorageKey) -> StorageResult<()>;

    /// Check if a key exists
    async fn exists(&self, key: &StorageKey) -> StorageResult<bool>;

    /// List all keys with a given prefix
    async fn list(&self, prefix: &StorageKey) -> StorageResult<Vec<StorageMetadata>>;

    /// Get the full path/URL for a key (for display purposes)
    fn get_display_path(&self, key: &StorageKey) -> String;
    fn key_to_path_opt(&self, key: Option<&StorageKey>) -> Option<PathBuf>;
    fn key_to_path(&self, key: &StorageKey) -> Option<PathBuf>;
}

// ============================================================================
// Native Implementation
// ============================================================================

#[cfg(not(target_arch = "wasm32"))]
mod native {
    use std::io::{Read, Write};
    use std::path::{Path, PathBuf};
    use std::sync::OnceLock;

    use async_trait::async_trait;
    use directories::ProjectDirs;

    use crate::frontend::storage::{
        Storage, StorageCategory, StorageError, StorageKey, StorageMetadata, StorageResult,
    };

    const APP_QUALIFIER: &str = "com";
    const APP_ORGANIZATION: &str = "Lightsong";
    const APP_NAME: &str = "MonsoonEmulator";

    static PROJECT_DIRS: OnceLock<Option<ProjectDirs>> = OnceLock::new();

    fn get_project_dirs() -> Option<&'static ProjectDirs> {
        PROJECT_DIRS
            .get_or_init(|| ProjectDirs::from(APP_QUALIFIER, APP_ORGANIZATION, APP_NAME))
            .as_ref()
    }

    /// Native file system storage implementation
    pub struct NativeStorage;

    impl Default for NativeStorage {
        fn default() -> Self { Self::new() }
    }

    impl NativeStorage {
        pub fn new() -> Self { NativeStorage }

        fn get_base_dir(&self, category: &StorageCategory) -> Option<PathBuf> {
            let dirs = get_project_dirs()?;
            let base = match category {
                StorageCategory::Config => dirs.config_dir(),
                StorageCategory::Data => dirs.data_dir(),
                StorageCategory::Cache => dirs.cache_dir(),
                StorageCategory::Root => Path::new("/"),
            };
            Some(base.to_path_buf())
        }
    }

    #[async_trait]
    impl Storage for NativeStorage {
        async fn get(&self, key: &StorageKey) -> StorageResult<Vec<u8>> {
            let path = self.key_to_path(key).ok_or(StorageError::NotAvailable)?;

            if !path.exists() {
                return Err(StorageError::NotFound);
            }

            let mut file =
                std::fs::File::open(&path).map_err(|e| StorageError::ReadError(e.to_string()))?;

            let mut data = Vec::new();
            file.read_to_end(&mut data)
                .map_err(|e| StorageError::ReadError(e.to_string()))?;

            Ok(data)
        }

        async fn set(&self, key: &StorageKey, data: Vec<u8>) -> StorageResult<()> {
            let path = self.key_to_path(key).ok_or(StorageError::NotAvailable)?;

            // Create parent directories
            if let Some(parent) = path.parent() {
                std::fs::create_dir_all(parent)
                    .map_err(|e| StorageError::WriteError(e.to_string()))?;
            }

            let mut file = std::fs::File::create(&path)
                .map_err(|e| StorageError::WriteError(e.to_string()))?;

            file.write_all(&data)
                .map_err(|e| StorageError::WriteError(e.to_string()))?;

            Ok(())
        }

        async fn delete(&self, key: &StorageKey) -> StorageResult<()> {
            let path = self.key_to_path(key).ok_or(StorageError::NotAvailable)?;

            if path.exists() {
                std::fs::remove_file(&path)
                    .map_err(|e| StorageError::DeleteError(e.to_string()))?;
            }

            Ok(())
        }

        async fn exists(&self, key: &StorageKey) -> StorageResult<bool> {
            let path = self.key_to_path(key).ok_or(StorageError::NotAvailable)?;

            Ok(path.exists())
        }

        async fn list(&self, prefix: &StorageKey) -> StorageResult<Vec<StorageMetadata>> {
            let base_path = self.key_to_path(prefix).ok_or(StorageError::NotAvailable)?;

            if !base_path.exists() {
                return Ok(Vec::new());
            }

            let mut results = Vec::new();

            if base_path.is_dir() {
                Self::collect_files(&base_path, prefix, &mut results)?;
            } else if base_path.is_file() {
                results.push(StorageMetadata {
                    key: prefix.clone(),
                });
            }

            Ok(results)
        }

        fn get_display_path(&self, key: &StorageKey) -> String {
            self.key_to_path(key)
                .map(|p| p.display().to_string())
                .unwrap_or_else(|| key.sub_path.to_string())
        }

        fn key_to_path_opt(&self, key: Option<&StorageKey>) -> Option<PathBuf> {
            if let Some(key) = key {
                self.key_to_path(key)
            } else {
                None
            }
        }

        fn key_to_path(&self, key: &StorageKey) -> Option<PathBuf> {
            let base = self.get_base_dir(&key.category)?;
            Some(base.join(key.sub_path.clone()))
        }
    }

    impl NativeStorage {
        fn collect_files(
            dir: &PathBuf,
            prefix: &StorageKey,
            results: &mut Vec<StorageMetadata>,
        ) -> StorageResult<()> {
            let entries =
                std::fs::read_dir(dir).map_err(|e| StorageError::ReadError(e.to_string()))?;

            for entry in entries.flatten() {
                let path = entry.path();
                let name = entry.file_name().to_string_lossy().to_string();
                let sub = if prefix.sub_path.ends_with('/') {
                    format!("{}{}", prefix.sub_path, name)
                } else {
                    format!("{}/{}", prefix.sub_path, name)
                };

                let key = StorageKey {
                    category: prefix.category,
                    sub_path: sub,
                };

                if path.is_file() {
                    results.push(StorageMetadata {
                        key,
                    });
                } else if path.is_dir() {
                    Self::collect_files(&path, &key, results)?;
                }
            }

            Ok(())
        }
    }
}

// ============================================================================
// WASM Implementation
// ============================================================================

#[cfg(target_arch = "wasm32")]
mod wasm {
    use js_sys::Uint8Array;
    use rexie::{KeyRange, Rexie, TransactionMode};
    use wasm_bindgen::JsValue;

    use super::*;

    const DB_NAME: &str = "monsoon_emulator";
    const DB_VERSION: u32 = 1;
    const STORE_NAME: &str = "storage";

    /// WASM storage implementation using IndexedDB via rexie.
    ///
    /// Provides persistent storage in the browser using IndexedDB,
    /// which supports larger storage limits and binary data.
    ///
    /// # Database Structure
    ///
    /// - Database name: "monsoon_emulator"
    /// - Object store: "storage" (key-value pairs where key is the StorageKey
    ///   path string)
    /// - Values are stored as Uint8Array (raw bytes)
    /// - Prefix queries use KeyRange::bound() on the primary key for efficient
    ///   listing
    pub struct WasmStorage;

    impl WasmStorage {
        pub fn new() -> Self { WasmStorage }

        /// Convert StorageKey to the string key used in IndexedDB
        fn key_string(key: &StorageKey) -> String {
            format!("{}/{}", key.category.prefix(), key.sub_path)
        }
    }

    impl Default for WasmStorage {
        fn default() -> Self { Self::new() }
    }

    /// Open the IndexedDB database, creating the object store if needed.
    async fn open_db() -> Result<Rexie, StorageError> {
        Rexie::builder(DB_NAME)
            .version(DB_VERSION)
            .add_object_store(rexie::ObjectStore::new(STORE_NAME))
            .build()
            .await
            .map_err(|e| StorageError::IndexedDbError(e.to_string()))
    }

    #[async_trait(?Send)]
    impl Storage for WasmStorage {
        async fn get(&self, key: &StorageKey) -> StorageResult<Vec<u8>> {
            let db = open_db().await?;
            let tx = db
                .transaction(&[STORE_NAME], TransactionMode::ReadOnly)
                .map_err(|e| StorageError::ReadError(e.to_string()))?;
            let store = tx
                .store(STORE_NAME)
                .map_err(|e| StorageError::ReadError(e.to_string()))?;

            let key_js = JsValue::from_str(&Self::key_string(key));
            match store
                .get(key_js)
                .await
                .map_err(|e| StorageError::ReadError(e.to_string()))?
            {
                Some(val) => {
                    let array = Uint8Array::new(&val);
                    Ok(array.to_vec())
                }
                None => Err(StorageError::NotFound),
            }
        }

        async fn set(&self, key: &StorageKey, data: Vec<u8>) -> StorageResult<()> {
            let db = open_db().await?;
            let tx = db
                .transaction(&[STORE_NAME], TransactionMode::ReadWrite)
                .map_err(|e| StorageError::WriteError(e.to_string()))?;
            let store = tx
                .store(STORE_NAME)
                .map_err(|e| StorageError::WriteError(e.to_string()))?;

            let key_js = JsValue::from_str(&Self::key_string(key));
            let value_js: JsValue = Uint8Array::from(data.as_slice()).into();
            store
                .put(&value_js, Some(&key_js))
                .await
                .map_err(|e| StorageError::WriteError(e.to_string()))?;
            tx.done()
                .await
                .map_err(|e| StorageError::WriteError(e.to_string()))?;
            Ok(())
        }

        async fn delete(&self, key: &StorageKey) -> StorageResult<()> {
            let db = open_db().await?;
            let tx = db
                .transaction(&[STORE_NAME], TransactionMode::ReadWrite)
                .map_err(|e| StorageError::DeleteError(e.to_string()))?;
            let store = tx
                .store(STORE_NAME)
                .map_err(|e| StorageError::DeleteError(e.to_string()))?;

            let key_js = JsValue::from_str(&Self::key_string(key));
            store
                .delete(key_js)
                .await
                .map_err(|e| StorageError::DeleteError(e.to_string()))?;
            tx.done()
                .await
                .map_err(|e| StorageError::DeleteError(e.to_string()))?;
            Ok(())
        }

        async fn exists(&self, key: &StorageKey) -> StorageResult<bool> {
            let db = open_db().await?;
            let tx = db
                .transaction(&[STORE_NAME], TransactionMode::ReadOnly)
                .map_err(|e| StorageError::ReadError(e.to_string()))?;
            let store = tx
                .store(STORE_NAME)
                .map_err(|e| StorageError::ReadError(e.to_string()))?;

            let key_js = JsValue::from_str(&Self::key_string(key));
            store
                .key_exists(key_js)
                .await
                .map_err(|e| StorageError::ReadError(e.to_string()))
        }

        async fn list(&self, prefix: &StorageKey) -> StorageResult<Vec<StorageMetadata>> {
            let db = open_db().await?;
            let tx = db
                .transaction(&[STORE_NAME], TransactionMode::ReadOnly)
                .map_err(|e| StorageError::ReadError(e.to_string()))?;
            let store = tx
                .store(STORE_NAME)
                .map_err(|e| StorageError::ReadError(e.to_string()))?;

            let prefix_str = Self::key_string(prefix);
            let lower = JsValue::from_str(&prefix_str);
            let upper = JsValue::from_str(&format!("{}\u{ffff}", prefix_str));
            let range = KeyRange::bound(&lower, &upper, Some(false), Some(false))
                .map_err(|e| StorageError::ReadError(format!("{:?}", e)))?;

            let keys = store
                .get_all_keys(Some(range), None)
                .await
                .map_err(|e| StorageError::ReadError(e.to_string()))?;

            Ok(keys
                .into_iter()
                .filter_map(|k| {
                    k.as_string().map(|s| StorageMetadata {
                        key: StorageKey::from(&s),
                    })
                })
                .collect())
        }

        fn get_display_path(&self, key: &StorageKey) -> String {
            format!("indexeddb://monsoon_emulator/{}", Self::key_string(key))
        }

        fn key_to_path_opt(&self, _key: Option<&StorageKey>) -> Option<PathBuf> {
            // WASM doesn't have filesystem paths
            None
        }

        fn key_to_path(&self, _key: &StorageKey) -> Option<PathBuf> {
            // WASM doesn't have filesystem paths
            None
        }
    }
}

// ============================================================================
// Platform Selection
// ============================================================================

#[cfg(not(target_arch = "wasm32"))]
pub use native::NativeStorage;
#[cfg(target_arch = "wasm32")]
pub use wasm::WasmStorage;

/// Get the platform-appropriate storage implementation
#[cfg(not(target_arch = "wasm32"))]
pub fn get_storage() -> impl Storage { NativeStorage::new() }

/// Get the platform-appropriate storage implementation
#[cfg(target_arch = "wasm32")]
pub fn get_storage() -> impl Storage { WasmStorage::new() }

// ============================================================================
// Helper Functions
// ============================================================================

/// Generate a storage key for a quicksave
pub fn quicksave_key(game_name: &str, timestamp: &str) -> StorageKey {
    let sub = format!("saves/{}/quicksaves/quicksave_{}.sav", game_name, timestamp);

    StorageKey {
        category: StorageCategory::Data,
        sub_path: sub,
    }
}

/// Generate a storage key for an autosave
pub fn autosave_key(game_name: &str, timestamp: &str) -> StorageKey {
    let sub = format!("saves/{}/autosaves/autosaves_{}.sav", game_name, timestamp);

    StorageKey {
        category: StorageCategory::Data,
        sub_path: sub,
    }
}

/// Generate the prefix for listing autosaves for a game
pub fn autosaves_prefix(game_name: &str) -> StorageKey {
    let sub = format!("saves/{}/autosaves/", game_name);

    StorageKey {
        category: StorageCategory::Data,
        sub_path: sub,
    }
}

/// Generate the prefix for listing quicksaves for a game
pub fn quicksaves_prefix(game_name: &str) -> StorageKey {
    let sub = format!("saves/{}/quicksaves/", game_name);

    StorageKey {
        category: StorageCategory::Data,
        sub_path: sub,
    }
}

/// Generate a storage key for the application config
pub fn config_key() -> StorageKey {
    StorageKey {
        category: StorageCategory::Config,
        sub_path: "config/config.toml".to_string(),
    }
}

/// Generate a storage key for egui state
pub fn egui_state_key() -> StorageKey {
    StorageKey {
        category: StorageCategory::Config,
        sub_path: "egui_state".to_string(),
    }
}

/// Generate a storage key for a cached ROM file
pub fn rom_cache_key(filename: &str) -> StorageKey {
    StorageKey {
        category: StorageCategory::Data,
        sub_path: format!("roms/{}", filename),
    }
}

/// Generate the prefix for listing all cached ROMs
pub fn roms_prefix() -> StorageKey {
    StorageKey {
        category: StorageCategory::Data,
        sub_path: "roms/".to_string(),
    }
}

/// Generate a storage key for a cached uploaded savestate
pub fn uploaded_savestate_key(filename: &str) -> StorageKey {
    StorageKey {
        category: StorageCategory::Data,
        sub_path: format!("uploads/savestates/{}", filename),
    }
}

// ============================================================================
// Synchronous Wrappers (Native Only)
// ============================================================================
//
// These provide synchronous access to storage for code that can't be async,
// such as startup config loading and shutdown config saving.

#[cfg(not(target_arch = "wasm32"))]
mod sync_wrappers {
    use crate::frontend::storage::{
        NativeStorage, Storage, StorageError, StorageKey, StorageMetadata, StorageResult,
    };

    /// Global storage instance for synchronous access
    static STORAGE: std::sync::OnceLock<NativeStorage> = std::sync::OnceLock::new();

    fn get_storage_instance() -> &'static NativeStorage { STORAGE.get_or_init(NativeStorage::new) }

    /// Get the full filesystem path for a storage key (native only)
    pub fn get_path_for_key(key: &StorageKey) -> Option<std::path::PathBuf> {
        get_storage_instance().key_to_path(key)
    }

    /// Read data synchronously from storage
    pub fn read_sync(key: &StorageKey) -> StorageResult<Vec<u8>> {
        let storage = get_storage_instance();
        let path = storage.key_to_path(key).ok_or(StorageError::NotAvailable)?;

        if !path.exists() {
            return Err(StorageError::NotFound);
        }

        std::fs::read(&path).map_err(|e| StorageError::ReadError(e.to_string()))
    }

    /// Write data synchronously to storage
    pub fn write_sync(key: &StorageKey, data: &[u8]) -> StorageResult<()> {
        let storage = get_storage_instance();
        let path = storage.key_to_path(key).ok_or(StorageError::NotAvailable)?;

        // Create parent directories
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent).map_err(|e| StorageError::WriteError(e.to_string()))?;
        }

        std::fs::write(&path, data).map_err(|e| StorageError::WriteError(e.to_string()))
    }

    /// Delete data synchronously from storage
    pub fn delete_sync(key: &StorageKey) -> StorageResult<()> {
        let storage = get_storage_instance();
        let path = storage.key_to_path(key).ok_or(StorageError::NotAvailable)?;

        if path.exists() {
            std::fs::remove_file(&path).map_err(|e| StorageError::DeleteError(e.to_string()))?;
        }

        Ok(())
    }

    /// Check if a key exists synchronously
    pub fn exists_sync(key: &StorageKey) -> StorageResult<bool> {
        let storage = get_storage_instance();
        let path = storage.key_to_path(key).ok_or(StorageError::NotAvailable)?;

        Ok(path.exists())
    }

    /// List all keys with a given prefix synchronously
    pub fn list_sync(prefix: &StorageKey) -> StorageResult<Vec<StorageMetadata>> {
        let storage = get_storage_instance();
        let base_path = storage
            .key_to_path(prefix)
            .ok_or(StorageError::NotAvailable)?;

        if !base_path.exists() {
            return Ok(Vec::new());
        }

        let mut results = Vec::new();

        if base_path.is_dir() {
            collect_files_sync(&base_path, prefix, &mut results)?;
        } else if base_path.is_file() {
            results.push(StorageMetadata {
                key: prefix.clone(),
            });
        }

        Ok(results)
    }

    /// Get the display path for a key
    pub fn get_display_path(key: &StorageKey) -> String {
        get_storage_instance().get_display_path(key)
    }

    fn collect_files_sync(
        dir: &std::path::Path,
        prefix: &StorageKey,
        results: &mut Vec<StorageMetadata>,
    ) -> StorageResult<()> {
        let entries = std::fs::read_dir(dir).map_err(|e| StorageError::ReadError(e.to_string()))?;

        for entry in entries.flatten() {
            let path = entry.path();
            let name = entry.file_name().to_string_lossy().to_string();
            let sub = if prefix.sub_path.ends_with('/') {
                format!("{}{}", prefix.sub_path, name)
            } else {
                format!("{}/{}", prefix.sub_path, name)
            };

            let key = StorageKey {
                category: prefix.category,
                sub_path: sub,
            };

            if path.is_file() {
                results.push(StorageMetadata {
                    key,
                });
            } else if path.is_dir() {
                collect_files_sync(&path, &key, results)?;
            }
        }

        Ok(())
    }
}

#[cfg(not(target_arch = "wasm32"))]
pub use sync_wrappers::*;