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
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
use std::{
    borrow::Cow,
    path::{Path, PathBuf},
};

use either::Either::{self, Left, Right};

/// Represents an entry in a [PathPrefixTree]
pub struct Entry<'a, V> {
    pub path: PathBuf,
    pub data: &'a V,
}
impl<'a, V> Entry<'a, V> {
    #[inline(always)]
    pub fn into_path(self) -> PathBuf {
        self.path
    }

    #[inline(always)]
    pub fn into_value(self) -> &'a V {
        self.data
    }
}
impl<'a, V> AsRef<Path> for Entry<'a, V> {
    #[inline(always)]
    fn as_ref(&self) -> &Path {
        self.path.as_path()
    }
}
impl<'a, V> AsRef<V> for Entry<'a, V> {
    #[inline(always)]
    fn as_ref(&self) -> &V {
        self.data
    }
}

#[derive(Debug, thiserror::Error)]
pub enum TryInsertError {
    #[error("unable to insert path due to existing entry with conflicting file type")]
    PathTypeConflict,
    #[error("the path '{}' is unreachable: an ancestor of this path is a file", .0.display())]
    Unreachable(PathBuf),
}

/// A [PathPrefixTree] is a tree data structure optimized for indexing/searching
/// a directory hierarchy in various ways. In particular, it is efficient to ask:
///
/// * If a path is contained in the tree
/// * The nearest ancestor contained in the tree for a given path
///
/// Furthermore, the structure of the tree is very compact, only requiring branching
/// where multiple paths with the same prefix diverge. As a result, the depth of the
/// tree is typically much less than the depth of the corresponding directory hierarchy,
/// and is always bounded by the depth of the directory structure itself.
///
/// This data structure is lexicographically ordered, and can be reified as a flat set
/// of paths, or traversed like a tree.
pub struct PathPrefixTree<V> {
    tree: PrefixTree,
    /// Storage for the data associated with nodes in the tree
    data: Vec<V>,
}
impl<V> Default for PathPrefixTree<V> {
    fn default() -> Self {
        Self {
            tree: PrefixTree::default(),
            data: vec![],
        }
    }
}
impl<V: Clone> Clone for PathPrefixTree<V> {
    fn clone(&self) -> Self {
        let tree = self.tree.clone();
        let data = self.data.clone();
        Self { tree, data }
    }
}
impl<'a, V> FromIterator<&'a str> for PathPrefixTree<V>
where
    V: Default,
{
    fn from_iter<T>(iter: T) -> Self
    where
        T: IntoIterator<Item = &'a str>,
    {
        let mut tree = Self::default();
        for path in iter {
            tree.insert(path, V::default());
        }
        tree
    }
}
impl<'a, V> FromIterator<&'a Path> for PathPrefixTree<V>
where
    V: Default,
{
    fn from_iter<T>(iter: T) -> Self
    where
        T: IntoIterator<Item = &'a Path>,
    {
        let mut tree = Self::default();
        for path in iter {
            tree.insert(path, V::default());
        }
        tree
    }
}
impl<V, P> FromIterator<(P, V)> for PathPrefixTree<V>
where
    P: AsRef<Path>,
{
    fn from_iter<T>(iter: T) -> Self
    where
        T: IntoIterator<Item = (P, V)>,
    {
        let mut tree = Self::default();
        for (path, value) in iter {
            tree.insert(path.as_ref(), value);
        }
        tree
    }
}
impl<V> PathPrefixTree<V> {
    /// Get a new, empty [PathPrefixTree]
    pub fn new() -> Self {
        Self::default()
    }

    /// Insert `path` in this tree with the given value associated with the resulting node.
    ///
    /// If `path` is already contained in this tree (using the definition of containment
    /// as described in `contains`), then this operation will replace the value for that node.
    pub fn insert<P: AsRef<Path>>(&mut self, path: P, value: V) {
        self.try_insert(path, value).expect("insert failed")
    }

    /// Try to insert `path` in this tree with the given value associated with the resulting node.
    ///
    /// If `path` is already contained in this tree (using the definition of containment
    /// as described in `contains`), then this operation will replace the value for that node,
    /// unless the file type for that node is in conflict, in which case an error will be returned.
    pub fn try_insert<P: AsRef<Path>>(&mut self, path: P, value: V) -> Result<(), TryInsertError> {
        let data = DataKey(self.data.len());
        self.data.push(value);
        self.tree.insert(path.as_ref(), data)
    }

    /// Reset this tree to its default, empty state.
    pub fn clear(&mut self) {
        self.tree.clear();
        self.data.clear();
    }

    /// Returns true if `path` has been inserted in this tree.
    pub fn contains<P: AsRef<Path>>(&self, path: P) -> bool {
        self.tree.contains(path.as_ref())
    }

    /// Get a reference to the data associated with `path`, if present in the tree
    pub fn get<P: AsRef<Path>>(&self, path: P) -> Option<&V> {
        let key = self.tree.get(path.as_ref())?;
        Some(&self.data[key.as_usize()])
    }

    /// Get a mutable reference to the data associated with `path`, if present in the tree
    pub fn get_mut<P: AsRef<Path>>(&mut self, path: P) -> Option<&mut V> {
        let key = self.tree.get(path.as_ref())?;
        Some(&mut self.data[key.as_usize()])
    }

    /// Get the nearest entry in the tree which is an ancestor of `path`
    pub fn nearest_ancestor<P: AsRef<Path>>(&self, path: P) -> Option<Entry<'_, V>> {
        self.tree
            .nearest_ancestor(path.as_ref())
            .map(|(path, key)| Entry {
                path,
                data: &self.data[key.as_usize()],
            })
    }

    /// Iterate over all of the entries inserted in this tree.
    pub fn iter(&self) -> impl Iterator<Item = Entry<'_, V>> + '_ {
        Dfs::new(self, /*only_leaves=*/ false, None)
    }

    /// Iterate over all of the entries inserted in this tree,
    /// with a path that refers to a file.
    ///
    /// This is like `iter`, but does not emit entries for directories.
    pub fn files(&self) -> impl Iterator<Item = Entry<'_, V>> + '_ {
        Dfs::new(self, /*only_leaves=*/ true, None)
    }

    /// Iterate over all entries in the tree which are descendants of `path`.
    ///
    /// If `max_depth` is set, the search is restricted to entries with a path
    /// containing no more than `max_depth` additional path components than
    /// `path` itself.
    ///
    /// For example, with a tree containing `/foo/bar`, `/foo/bar/baz` and
    /// `/foo/bar/baz/qux`, calling this function with `/foo/bar` and a max depth
    /// of 2, will only return `/foo/bar/baz`, not `foo/bar/baz/qux`, since
    /// the latter is 2 levels deeper in the hierarchy.
    pub fn children<P: AsRef<Path>>(
        &self,
        path: P,
        max_depth: Option<usize>,
    ) -> impl Iterator<Item = Entry<'_, V>> + '_ {
        Dfs::at(self, path.as_ref(), /*only_leaves=*/ false, max_depth)
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum PathType {
    /// The path does not exist, and we've observed no descendant paths yet
    Unknown,
    /// The path is known to exist, and is not a directory
    File,
    /// The path is either known to exist and is a directory, or was previously
    /// Unknown, but one or more descendant paths have been observed, which
    /// dictates that this path must be a directory.
    Dir,
}

/// This is the type-erased implementation backing [PathPrefixTree]
#[derive(Default, Clone)]
struct PrefixTree {
    /// We permit multiple top-level root nodes in this tree,
    /// making it actually a forest, but you could also think
    /// of this field as representing children of a theoretical
    /// "top" node that is the parent of all possible paths.
    ///
    /// On a Unix system, that would implicitly be `/`, but on
    /// Windows there can be multiple prefixes, e.g. `C:\`, etc.
    roots: Vec<Node>,
}
impl PrefixTree {
    /// Insert `path` into this tree with `data`
    pub fn insert(&mut self, path: &Path, data: DataKey) -> Result<(), TryInsertError> {
        let (path, ty) = canonicalize_and_type(path);

        // Try to find existing root to insert into
        if let Some(sort) = self.try_insert_existing(&path, ty, data)? {
            // Found root with common prefix
            if sort {
                self.roots.sort();
            }
            return Ok(());
        }

        self.insert_new(path.into_owned(), ty, data);

        Ok(())
    }

    fn try_insert_existing(
        &mut self,
        path: &Path,
        ty: PathType,
        data: DataKey,
    ) -> Result<Option<bool>, TryInsertError> {
        for root in self.roots.iter_mut() {
            if root.has_common_prefix(path) {
                // If we modified `root` due to this insertion, require `roots` to be re-sorted
                return insert_into_prefix(root, path, ty, data).map(|depth| Some(depth == 0));
            }
        }

        Ok(None)
    }

    fn insert_new(&mut self, mut path: PathBuf, ty: PathType, data: DataKey) {
        // Create new root node
        let root = match ty {
            PathType::Dir => Node::Branch {
                component: path,
                children: vec![],
                data: Some(data),
            },
            ty @ (PathType::Unknown | PathType::File) => {
                let mut components = path.components();
                let file = PathBuf::from(components.next_back().unwrap().as_os_str());
                let child = Node::Leaf {
                    component: file,
                    ty,
                    data: Some(data),
                };
                path.pop();
                Node::Branch {
                    component: path,
                    children: vec![child],
                    data: None,
                }
            }
        };

        // Insert the new root node, and ensure the roots are ordered
        self.roots.push(root);
        self.roots.sort();
    }

    /// Reset this tree to its default, empty state.
    pub fn clear(&mut self) {
        self.roots.clear();
    }

    pub fn contains(&self, path: &Path) -> bool {
        let (path, _) = canonicalize_and_type(path);
        self.roots.iter().any(|root| root.contains(&path))
    }

    pub fn get(&self, path: &Path) -> Option<DataKey> {
        let (path, _) = canonicalize_and_type(path);
        self.roots.iter().find_map(|root| {
            if root.has_common_prefix(&path) {
                root.get(&path).and_then(|n| n.data())
            } else {
                None
            }
        })
    }

    pub fn nearest_ancestor(&self, path: &Path) -> Option<(PathBuf, DataKey)> {
        let (path, _) = canonicalize_and_type(path);
        self.roots
            .iter()
            .find_map(|root| root.nearest_ancestor(&path))
    }
}

/// This is similar to `Path::canonicalize`, except it does a few things differently:
///
/// * If the given path cannot be canonicalized the "standard" way, we use a fallback
///   method as described in the following points.
/// * In the fallback canonicalization, the path type is always Unknown. If such a path
///   is inserted in the tree, it may become Dir if descendant paths are inserted in the
///   tree.
/// * As a result of the above this does not try to determine if a file path is treated as a
///   directory, e.g. `/foo/bar.txt/qux`. We assume that the user of the [PathPrefixTree] is
///   either validating paths themselves, or is ok with invalid paths.
/// * The fallback canonicalization only partially expands parent references (i.e. `..`). When
///   these occur at the start of the path, they are preserved, not expanded. When they occur
///   following some other type of path component, they cause that component to be dropped, and
///   the `..` is considered resolved (and also dropped).
fn canonicalize_and_type<'a>(path: &'a Path) -> (Cow<'a, Path>, PathType) {
    use smallvec::SmallVec;
    use std::path::Component;

    const PATH_SEPARATOR_SIZE: usize = std::path::MAIN_SEPARATOR_STR.as_bytes().len();

    if let Ok(path) = path.canonicalize() {
        let path: Cow<'a, Path> = Cow::Owned(path);
        let ty = match path.metadata() {
            Err(_) => PathType::Unknown,
            Ok(metadata) => {
                if metadata.is_dir() {
                    PathType::Dir
                } else {
                    PathType::File
                }
            }
        };
        return (path, ty);
    }

    let mut components = SmallVec::<[Component; 4]>::default();
    let mut canonical = true;
    let mut size_in_bytes = 0;
    for component in path.components() {
        match component {
            component @ (Component::Normal(_) | Component::RootDir | Component::Prefix(_)) => {
                size_in_bytes += component.as_os_str().len() + PATH_SEPARATOR_SIZE;
                components.push(component);
            }
            // This only occurs when `.` is the first path component, so drop this
            // component as we automatically assume relative paths are relative to
            // the current working directory
            Component::CurDir => {
                canonical = false;
            }
            // This occurs when `..` is found anywhere in the path. We normalize
            // these by dropping the preceding component when there is a preceding
            // component, and when it is not `..`. If there are no preceding
            // components, then `..` becomes the first component.
            Component::ParentDir => match components.last() {
                None | Some(Component::ParentDir) => {
                    let component = Component::ParentDir;
                    size_in_bytes += component.as_os_str().len() + PATH_SEPARATOR_SIZE;
                    components.push(component);
                }
                Some(_) => {
                    canonical = false;
                    let component = unsafe { components.pop().unwrap_unchecked() };
                    size_in_bytes -= component.as_os_str().len() + PATH_SEPARATOR_SIZE;
                }
            },
        }
    }

    // If `path` is already canonical, return it unmodified
    if canonical {
        return (Cow::Borrowed(path), PathType::Unknown);
    }

    // Otherwise, we need to construct the canonical path
    let mut path = PathBuf::with_capacity(size_in_bytes);
    for component in components.into_iter() {
        path.push(component);
    }
    (Cow::Owned(path), PathType::Unknown)
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
struct DataKey(usize);
impl DataKey {
    #[inline(always)]
    const fn as_usize(self) -> usize {
        self.0
    }
}

#[derive(Debug, PartialEq, Eq)]
pub enum Prefix {
    /// The node path exactly equals the input path
    Exact,
    /// The input path is an ancestor of the node path.
    Ancestor,
    /// The input path is a child of the node path
    Child,
    /// The input path and node path diverge, and the
    /// given path is the common prefix of the two.
    Partial(PathBuf),
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum Node {
    /// A leaf node is always a file
    Leaf {
        component: PathBuf,
        ty: PathType,
        data: Option<DataKey>,
    },
    /// A branch node is always a directory, and may have zero or more children
    Branch {
        component: PathBuf,
        children: Vec<Node>,
        data: Option<DataKey>,
    },
}
impl Ord for Node {
    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
        self.component().cmp(other.component())
    }
}
impl PartialOrd for Node {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
        Some(self.cmp(other))
    }
}
impl Node {
    /// Get the path component for this node
    #[inline]
    pub fn component(&self) -> &Path {
        match self {
            Self::Leaf { ref component, .. } | Self::Branch { ref component, .. } => {
                component.as_path()
            }
        }
    }

    /// Get the data key for this node, if applicable
    #[inline]
    pub fn data(&self) -> Option<DataKey> {
        match self {
            Self::Leaf { ref data, .. } | Self::Branch { ref data, .. } => *data,
        }
    }

    /// Returns the common prefix with `path`.
    ///
    /// If `None`, there is no common prefix.
    ///
    /// If `Some`, see the docs for [PrefixMatch] for details on what the various
    /// match types mean.
    pub fn common_prefix(&self, path: &Path) -> Option<Prefix> {
        assert_ne!(
            path,
            Path::new(""),
            "invalid empty path given to common_prefix"
        );

        let mut ac = self.component().components();
        let mut bc = path.components();
        let mut common_prefix = PathBuf::new();
        loop {
            match (ac.next(), bc.next()) {
                // We've reached the end of the components of `self`, so we've
                // got at least some common prefix with `path`, go ahead and return
                // it without further consideration.
                (None, None) => break Some(Prefix::Exact),
                (None, Some(_)) => break Some(Prefix::Child),
                // The next component in both paths is common, so append it to
                // our common prefix.
                (Some(a), Some(b)) if a == b => {
                    common_prefix.push(a);
                }
                // We diverge from `path` here.
                (Some(_), Some(_)) => {
                    if common_prefix == Path::new("") {
                        break None;
                    } else {
                        break Some(Prefix::Partial(common_prefix));
                    }
                }
                // We've reached the end of the components of `path` before `self`.
                // This indicates that `path` is shallower than `self`, and thus `self`
                // must be split to accomodate `path`.
                (Some(_), None) => {
                    break Some(Prefix::Ancestor);
                }
            }
        }
    }

    /// Returns true if `self` has a common prefix with `path`.
    pub fn has_common_prefix(&self, path: &Path) -> bool {
        assert_ne!(
            path,
            Path::new(""),
            "invalid empty path given to has_common_prefix"
        );

        let mut ac = self.component().components();
        let mut bc = path.components();
        let mut has_minimum_prefix = false;
        loop {
            match (ac.next(), bc.next()) {
                (None, _) => break true,
                (Some(a), Some(b)) if a == b => {
                    has_minimum_prefix = true;
                }
                (Some(_), Some(_)) => break has_minimum_prefix,
                (Some(_), None) => break true,
            }
        }
    }

    /// Returns true if `path` is explicitly represented under this node
    ///
    /// Explicit representation means there is a `Node` in the tree whose full path
    /// is equal to `path`. Implicit representation is only applicable to directories,
    /// and refers to when there is a `Node` in the tree for which `path` is a prefix,
    /// but the full path for that node refers to an item deeper in the directory tree,
    /// e.g. `/foo/bar` is implicitly represented by a node whose path is `/foo/bar/baz.txt`
    pub fn contains(&self, mut path: &Path) -> bool {
        let mut next = Some(self);
        while let Some(node) = next.take() {
            match node {
                Self::Branch {
                    ref component,
                    ref children,
                    ref data,
                } => {
                    if let Ok(rest) = path.strip_prefix(component) {
                        if rest == Path::new("") {
                            return data.is_some();
                        }
                        match children.iter().position(|c| c.has_common_prefix(rest)) {
                            Some(index) => {
                                path = rest;
                                next = Some(&children[index]);
                                continue;
                            }
                            None => {
                                // the remainder of `path` is not covered by any children
                                break;
                            }
                        }
                    } else {
                        // `component` may have a common prefix with `path`, but `path` is
                        // not explicitly represented in the tree, so we must return false
                        break;
                    }
                }
                Self::Leaf {
                    ref component,
                    ref data,
                    ..
                } => {
                    // `component` may have a common prefix with `path`, but unless `path`
                    // is equal to `component`, it is not explicitly represented in the tree
                    return path == component && data.is_some();
                }
            }
        }

        false
    }

    pub fn get<'a>(&'a self, mut path: &Path) -> Option<&'a Node> {
        let mut next = Some(self);
        while let Some(node) = next.take() {
            match node {
                Self::Branch {
                    ref component,
                    ref children,
                    ..
                } => {
                    if let Ok(rest) = path.strip_prefix(component) {
                        if rest == Path::new("") {
                            return Some(node);
                        }
                        match children.iter().position(|c| c.has_common_prefix(rest)) {
                            Some(index) => {
                                path = rest;
                                next = Some(&children[index]);
                                continue;
                            }
                            None => {
                                break;
                            }
                        }
                    }
                }
                Self::Leaf { ref component, .. } => {
                    if path == component {
                        return Some(node);
                    }
                    break;
                }
            }
        }

        None
    }

    /// Return the path and data key associated with it, which is the nearest
    /// ancestor of `path`, with data, represented under this node.
    ///
    /// For example, consider a tree in which `/foo/bar/baz/qux.txt` and
    /// `/foo/bar/qux2.txt` are both inserted:
    ///
    /// * Given `/foo/bar/baz/other.txt` this function will select `/foo/bar/qux2.txt`,
    /// because `/foo/bar/baz/qux.txt` is a sibling, but not an ancestor.
    /// * Given `/foo/bar/qux2.txt`, it will select None, because there is no ancestor
    /// explicitly represented in the tree for this path
    /// * Given `/foo/bar/baz/qux.txt`, it will select `/foo/bar/qux2.txt`, because
    /// `/foo/bar/baz/qux.txt` is not an ancestor of itself, and its nearest ancestor
    /// is the selected path.
    pub fn nearest_ancestor(&self, mut path: &Path) -> Option<(PathBuf, DataKey)> {
        use smallvec::SmallVec;
        if !self.has_common_prefix(path) {
            return None;
        }

        let mut candidates = SmallVec::<[(PathBuf, DataKey); 2]>::default();
        let mut ancestor = PathBuf::new();
        let mut next = Some(self);
        while let Some(node) = next.take() {
            match node {
                Self::Branch {
                    ref component,
                    ref children,
                    data,
                } => {
                    if let Ok(rest) = path.strip_prefix(component) {
                        // We found `path`, so take the most recent candidate,
                        // if one is available. If not, then there are no nearest
                        // ancestors for `path`
                        if rest == Path::new("") {
                            return candidates.pop();
                        }

                        // Otherwise, find the next child node to descend into.
                        // If a suitable candidate is found, and the current node
                        // has data associated with it, push the current path as
                        // a candidate node, and check the child node to see if it
                        // is closer.
                        //
                        // If no suitable candidate is found, but the current node
                        // has data associated with it, then the current node is the
                        // nearest ancestor, so we can just return it. If it has no
                        // data, then the most recent candidate is returned, if available.
                        ancestor.push(component);
                        let child = children.iter().position(|c| c.has_common_prefix(rest));
                        if let Some(index) = child {
                            if let Some(data) = *data {
                                candidates.push((ancestor.clone(), data));
                            }

                            path = rest;
                            next = Some(&children[index]);
                            continue;
                        } else {
                            return data
                                .map(|data| (ancestor, data))
                                .or_else(|| candidates.pop());
                        }
                    } else {
                        // To insert `path` in the tree, we'd have to split `node`, which
                        // means the most recent candidate is the nearest ancestor, if we
                        // have one
                        return candidates.pop();
                    }
                }
                // Leaf nodes by definition cannot be the nearest ancestor, so use
                // the most recent candidate, if we have one
                Self::Leaf { .. } => return candidates.pop(),
            }
        }

        None
    }

    fn take(&mut self, prefix: &Path) -> Self {
        match self {
            Node::Branch {
                ref component,
                ref mut children,
                ref mut data,
            } => {
                let component = component.strip_prefix(prefix).unwrap().to_path_buf();
                Node::Branch {
                    component,
                    children: core::mem::take(children),
                    data: data.take(),
                }
            }
            Node::Leaf {
                ref component,
                ref mut data,
                ty,
            } => {
                let component = component.strip_prefix(prefix).unwrap().to_path_buf();
                Node::Leaf {
                    component,
                    ty: *ty,
                    data: data.take(),
                }
            }
        }
    }

    fn split_at(&mut self, common_prefix: PathBuf) {
        let split = self.take(&common_prefix);
        *self = Node::Branch {
            component: common_prefix,
            children: vec![split],
            data: None,
        };
    }

    fn set_type(&mut self, ty: PathType) -> Result<(), TryInsertError> {
        match self {
            Self::Leaf {
                ref mut component,
                ref mut data,
                ty: ref mut prev_ty,
                ..
            } => match ty {
                PathType::Unknown | PathType::File => *prev_ty = ty,
                PathType::Dir => {
                    let component = core::mem::replace(component, PathBuf::new());
                    let data = data.take();
                    *self = Self::Branch {
                        component,
                        data,
                        children: vec![],
                    };
                }
            },
            Self::Branch { .. } => match ty {
                PathType::Dir => (),
                PathType::Unknown | PathType::File => return Err(TryInsertError::PathTypeConflict),
            },
        }

        Ok(())
    }

    fn set_data(&mut self, data: DataKey) {
        match self {
            Self::Branch {
                data: ref mut prev_data,
                ..
            }
            | Self::Leaf {
                data: ref mut prev_data,
                ..
            } => {
                *prev_data = Some(data);
            }
        }
    }

    fn push_child(
        &mut self,
        component: PathBuf,
        ty: PathType,
        data: Option<DataKey>,
    ) -> Result<(), TryInsertError> {
        let child = match ty {
            PathType::File | PathType::Unknown => Self::Leaf {
                component,
                ty,
                data,
            },
            PathType::Dir => Self::Branch {
                component,
                children: vec![],
                data,
            },
        };
        match self {
            Self::Branch {
                ref mut children, ..
            } => {
                children.push(child);
                children.sort();
            }
            Self::Leaf {
                component: ref mut parent_component,
                data: ref mut parent_data,
                ty: PathType::Unknown,
            } => {
                let children = vec![child];
                let component = core::mem::replace(parent_component, PathBuf::new());
                let data = parent_data.take();
                *self = Self::Branch {
                    component,
                    children,
                    data,
                };
            }
            Self::Leaf { .. } => return Err(TryInsertError::PathTypeConflict),
        }

        Ok(())
    }

    fn try_insert_new<'a>(
        &'a mut self,
        path: &Path,
        component: &Path,
        ty: PathType,
        data: Option<DataKey>,
    ) -> Either<Result<(), TryInsertError>, &'a mut Node> {
        match self {
            Self::Branch {
                ref mut children, ..
            } => {
                if let Some(index) = children.iter().position(|c| c.has_common_prefix(component)) {
                    // We can't insert this as new, but the given index is a child we can try to insert into next
                    return Right(&mut children[index]);
                }
                let child = match ty {
                    PathType::File | PathType::Unknown => Self::Leaf {
                        component: component.to_path_buf(),
                        ty,
                        data,
                    },
                    PathType::Dir => Self::Branch {
                        component: component.to_path_buf(),
                        children: vec![],
                        data,
                    },
                };
                children.push(child);
                children.sort();

                Left(Ok(()))
            }
            Self::Leaf {
                ty: PathType::File, ..
            } => Left(Err(TryInsertError::Unreachable(path.to_path_buf()))),
            Self::Leaf {
                component: ref mut parent_component,
                data: ref mut parent_data,
                ..
            } => {
                let child = match ty {
                    PathType::File | PathType::Unknown => Self::Leaf {
                        component: component.to_path_buf(),
                        ty,
                        data,
                    },
                    PathType::Dir => Self::Branch {
                        component: component.to_path_buf(),
                        children: vec![],
                        data,
                    },
                };
                let children = vec![child];
                let component = core::mem::replace(parent_component, PathBuf::new());
                let data = parent_data.take();
                *self = Self::Branch {
                    component,
                    children,
                    data,
                };
                Left(Ok(()))
            }
        }
    }
}

/// Insert `path` into `node` which contains a prefix of `path`.
///
/// Returns the depth relative to `node` where `path` is inserted, a depth
/// of zero indicates that `node` itself was modified.
#[inline(never)]
fn insert_into_prefix(
    node: &mut Node,
    mut path: &Path,
    ty: PathType,
    data: DataKey,
) -> Result<usize, TryInsertError> {
    let orig_path = path;
    let mut next = Some((node, 0));

    loop {
        let (node, depth) = next.take().unwrap();

        if let Some(prefix) = node.common_prefix(path) {
            match prefix {
                Prefix::Exact => {
                    node.set_type(ty)?;
                    node.set_data(data);
                    break Ok(depth);
                }
                Prefix::Child => {
                    path = path.strip_prefix(node.component()).unwrap();
                    match node.try_insert_new(orig_path, path, ty, Some(data)) {
                        Left(result) => {
                            result?;
                            break Ok(depth + 1);
                        }
                        Right(next_child) => {
                            next = Some((next_child, depth + 1));
                            continue;
                        }
                    }
                }
                Prefix::Ancestor => {
                    node.split_at(path.to_path_buf());
                    node.set_data(data);
                    break Ok(depth);
                }
                Prefix::Partial(common_prefix) => {
                    let component = path.strip_prefix(&common_prefix).unwrap().to_path_buf();
                    node.split_at(common_prefix);
                    node.push_child(component, ty, Some(data))?;
                    break Ok(depth);
                }
            }
        }
    }
}

#[derive(Debug)]
struct DfsVisitor<'a> {
    worklist: &'a [Node],
    prefix: PathBuf,
    component_prefix: PathBuf,
    queued: Option<(PathBuf, usize, &'a [Node])>,
    depth: usize,
    only_leaves: bool,
}
impl<'a> DfsVisitor<'a> {
    fn new(prefix: PathBuf, worklist: &'a [Node], depth: usize, only_leaves: bool) -> Self {
        Self {
            worklist,
            prefix,
            component_prefix: PathBuf::new(),
            queued: None,
            depth,
            only_leaves,
        }
    }

    pub fn next(
        &mut self,
        max_depth: Option<usize>,
    ) -> Option<Either<(PathBuf, DataKey), DfsVisitor<'a>>> {
        if let Some((prefix, relative_depth, worklist)) = self.queued.take() {
            return Some(Right(DfsVisitor::new(
                prefix,
                worklist,
                relative_depth,
                self.only_leaves,
            )));
        }

        loop {
            match self.worklist.split_first()? {
                (Node::Leaf { data: None, .. }, worklist) => {
                    self.worklist = worklist;
                    continue;
                }
                (
                    Node::Leaf {
                        ref component,
                        data: Some(data),
                        ..
                    },
                    worklist,
                ) => {
                    self.worklist = worklist;

                    if self.exceeds_max_depth(component, max_depth) {
                        continue;
                    }
                    let suffix = self.strip_prefix(component);
                    let prefix = self.prefix.join(suffix);
                    break Some(Left((prefix, *data)));
                }
                (
                    Node::Branch {
                        ref component,
                        ref children,
                        data,
                    },
                    worklist,
                ) => {
                    self.worklist = worklist;

                    let relative_depth = self.relative_depth(component, max_depth);
                    if let Some(max_depth) = max_depth {
                        if relative_depth > max_depth {
                            continue;
                        }
                    }
                    let suffix = self.strip_prefix(component);
                    let prefix = self.prefix.join(suffix);
                    if !children.is_empty() {
                        match data {
                            // We have to emit the branch node first, so save the descent
                            // information for the next time `next` is called
                            Some(data) => {
                                self.queued =
                                    Some((prefix.clone(), relative_depth, children.as_slice()));
                                break Some(Left((prefix, *data)));
                            }
                            // We don't need to emit the branch node, so return a new visitor
                            // to start descending into the children
                            None => {
                                break Some(Right(DfsVisitor::new(
                                    prefix,
                                    children.as_slice(),
                                    relative_depth,
                                    self.only_leaves,
                                )));
                            }
                        }
                    }
                }
            }
        }
    }

    fn exceeds_max_depth(&self, path: &Path, max_depth: Option<usize>) -> bool {
        match max_depth {
            None => false,
            Some(max_depth) => {
                let suffix = self.strip_prefix(path);
                let relative_depth = self.depth + suffix.components().count();
                relative_depth > max_depth
            }
        }
    }

    fn relative_depth(&self, path: &Path, max_depth: Option<usize>) -> usize {
        match max_depth {
            // If we don't care about the depth, do nothing
            None => 0,
            Some(_) => {
                let suffix = self.strip_prefix(path);
                self.depth + suffix.components().count()
            }
        }
    }

    #[inline]
    fn strip_prefix<'p>(&self, path: &'p Path) -> &'p Path {
        if self.component_prefix == Path::new("") {
            return path;
        }
        path.strip_prefix(&self.component_prefix).unwrap()
    }
}

struct Dfs<'a, V> {
    data: &'a [V],
    stack: Vec<DfsVisitor<'a>>,
    current: DfsVisitor<'a>,
    max_depth: Option<usize>,
}
impl<V> core::fmt::Debug for Dfs<'_, V> {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        f.debug_struct("Dfs")
            .field("stack", &self.stack)
            .field("current", &self.current)
            .field("max_depth", &self.max_depth)
            .finish()
    }
}
impl<'a, V> Dfs<'a, V> {
    fn new(tree: &'a PathPrefixTree<V>, only_leaves: bool, max_depth: Option<usize>) -> Self {
        Self {
            data: tree.data.as_slice(),
            stack: vec![],
            current: DfsVisitor::new(PathBuf::new(), tree.tree.roots.as_slice(), 0, only_leaves),
            max_depth,
        }
    }

    fn at(
        tree: &'a PathPrefixTree<V>,
        path: &Path,
        only_leaves: bool,
        max_depth: Option<usize>,
    ) -> Self {
        // Traverse tree until we find our initial working set
        match tree
            .tree
            .roots
            .iter()
            .find(|root| root.has_common_prefix(path))
        {
            None => return Self::empty(tree, only_leaves, max_depth),
            Some(root) => {
                let mut next = Some(root);
                let mut input_path = path;
                let mut prefix = PathBuf::new();
                loop {
                    let node = next.take().unwrap();
                    match node.common_prefix(input_path) {
                        // No children for `path` in this tree
                        None => break Self::empty(tree, only_leaves, max_depth),
                        Some(Prefix::Exact) => {
                            break Self {
                                data: tree.data.as_slice(),
                                stack: vec![],
                                current: DfsVisitor::new(
                                    prefix,
                                    core::slice::from_ref(node),
                                    0,
                                    only_leaves,
                                ),
                                max_depth,
                            };
                        }
                        // This node is the first child, as long as it is not deeper than `max_depth`
                        Some(Prefix::Ancestor) => {
                            prefix.push(input_path);
                            break Self {
                                data: tree.data.as_slice(),
                                stack: vec![],
                                current: DfsVisitor {
                                    component_prefix: PathBuf::from(input_path),
                                    ..DfsVisitor::new(
                                        prefix,
                                        core::slice::from_ref(node),
                                        0,
                                        only_leaves,
                                    )
                                },
                                max_depth,
                            };
                        }
                        // We need to recurse deeper into the tree to find potential children
                        Some(Prefix::Child) => {
                            match node {
                                Node::Branch {
                                    ref component,
                                    ref children,
                                    ..
                                } => {
                                    input_path = input_path.strip_prefix(component).unwrap();
                                    next =
                                        children.iter().find(|c| c.has_common_prefix(input_path));
                                    if next.is_none() {
                                        break Self::empty(tree, only_leaves, max_depth);
                                    }
                                    prefix.push(component);
                                }
                                // There are no children for `path`, so return an empty iterator
                                Node::Leaf { .. } => {
                                    break Self::empty(tree, only_leaves, max_depth)
                                }
                            }
                        }
                        // There can be no children of `path` under `node`
                        Some(Prefix::Partial(_)) => {
                            break Self::empty(tree, only_leaves, max_depth)
                        }
                    }
                }
            }
        }
    }

    #[inline]
    fn empty(tree: &'a PathPrefixTree<V>, only_leaves: bool, max_depth: Option<usize>) -> Self {
        Self {
            data: tree.data.as_slice(),
            stack: vec![],
            current: DfsVisitor::new(PathBuf::new(), &[], 0, only_leaves),
            max_depth,
        }
    }
}
impl<'a, V> Iterator for Dfs<'a, V> {
    type Item = Entry<'a, V>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            match self.current.next(self.max_depth) {
                // No more nodes at this depth
                None => {
                    // Try to resume iteration at the next level up, else we're done
                    self.current = self.stack.pop()?;
                }
                // Visitor produced the next element at this depth
                Some(Left((path, key))) => {
                    return Some(Entry {
                        path,
                        data: &self.data[key.as_usize()],
                    });
                }
                // Visitor has indicated we should suspend iteration at this
                // depth and descend into a child node first
                Some(Right(visitor)) => {
                    let suspended = core::mem::replace(&mut self.current, visitor);
                    self.stack.push(suspended);
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use pretty_assertions::assert_eq;

    use super::*;

    #[test]
    fn path_tree_insert() {
        // Tests:
        // * insertion in a clean tree
        // * insertion under an existing node
        // * insertion in a dirty tree
        let tree = PathPrefixTree::<()>::from_iter(["/foo/bar", "/foo/bar/baz", "/qux"]);

        let child = Node::Leaf {
            component: PathBuf::from("baz"),
            ty: PathType::Unknown,
            data: Some(DataKey(1)),
        };
        let a = Node::Branch {
            component: PathBuf::from("foo"),
            children: vec![Node::Branch {
                component: PathBuf::from("bar"),
                children: vec![child],
                data: Some(DataKey(0)),
            }],
            data: None,
        };
        let b = Node::Leaf {
            component: PathBuf::from("qux"),
            ty: PathType::Unknown,
            data: Some(DataKey(2)),
        };
        let root = Node::Branch {
            component: PathBuf::from("/"),
            children: vec![a, b],
            data: None,
        };

        assert_eq!(tree.tree.roots.as_slice(), &[root]);
    }

    #[test]
    fn path_tree_nearest_ancestor() {
        let tree = PathPrefixTree::<()>::from_iter(["/foo/bar", "/foo/bar/baz", "/qux"]);

        assert_eq!(
            tree.nearest_ancestor("/foo/bar/baz").map(Entry::into_path),
            Some(PathBuf::from("/foo/bar"))
        );
        assert_eq!(
            tree.nearest_ancestor("/foo/bar").map(Entry::into_path),
            None
        );
        assert_eq!(tree.nearest_ancestor("/qux").map(Entry::into_path), None);
    }

    #[test]
    fn path_tree_contains() {
        let tree = PathPrefixTree::<()>::from_iter(["/foo/bar", "/foo/bar/baz", "/qux"]);

        assert!(tree.contains("/foo/bar/baz"));
        assert!(tree.contains("/foo/bar"));
        assert!(!tree.contains("/foo"));
        assert!(!tree.contains("/foo/bar/baz/thing.txt"));
    }

    #[test]
    fn path_tree_get() {
        let mut tree = PathPrefixTree::default();
        tree.insert("/foo/bar/baz", 1usize);
        tree.insert("/foo/bar/baz/qux", 2usize);

        assert_eq!(tree.get("/foo/bar/baz/qux").copied(), Some(2));
        assert_eq!(tree.get("/foo/bar/baz").copied(), Some(1));
    }

    #[test]
    fn path_tree_iter() {
        let tree = PathPrefixTree::<()>::from_iter(["/qux", "/foo/bar/baz", "/foo/bar"]);

        let paths = tree.iter().map(|e| e.into_path()).collect::<Vec<_>>();

        let expected = vec![
            PathBuf::from("/foo/bar"),
            PathBuf::from("/foo/bar/baz"),
            PathBuf::from("/qux"),
        ];

        assert_eq!(paths, expected);
    }

    #[test]
    fn path_tree_children() {
        let tree = PathPrefixTree::<()>::from_iter(["/qux", "/foo/bar/baz", "/foo/bar"]);

        let paths = tree
            .children("/foo/bar", None)
            .map(|e| e.into_path())
            .collect::<Vec<_>>();

        let expected = vec![PathBuf::from("/foo/bar"), PathBuf::from("/foo/bar/baz")];

        assert_eq!(paths, expected);

        let paths = tree
            .children("/foo", None)
            .map(|e| e.into_path())
            .collect::<Vec<_>>();

        assert_eq!(paths, expected);
    }
}