structfs-core-store 0.2.0

Core StructFS store traits - Record, Value, Path, Format
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
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
//! OverlayStore: Route reads/writes to different stores based on path prefixes.
//!
//! This implementation uses a prefix trie for efficient routing with fallthrough
//! semantics - the deepest matching prefix handles the request.
//!
//! Supports both direct store mounts and redirects (path aliases) with cycle detection.

use std::collections::HashSet;

use crate::path_trie::PathTrie;
use crate::{Error, Path, PathError, Reader, Record, Writer};

/// A boxed store that is Send + Sync.
pub type StoreBox = Box<dyn Store + Send + Sync>;

/// Combined read/write trait for stores.
pub trait Store: Reader + Writer {}
impl<T: Reader + Writer> Store for T {}

/// Access control for redirects.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RedirectMode {
    /// Allow reads through this redirect.
    ReadOnly,
    /// Allow writes through this redirect.
    WriteOnly,
    /// Allow both reads and writes.
    ReadWrite,
}

/// What a route points to.
pub enum RouteTarget {
    /// Direct store mount.
    Store(StoreBox),
    /// Redirect to another path in the overlay.
    Redirect {
        /// Target path to redirect to.
        target: Path,
        /// Access mode for this redirect.
        mode: RedirectMode,
        /// Which mount created this redirect (for cascade unmount).
        source_mount: Option<String>,
    },
}

/// Wraps a Reader to reject all writes.
pub struct OnlyReadable<R> {
    inner: R,
}

impl<R> OnlyReadable<R> {
    /// Create a new read-only wrapper.
    pub fn new(inner: R) -> Self {
        Self { inner }
    }

    /// Get a reference to the inner reader.
    pub fn inner(&self) -> &R {
        &self.inner
    }
}

impl<R: Reader> Reader for OnlyReadable<R> {
    fn read(&mut self, from: &Path) -> Result<Option<Record>, Error> {
        self.inner.read(from)
    }
}

impl<R: Reader + Send + Sync> Writer for OnlyReadable<R> {
    fn write(&mut self, to: &Path, _data: Record) -> Result<Path, Error> {
        Err(Error::Path(PathError::InvalidPath {
            message: format!("Path '{}' is read-only", to),
        }))
    }
}

/// Wraps a Writer to reject all reads.
pub struct OnlyWritable<W> {
    inner: W,
}

impl<W> OnlyWritable<W> {
    /// Create a new write-only wrapper.
    pub fn new(inner: W) -> Self {
        Self { inner }
    }

    /// Get a reference to the inner writer.
    pub fn inner(&self) -> &W {
        &self.inner
    }
}

impl<W: Writer + Send + Sync> Reader for OnlyWritable<W> {
    fn read(&mut self, _from: &Path) -> Result<Option<Record>, Error> {
        Ok(None)
    }
}

impl<W: Writer> Writer for OnlyWritable<W> {
    fn write(&mut self, to: &Path, data: Record) -> Result<Path, Error> {
        self.inner.write(to, data)
    }
}

/// Views into a sub-path of a store.
pub struct SubStoreView<S> {
    inner: S,
    prefix: Path,
}

impl<S> SubStoreView<S> {
    /// Create a new sub-store view.
    pub fn new(inner: S, prefix: Path) -> Self {
        Self { inner, prefix }
    }
}

impl<S: Reader> Reader for SubStoreView<S> {
    fn read(&mut self, from: &Path) -> Result<Option<Record>, Error> {
        self.inner.read(&self.prefix.join(from))
    }
}

impl<S: Writer> Writer for SubStoreView<S> {
    fn write(&mut self, to: &Path, data: Record) -> Result<Path, Error> {
        self.inner.write(&self.prefix.join(to), data)
    }
}

/// Route reads and writes to different stores based on path prefixes.
///
/// Uses a prefix trie internally for efficient routing. When a path is accessed,
/// the store walks the trie to find the deepest mounted store that matches the
/// path prefix, then delegates to that store with the remaining suffix.
///
/// Supports redirects (path aliases) that forward requests to other paths,
/// with cycle detection to prevent infinite loops.
///
/// # Example
///
/// ```rust
/// use structfs_core_store::{Reader, Writer, Record, Value, path};
/// use structfs_core_store::overlay_store::OverlayStore;
///
/// // Create an overlay
/// let mut overlay = OverlayStore::new();
///
/// // Mount stores at different paths
/// // overlay.mount(path!("users"), user_store);
/// // overlay.mount(path!("config"), config_store);
///
/// // Reads to /users/alice go to user_store with path "alice"
/// // Reads to /config/theme go to config_store with path "theme"
/// ```
pub struct OverlayStore {
    trie: PathTrie<RouteTarget>,
}

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

impl OverlayStore {
    /// Create a new empty overlay store.
    pub fn new() -> Self {
        Self {
            trie: PathTrie::new(),
        }
    }

    /// Mount a store at the given path prefix.
    ///
    /// Returns the previous store at that exact path if any.
    pub fn mount<S: Store + Send + Sync + 'static>(
        &mut self,
        path: Path,
        store: S,
    ) -> Option<StoreBox> {
        match self.trie.insert(&path, RouteTarget::Store(Box::new(store))) {
            Some(RouteTarget::Store(s)) => Some(s),
            _ => None,
        }
    }

    /// Mount a boxed store at the given path prefix.
    ///
    /// Returns the previous store at that exact path if any.
    pub fn mount_boxed(&mut self, path: Path, store: StoreBox) -> Option<StoreBox> {
        match self.trie.insert(&path, RouteTarget::Store(store)) {
            Some(RouteTarget::Store(s)) => Some(s),
            _ => None,
        }
    }

    /// Unmount store at exact path, keeping any nested mounts.
    ///
    /// Returns the removed store if found.
    pub fn unmount(&mut self, path: &Path) -> Option<StoreBox> {
        match self.trie.remove(path) {
            Some(RouteTarget::Store(s)) => Some(s),
            _ => None,
        }
    }

    /// Unmount entire subtree at path, returning it as a new OverlayStore.
    pub fn unmount_subtree(&mut self, path: &Path) -> Option<OverlayStore> {
        self.trie
            .remove_subtree(path)
            .map(|trie| OverlayStore { trie })
    }

    /// Add a redirect from one path to another.
    ///
    /// When a path under `from` is accessed, it will be redirected to
    /// the corresponding path under `to`.
    pub fn add_redirect(
        &mut self,
        from: Path,
        to: Path,
        mode: RedirectMode,
        source_mount: Option<String>,
    ) {
        self.trie.insert(
            &from,
            RouteTarget::Redirect {
                target: to,
                mode,
                source_mount,
            },
        );
    }

    /// Remove all redirects created by a specific mount.
    pub fn remove_redirects_for_mount(&mut self, mount_name: &str) {
        // Collect paths to remove (can't mutate while iterating)
        let to_remove: Vec<Path> = self
            .trie
            .iter()
            .filter_map(|(path, target)| match target {
                RouteTarget::Redirect {
                    source_mount: Some(src),
                    ..
                } if src == mount_name => Some(path),
                _ => None,
            })
            .collect();

        for path in to_remove {
            self.trie.remove(&path);
        }
    }

    /// List all redirects.
    pub fn list_redirects(&self) -> Vec<(Path, Path, RedirectMode)> {
        self.trie
            .iter()
            .filter_map(|(from, target)| match target {
                RouteTarget::Redirect { target, mode, .. } => Some((from, target.clone(), *mode)),
                _ => None,
            })
            .collect()
    }

    /// Check if any store would handle this path (fallthrough).
    pub fn has_route(&self, path: &Path) -> bool {
        self.trie.find_ancestor(path).is_some()
    }

    /// Number of mounted routes (stores + redirects).
    pub fn route_count(&self) -> usize {
        self.trie.len()
    }

    /// Number of mounted stores (excluding redirects).
    pub fn store_count(&self) -> usize {
        self.trie
            .iter()
            .filter(|(_, t)| matches!(t, RouteTarget::Store(_)))
            .count()
    }

    /// True if no routes mounted.
    pub fn is_empty(&self) -> bool {
        self.trie.is_empty()
    }

    /// Iterate over all store mount points (excluding redirects).
    pub fn mounts(&self) -> impl Iterator<Item = (Path, &StoreBox)> {
        self.trie.iter().filter_map(|(path, target)| match target {
            RouteTarget::Store(s) => Some((path, s)),
            _ => None,
        })
    }

    // === Deprecated methods for backwards compatibility ===

    /// Add a store layer at the given path prefix.
    ///
    /// Deprecated: use `mount()` instead.
    #[deprecated(note = "use mount() instead")]
    pub fn add_layer<S: Store + Send + Sync + 'static>(&mut self, mount_root: Path, store: S) {
        self.mount(mount_root, store);
    }

    /// Add a read-only layer.
    pub fn add_read_only_layer<R: Reader + Send + Sync + 'static>(
        &mut self,
        mount_root: Path,
        reader: R,
    ) {
        self.mount(mount_root, OnlyReadable::new(reader));
    }

    /// Add a write-only layer.
    pub fn add_write_only_layer<W: Writer + Send + Sync + 'static>(
        &mut self,
        mount_root: Path,
        writer: W,
    ) {
        self.mount(mount_root, OnlyWritable::new(writer));
    }

    /// Get the number of layers.
    ///
    /// Deprecated: use `store_count()` instead.
    #[deprecated(note = "use store_count() instead")]
    pub fn layer_count(&self) -> usize {
        self.store_count()
    }

    /// Remove a layer by its exact prefix path.
    ///
    /// Deprecated: use `unmount()` instead.
    #[deprecated(note = "use unmount() instead")]
    pub fn remove_layer(&mut self, prefix: &Path) -> Option<StoreBox> {
        self.unmount(prefix)
    }
}

/// Result of resolving a path through redirects.
struct ResolvedRoute<'a> {
    store: &'a mut StoreBox,
    suffix: Path,
    /// The prefix that led to this store (for reconstructing full paths).
    prefix: Path,
}

impl OverlayStore {
    /// Resolve a path for reading, following redirects with cycle detection.
    fn resolve_for_read(&mut self, path: &Path) -> Result<Option<ResolvedRoute<'_>>, Error> {
        let mut visited = HashSet::new();
        self.resolve_with_tracking(path, &mut visited, false)
    }

    /// Resolve a path for writing, following redirects with cycle detection.
    fn resolve_for_write(&mut self, path: &Path) -> Result<Option<ResolvedRoute<'_>>, Error> {
        let mut visited = HashSet::new();
        self.resolve_with_tracking(path, &mut visited, true)
    }

    fn resolve_with_tracking(
        &mut self,
        path: &Path,
        visited: &mut HashSet<Path>,
        is_write: bool,
    ) -> Result<Option<ResolvedRoute<'_>>, Error> {
        // Cycle detection - use the path prefix that matched, not the full path
        if !visited.insert(path.clone()) {
            return Err(Error::store(
                "overlay",
                if is_write { "write" } else { "read" },
                "redirect cycle detected",
            ));
        }

        // Find the ancestor route
        let ancestor = self.trie.find_ancestor(path);
        let (prefix_len, suffix, is_redirect, redirect_info) = match ancestor {
            Some((target, suffix)) => {
                let prefix_len = path.len() - suffix.len();
                match target {
                    RouteTarget::Store(_) => (prefix_len, suffix, false, None),
                    RouteTarget::Redirect { target, mode, .. } => {
                        // Check access mode
                        let allowed = !matches!(
                            (is_write, mode),
                            (false, RedirectMode::WriteOnly) | (true, RedirectMode::ReadOnly)
                        );
                        if !allowed {
                            return Ok(None);
                        }
                        (prefix_len, suffix, true, Some(target.clone()))
                    }
                }
            }
            None => return Ok(None),
        };

        // If it's a redirect, follow it recursively
        if is_redirect {
            if let Some(redirect_target) = redirect_info {
                let new_path = redirect_target.join(&suffix);
                return self.resolve_with_tracking(&new_path, visited, is_write);
            }
        }

        // It's a store - get mutable reference
        match self.trie.find_ancestor_mut(path) {
            Some((RouteTarget::Store(store), suffix)) => {
                let prefix = path.slice(0, prefix_len);
                Ok(Some(ResolvedRoute {
                    store,
                    suffix,
                    prefix,
                }))
            }
            _ => Ok(None),
        }
    }
}

impl Reader for OverlayStore {
    fn read(&mut self, from: &Path) -> Result<Option<Record>, Error> {
        match self.resolve_for_read(from)? {
            Some(resolved) => resolved.store.read(&resolved.suffix),
            None => Err(Error::NoRoute { path: from.clone() }),
        }
    }
}

impl Writer for OverlayStore {
    fn write(&mut self, to: &Path, data: Record) -> Result<Path, Error> {
        match self.resolve_for_write(to)? {
            Some(resolved) => {
                let result_suffix = resolved.store.write(&resolved.suffix, data)?;
                Ok(resolved.prefix.join(&result_suffix))
            }
            None => Err(Error::NoRoute { path: to.clone() }),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{path, Value};
    use std::collections::HashMap;

    // Simple in-memory store for testing
    struct TestStore {
        data: HashMap<Path, Record>,
    }

    impl TestStore {
        fn new(_name: &str) -> Self {
            Self {
                data: HashMap::new(),
            }
        }
    }

    impl Reader for TestStore {
        fn read(&mut self, from: &Path) -> Result<Option<Record>, Error> {
            Ok(self.data.get(from).cloned())
        }
    }

    impl Writer for TestStore {
        fn write(&mut self, to: &Path, data: Record) -> Result<Path, Error> {
            self.data.insert(to.clone(), data);
            Ok(to.clone())
        }
    }

    #[test]
    fn basic_routing() {
        let mut overlay = OverlayStore::new();

        let mut users_store = TestStore::new("users");
        users_store
            .write(&path!("alice"), Record::parsed(Value::from("Alice")))
            .unwrap();

        let mut config_store = TestStore::new("config");
        config_store
            .write(&path!("theme"), Record::parsed(Value::from("dark")))
            .unwrap();

        overlay.mount(path!("users"), users_store);
        overlay.mount(path!("config"), config_store);

        // Read from users
        let record = overlay.read(&path!("users/alice")).unwrap().unwrap();
        let value = record.into_value(&crate::NoCodec).unwrap();
        assert_eq!(value, Value::from("Alice"));

        // Read from config
        let record = overlay.read(&path!("config/theme")).unwrap().unwrap();
        let value = record.into_value(&crate::NoCodec).unwrap();
        assert_eq!(value, Value::from("dark"));
    }

    #[test]
    fn mount_replaces_existing() {
        let mut overlay = OverlayStore::new();

        let mut store1 = TestStore::new("first");
        store1
            .write(&path!("key"), Record::parsed(Value::from("first")))
            .unwrap();

        let mut store2 = TestStore::new("second");
        store2
            .write(&path!("key"), Record::parsed(Value::from("second")))
            .unwrap();

        // Mount store1, then replace with store2
        let old = overlay.mount(path!("data"), store1);
        assert!(old.is_none());

        let old = overlay.mount(path!("data"), store2);
        assert!(old.is_some());

        // store2 should be active
        let record = overlay.read(&path!("data/key")).unwrap().unwrap();
        let value = record.into_value(&crate::NoCodec).unwrap();
        assert_eq!(value, Value::from("second"));

        // Only one store mounted
        assert_eq!(overlay.store_count(), 1);
    }

    #[test]
    fn root_mount() {
        let mut overlay = OverlayStore::new();

        let mut store = TestStore::new("root");
        store
            .write(&path!("test"), Record::parsed(Value::from("value")))
            .unwrap();

        overlay.mount(path!(""), store);

        let record = overlay.read(&path!("test")).unwrap().unwrap();
        let value = record.into_value(&crate::NoCodec).unwrap();
        assert_eq!(value, Value::from("value"));
    }

    #[test]
    fn no_route_error() {
        let mut overlay = OverlayStore::new();

        let result = overlay.read(&path!("nonexistent"));
        assert!(result.is_err());
    }

    #[test]
    fn write_through_overlay() {
        let mut overlay = OverlayStore::new();
        overlay.mount(path!("data"), TestStore::new("data"));

        let result = overlay.write(&path!("data/key"), Record::parsed(Value::from("value")));
        assert!(result.is_ok());

        let record = overlay.read(&path!("data/key")).unwrap().unwrap();
        let value = record.into_value(&crate::NoCodec).unwrap();
        assert_eq!(value, Value::from("value"));
    }

    #[test]
    fn only_readable_rejects_writes() {
        let mut overlay = OverlayStore::new();
        overlay.add_read_only_layer(path!("readonly"), TestStore::new("readonly"));

        let result = overlay.write(&path!("readonly/key"), Record::parsed(Value::from("value")));
        assert!(result.is_err());
    }

    #[test]
    fn only_writable_returns_none_on_read() {
        let mut overlay = OverlayStore::new();
        overlay.add_write_only_layer(path!("writeonly"), TestStore::new("writeonly"));

        let result = overlay.read(&path!("writeonly/key")).unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn only_readable_inner() {
        let store = TestStore::new("test");
        let readable = OnlyReadable::new(store);
        let _inner = readable.inner();
        // Just verify it compiles and can be called
    }

    #[test]
    fn only_writable_inner() {
        let store = TestStore::new("test");
        let writable = OnlyWritable::new(store);
        let _inner = writable.inner();
        // Just verify it compiles and can be called
    }

    #[test]
    fn sub_store_view_read() {
        let mut store = TestStore::new("test");
        store
            .write(&path!("prefix/key"), Record::parsed(Value::from("value")))
            .unwrap();

        let mut view = SubStoreView::new(store, path!("prefix"));

        // Reading "key" should read "prefix/key" from the inner store
        let result = view.read(&path!("key")).unwrap().unwrap();
        let value = result.into_value(&crate::NoCodec).unwrap();
        assert_eq!(value, Value::from("value"));
    }

    #[test]
    fn sub_store_view_write() {
        let store = TestStore::new("test");
        let mut view = SubStoreView::new(store, path!("prefix"));

        // Writing to "key" should write to "prefix/key" in the inner store
        view.write(&path!("key"), Record::parsed(Value::from("data")))
            .unwrap();

        let result = view.read(&path!("key")).unwrap().unwrap();
        let value = result.into_value(&crate::NoCodec).unwrap();
        assert_eq!(value, Value::from("data"));
    }

    #[test]
    fn store_count() {
        let mut overlay = OverlayStore::new();
        assert_eq!(overlay.store_count(), 0);

        overlay.mount(path!("a"), TestStore::new("a"));
        assert_eq!(overlay.store_count(), 1);

        overlay.mount(path!("b"), TestStore::new("b"));
        assert_eq!(overlay.store_count(), 2);
    }

    #[test]
    fn overlay_store_default() {
        let overlay = OverlayStore::default();
        assert_eq!(overlay.store_count(), 0);
    }

    #[test]
    fn write_no_route_error() {
        let mut overlay = OverlayStore::new();
        let result = overlay.write(&path!("nonexistent"), Record::parsed(Value::Null));
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("no route"));
    }

    #[test]
    fn only_writable_write() {
        let store = TestStore::new("test");
        let mut writable = OnlyWritable::new(store);

        // Should be able to write
        let result = writable.write(&path!("key"), Record::parsed(Value::from("data")));
        assert!(result.is_ok());
    }

    #[test]
    fn only_readable_read() {
        let mut store = TestStore::new("test");
        store
            .write(&path!("key"), Record::parsed(Value::from("data")))
            .unwrap();

        let mut readable = OnlyReadable::new(store);

        // Should be able to read
        let result = readable.read(&path!("key")).unwrap().unwrap();
        let value = result.into_value(&crate::NoCodec).unwrap();
        assert_eq!(value, Value::from("data"));
    }

    #[test]
    fn only_readable_error_message() {
        let store = TestStore::new("test");
        let mut readable = OnlyReadable::new(store);

        let result = readable.write(&path!("test/path"), Record::parsed(Value::Null));
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("read-only"));
        assert!(err.to_string().contains("test/path"));
    }

    #[test]
    fn write_returns_full_path() {
        let mut overlay = OverlayStore::new();
        overlay.mount(path!("prefix"), TestStore::new("test"));

        // Write returns the full path including the prefix
        let result = overlay.write(&path!("prefix/key"), Record::parsed(Value::from("data")));
        assert_eq!(result.unwrap(), path!("prefix/key"));
    }

    #[test]
    fn nested_prefix() {
        let mut overlay = OverlayStore::new();

        let mut store = TestStore::new("nested");
        store
            .write(&path!("deep/key"), Record::parsed(Value::from("value")))
            .unwrap();

        overlay.mount(path!("a/b"), store);

        let result = overlay.read(&path!("a/b/deep/key")).unwrap().unwrap();
        let value = result.into_value(&crate::NoCodec).unwrap();
        assert_eq!(value, Value::from("value"));
    }

    #[test]
    fn unmount_existing() {
        let mut overlay = OverlayStore::new();
        overlay.mount(path!("a"), TestStore::new("a"));
        overlay.mount(path!("b"), TestStore::new("b"));
        assert_eq!(overlay.store_count(), 2);

        // Unmount "a"
        let removed = overlay.unmount(&path!("a"));
        assert!(removed.is_some());
        assert_eq!(overlay.store_count(), 1);

        // Reading from "a" should now fail
        let result = overlay.read(&path!("a/key"));
        assert!(result.is_err());

        // "b" should still work
        overlay
            .write(&path!("b/key"), Record::parsed(Value::from("test")))
            .unwrap();
        let result = overlay.read(&path!("b/key"));
        assert!(result.unwrap().is_some());
    }

    #[test]
    fn unmount_nonexistent() {
        let mut overlay = OverlayStore::new();
        overlay.mount(path!("a"), TestStore::new("a"));

        // Try to unmount non-existent
        let removed = overlay.unmount(&path!("nonexistent"));
        assert!(removed.is_none());
        assert_eq!(overlay.store_count(), 1);
    }

    #[test]
    fn unmount_keeps_children() {
        let mut overlay = OverlayStore::new();
        overlay.mount(path!("data"), TestStore::new("data"));
        overlay.mount(path!("data/nested"), TestStore::new("nested"));
        assert_eq!(overlay.store_count(), 2);

        // Write to nested
        overlay
            .write(
                &path!("data/nested/key"),
                Record::parsed(Value::from("nested_value")),
            )
            .unwrap();

        // Unmount data (not nested)
        overlay.unmount(&path!("data"));
        assert_eq!(overlay.store_count(), 1);

        // nested should still work
        let result = overlay.read(&path!("data/nested/key")).unwrap();
        assert!(result.is_some());
    }

    #[test]
    fn unmount_subtree_removes_all() {
        let mut overlay = OverlayStore::new();
        overlay.mount(path!("data"), TestStore::new("data"));
        overlay.mount(path!("data/cache"), TestStore::new("cache"));
        assert_eq!(overlay.store_count(), 2);

        let subtree = overlay.unmount_subtree(&path!("data")).unwrap();

        assert_eq!(subtree.store_count(), 2);
        assert!(overlay.is_empty());
    }

    #[test]
    fn deeper_mount_wins() {
        let mut overlay = OverlayStore::new();
        overlay.mount(path!("data"), TestStore::new("data"));
        overlay.mount(path!("data/special"), TestStore::new("special"));

        // Write to data/special/key goes to the special store
        overlay
            .write(
                &path!("data/special/key"),
                Record::parsed(Value::from("special")),
            )
            .unwrap();

        // Write to data/other/key goes to the data store
        overlay
            .write(
                &path!("data/other/key"),
                Record::parsed(Value::from("other")),
            )
            .unwrap();

        // Unmount special
        overlay.unmount(&path!("data/special"));

        // data should still work
        let result = overlay.read(&path!("data/other/key")).unwrap();
        assert!(result.is_some());

        // But data/special/key is now routed to data store (which doesn't have it)
        let result = overlay.read(&path!("data/special/key")).unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn has_route() {
        let mut overlay = OverlayStore::new();
        overlay.mount(path!("data"), TestStore::new("data"));

        assert!(overlay.has_route(&path!("data")));
        assert!(overlay.has_route(&path!("data/anything")));
        assert!(!overlay.has_route(&path!("other")));
    }

    #[test]
    fn is_empty() {
        let mut overlay = OverlayStore::new();
        assert!(overlay.is_empty());

        overlay.mount(path!("a"), TestStore::new("a"));
        assert!(!overlay.is_empty());

        overlay.unmount(&path!("a"));
        assert!(overlay.is_empty());
    }

    #[test]
    fn mounts_iteration() {
        let mut overlay = OverlayStore::new();
        overlay.mount(path!("a"), TestStore::new("a"));
        overlay.mount(path!("b"), TestStore::new("b"));
        overlay.mount(path!("a/c"), TestStore::new("c"));

        let mounts: Vec<_> = overlay.mounts().map(|(p, _)| p).collect();
        assert_eq!(mounts.len(), 3);
    }

    #[test]
    fn mount_boxed() {
        let mut overlay = OverlayStore::new();
        let store: StoreBox = Box::new(TestStore::new("boxed"));
        overlay.mount_boxed(path!("boxed"), store);

        overlay
            .write(&path!("boxed/key"), Record::parsed(Value::from("value")))
            .unwrap();
        let result = overlay.read(&path!("boxed/key")).unwrap();
        assert!(result.is_some());
    }

    // Tests for deprecated methods to ensure backwards compatibility
    #[test]
    #[allow(deprecated)]
    fn deprecated_add_layer() {
        let mut overlay = OverlayStore::new();
        overlay.add_layer(path!("data"), TestStore::new("data"));

        overlay
            .write(&path!("data/key"), Record::parsed(Value::from("value")))
            .unwrap();
        let result = overlay.read(&path!("data/key")).unwrap();
        assert!(result.is_some());
    }

    #[test]
    #[allow(deprecated)]
    fn deprecated_layer_count() {
        let mut overlay = OverlayStore::new();
        assert_eq!(overlay.layer_count(), 0);

        overlay.mount(path!("a"), TestStore::new("a"));
        assert_eq!(overlay.layer_count(), 1);
    }

    #[test]
    #[allow(deprecated)]
    fn deprecated_remove_layer() {
        let mut overlay = OverlayStore::new();
        overlay.mount(path!("a"), TestStore::new("a"));

        let removed = overlay.remove_layer(&path!("a"));
        assert!(removed.is_some());
        assert!(overlay.is_empty());
    }

    #[test]
    fn fallthrough_routing() {
        let mut overlay = OverlayStore::new();
        overlay.mount(path!("data"), TestStore::new("data"));

        // Write via fallthrough (path goes deep but store is at data)
        overlay
            .write(
                &path!("data/deep/nested/key"),
                Record::parsed(Value::from("value")),
            )
            .unwrap();

        // Read via fallthrough
        let result = overlay.read(&path!("data/deep/nested/key")).unwrap();
        assert!(result.is_some());
    }

    #[test]
    fn empty_path_mounts_at_root() {
        let mut overlay = OverlayStore::new();
        overlay.mount(path!(""), TestStore::new("root"));

        // Everything routes to root store
        overlay
            .write(&path!("any/path"), Record::parsed(Value::from("v")))
            .unwrap();
        let result = overlay.read(&path!("any/path")).unwrap();
        assert!(result.is_some());
    }

    // === Redirect tests ===

    #[test]
    fn redirect_basic() {
        let mut overlay = OverlayStore::new();

        // Mount a store at /data
        let mut store = TestStore::new("data");
        store
            .write(&path!("key"), Record::parsed(Value::from("value")))
            .unwrap();
        overlay.mount(path!("data"), store);

        // Add redirect: /alias -> /data
        overlay.add_redirect(path!("alias"), path!("data"), RedirectMode::ReadWrite, None);

        // Reading /alias/key should follow redirect to /data/key
        let result = overlay.read(&path!("alias/key")).unwrap().unwrap();
        let value = result.into_value(&crate::NoCodec).unwrap();
        assert_eq!(value, Value::from("value"));
    }

    #[test]
    fn redirect_write() {
        let mut overlay = OverlayStore::new();
        overlay.mount(path!("data"), TestStore::new("data"));

        // Add redirect: /alias -> /data
        overlay.add_redirect(path!("alias"), path!("data"), RedirectMode::ReadWrite, None);

        // Write via redirect
        overlay
            .write(&path!("alias/key"), Record::parsed(Value::from("written")))
            .unwrap();

        // Read via original path
        let result = overlay.read(&path!("data/key")).unwrap().unwrap();
        let value = result.into_value(&crate::NoCodec).unwrap();
        assert_eq!(value, Value::from("written"));
    }

    #[test]
    fn redirect_read_only() {
        let mut overlay = OverlayStore::new();

        let mut store = TestStore::new("data");
        store
            .write(&path!("key"), Record::parsed(Value::from("value")))
            .unwrap();
        overlay.mount(path!("data"), store);

        // Add read-only redirect
        overlay.add_redirect(
            path!("readonly"),
            path!("data"),
            RedirectMode::ReadOnly,
            None,
        );

        // Reading works
        let result = overlay.read(&path!("readonly/key")).unwrap();
        assert!(result.is_some());

        // Writing fails (returns no route)
        let result = overlay.write(&path!("readonly/key"), Record::parsed(Value::Null));
        assert!(result.is_err());
    }

    #[test]
    fn redirect_write_only() {
        let mut overlay = OverlayStore::new();
        overlay.mount(path!("data"), TestStore::new("data"));

        // Add write-only redirect
        overlay.add_redirect(
            path!("writeonly"),
            path!("data"),
            RedirectMode::WriteOnly,
            None,
        );

        // Writing works
        overlay
            .write(
                &path!("writeonly/key"),
                Record::parsed(Value::from("value")),
            )
            .unwrap();

        // Reading fails (returns no route)
        let result = overlay.read(&path!("writeonly/key"));
        assert!(result.is_err());

        // But original path still works
        let result = overlay.read(&path!("data/key")).unwrap();
        assert!(result.is_some());
    }

    #[test]
    fn redirect_cycle_detection() {
        let mut overlay = OverlayStore::new();

        // Create a cycle: /a -> /b -> /a
        overlay.add_redirect(path!("a"), path!("b"), RedirectMode::ReadWrite, None);
        overlay.add_redirect(path!("b"), path!("a"), RedirectMode::ReadWrite, None);

        // Attempting to resolve should detect the cycle
        let result = overlay.read(&path!("a/key"));
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("cycle"));
    }

    #[test]
    fn redirect_chain() {
        let mut overlay = OverlayStore::new();

        let mut store = TestStore::new("data");
        store
            .write(&path!("key"), Record::parsed(Value::from("chained")))
            .unwrap();
        overlay.mount(path!("data"), store);

        // Create a chain: /a -> /b -> /data
        overlay.add_redirect(path!("b"), path!("data"), RedirectMode::ReadWrite, None);
        overlay.add_redirect(path!("a"), path!("b"), RedirectMode::ReadWrite, None);

        // Reading through the chain works
        let result = overlay.read(&path!("a/key")).unwrap().unwrap();
        let value = result.into_value(&crate::NoCodec).unwrap();
        assert_eq!(value, Value::from("chained"));
    }

    #[test]
    fn redirect_remove_for_mount() {
        let mut overlay = OverlayStore::new();
        overlay.mount(path!("data"), TestStore::new("data"));

        // Add redirects with source_mount
        overlay.add_redirect(
            path!("alias1"),
            path!("data"),
            RedirectMode::ReadWrite,
            Some("mymount".to_string()),
        );
        overlay.add_redirect(
            path!("alias2"),
            path!("data"),
            RedirectMode::ReadWrite,
            Some("mymount".to_string()),
        );
        overlay.add_redirect(
            path!("other"),
            path!("data"),
            RedirectMode::ReadWrite,
            Some("othermount".to_string()),
        );

        assert_eq!(overlay.list_redirects().len(), 3);

        // Remove redirects for "mymount"
        overlay.remove_redirects_for_mount("mymount");

        // Only "other" redirect remains
        let redirects = overlay.list_redirects();
        assert_eq!(redirects.len(), 1);
        assert_eq!(redirects[0].0, path!("other"));
    }

    #[test]
    fn redirect_list() {
        let mut overlay = OverlayStore::new();
        overlay.mount(path!("data"), TestStore::new("data"));

        overlay.add_redirect(path!("alias"), path!("data"), RedirectMode::ReadOnly, None);

        let redirects = overlay.list_redirects();
        assert_eq!(redirects.len(), 1);
        assert_eq!(redirects[0].0, path!("alias"));
        assert_eq!(redirects[0].1, path!("data"));
        assert_eq!(redirects[0].2, RedirectMode::ReadOnly);
    }

    #[test]
    fn route_count_vs_store_count() {
        let mut overlay = OverlayStore::new();
        overlay.mount(path!("data"), TestStore::new("data"));
        overlay.add_redirect(path!("alias"), path!("data"), RedirectMode::ReadWrite, None);

        // route_count includes both stores and redirects
        assert_eq!(overlay.route_count(), 2);
        // store_count only counts stores
        assert_eq!(overlay.store_count(), 1);
    }
}