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
//! Implementation of [`FsTree`].

use std::{
    collections::BTreeMap,
    ffi::OsStr,
    io, mem,
    ops::Index,
    path::{Path, PathBuf},
};

use file_type_enum::FileType;

use crate::{
    fs,
    iter::{Iter, NodesIter, PathsIter},
    utils, Error, Result,
};

/// The children [Trie](https://en.wikipedia.org/wiki/Trie) type alias.
pub type TrieMap = BTreeMap<PathBuf, FsTree>;

/// A filesystem tree recursive type.
///
/// # Iterators:
///
/// See the [iterator module documentation](crate::iter).
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum FsTree {
    /// A regular file.
    Regular,
    /// A directory, might have children `FsTree`s inside.
    Directory(TrieMap),
    /// Symbolic link, and it's target path (the link might be broken).
    Symlink(PathBuf),
}

impl FsTree {
    /// Creates an empty directory node.
    ///
    /// This is an alias to `FsTree::Directory(Default::default())`.
    ///
    /// ```
    /// use fs_tree::{FsTree, TrieMap};
    ///
    /// let result = FsTree::new_dir();
    /// let expected = FsTree::Directory(TrieMap::new());
    ///
    /// assert_eq!(result, expected);
    /// ```
    pub fn new_dir() -> Self {
        Self::Directory(TrieMap::new())
    }

    /// Calculate the length by counting the leafs.
    pub fn len_leafs(&self) -> usize {
        if let Some(children) = self.children() {
            children.values().map(Self::len_leafs).sum::<usize>()
        } else if self.is_leaf() {
            1
        } else {
            0
        }
    }

    /// Calculate the length by counting all tree nodes, including the root.
    pub fn len_all(&self) -> usize {
        if let Some(children) = self.children() {
            children.values().map(Self::len_leafs).sum::<usize>()
        } else {
            1
        }
    }

    /// Construct a `FsTree` by reading from `path`, follows symlinks.
    ///
    /// If you want symlink-awareness, check [`symlink_read_at`].
    ///
    /// # Errors:
    ///
    /// - If any IO error occurs.
    /// - If any file has an unexpected file type.
    ///
    /// [`symlink_read_at`]: FsTree::read_at
    pub fn read_at(path: impl AsRef<Path>) -> Result<Self> {
        Self::__read_at(path.as_ref(), true)
    }

    /// Construct a `FsTree` by reading from `path`.
    ///
    /// If you don't want symlink-awareness, check [`read_at`].
    ///
    /// # Errors:
    ///
    /// - If any IO error occurs.
    /// - If any file has an unexpected file type.
    ///
    /// [`read_at`]: FsTree::symlink_read_at
    pub fn symlink_read_at(path: impl AsRef<Path>) -> Result<Self> {
        Self::__read_at(path.as_ref(), false)
    }

    fn __read_at(path: &Path, follow_symlinks: bool) -> Result<Self> {
        let get_file_type = if follow_symlinks {
            FileType::read_at
        } else {
            FileType::symlink_read_at
        };

        match get_file_type(path)? {
            FileType::Regular => Ok(Self::Regular),
            FileType::Directory => {
                let mut children = TrieMap::new();

                for entry in fs::read_dir(path)? {
                    let entry = entry?;
                    let entry_path = entry.path();

                    let node = Self::__read_at(&entry_path, follow_symlinks)?;

                    let stripped_file_path = entry_path
                        .strip_prefix(path)
                        .expect("Failed to strip prefix, expected to always succeed in Linux");

                    children.insert(stripped_file_path.into(), node);
                }

                Ok(Self::Directory(children))
            },
            FileType::Symlink => {
                let target_path = utils::follow_symlink(path)?;
                Ok(Self::Symlink(target_path))
            },
            other_type => {
                Err(Error::UnexpectedFileTypeError(
                    other_type,
                    path.to_path_buf(),
                ))
            },
        }
    }

    /// Construct a structural copy of this `FsTree` by reading files at the given path.
    ///
    /// In other words, the returned tree is formed of all paths in `self` that are also found in
    /// the given `path` (intersection), missing files are skipped and types might differ.
    ///
    /// This function can be useful if you need to load a subtree from a huge folder and cannot
    /// afford to load the whole folder, or if you just want to filter out every node outside of the
    /// specified structure.
    ///
    /// This function will make at maximum `self.len()` syscalls.
    ///
    /// If you don't want symlink-awareness, check [`FsTree::symlink_read_copy_at`].
    ///
    /// # Examples:
    ///
    /// ```no_run
    /// use fs_tree::FsTree;
    ///
    /// fn dynamically_load_structure() -> FsTree {
    /// #    "
    ///     ...
    /// #    ";
    /// #   todo!();
    /// }
    ///
    /// let structure = dynamically_load_structure();
    ///
    /// let new_tree = structure.read_copy_at("path_here").unwrap();
    ///
    /// // It is guaranteed that every path in here is present in `structure`
    /// for path in new_tree.paths() {
    ///     assert!(structure.get(path).is_some());
    /// }
    /// ```
    ///
    /// # Errors:
    ///
    /// - If an IO error happens, except [`io::ErrorKind::NotFound`]
    ///
    /// [`io::ErrorKind::NotFound`]: std::io::ErrorKind::NotFound
    pub fn read_copy_at(&self, path: impl AsRef<Path>) -> Result<Self> {
        self.__read_copy_at(path.as_ref(), true)
    }

    /// Construct a structural copy of this `FsTree` by reading files at the given path.
    ///
    /// In other words, the returned tree is formed of all paths in `self` that are also found in
    /// the given `path` (intersection), missing files are skipped and types might differ.
    ///
    /// This function can be useful if you need to load a subtree from a huge folder and cannot
    /// afford to load the whole folder, or if you just want to filter out every node outside of the
    /// specified structure.
    ///
    /// This function will make at maximum `self.len()` syscalls.
    ///
    /// If you don't want symlink-awareness, check [`FsTree::read_copy_at`].
    ///
    /// # Examples:
    ///
    /// ```no_run
    /// use fs_tree::FsTree;
    ///
    /// fn dynamically_load_structure() -> FsTree {
    /// #    "
    ///     ...
    /// #    ";
    /// #   todo!();
    /// }
    ///
    /// let structure = dynamically_load_structure();
    ///
    /// let new_tree = structure.symlink_read_copy_at("path_here").unwrap();
    ///
    /// // It is guaranteed that every path in here is present in `structure`
    /// for path in new_tree.paths() {
    ///     assert!(structure.get(path).is_some());
    /// }
    /// ```
    ///
    /// # Errors:
    ///
    /// - If an IO error happens, except [`io::ErrorKind::NotFound`]
    ///
    /// [`io::ErrorKind::NotFound`]: std::io::ErrorKind::NotFound
    pub fn symlink_read_copy_at(&self, path: impl AsRef<Path>) -> Result<Self> {
        self.__read_copy_at(path.as_ref(), false)
    }

    // TODO: There are easy optimizations to be done in here
    fn __read_copy_at(&self, folder: &Path, follow_symlinks: bool) -> Result<Self> {
        let mut new_tree = FsTree::new_dir();

        for relative_path in self.paths() {
            let path = folder.join(&relative_path);

            let get_file_type = if follow_symlinks {
                FileType::read_at
            } else {
                FileType::symlink_read_at
            };

            let file_type = match get_file_type(&path) {
                Ok(file_type) => file_type,
                Err(err) if err.kind() == io::ErrorKind::NotFound => continue,
                Err(err) => return Err(err.into()),
            };

            let node = match file_type {
                FileType::Regular => Self::Regular,
                FileType::Directory => Self::new_dir(),
                FileType::Symlink => {
                    let target_path = utils::follow_symlink(&path)?;
                    Self::Symlink(target_path)
                },
                _ => continue,
            };

            new_tree.insert(relative_path, node);
        }

        Ok(new_tree)
    }

    /// Construct a `FsTree` from path pieces.
    ///
    /// Returns `None` if the input is empty.
    ///
    /// Returned value can correspond to a regular file or directory, but not a symlink.
    ///
    /// # Warning
    ///
    /// The last piece is always a file, so inputs ending with `/`, like `Path::new("example/")` are
    /// **NOT** parsed as directories.
    ///
    /// This might change in the future, for my personal usage cases (author writing), this was
    /// always OK, but if you'd like this to change, open an issue 👍.
    ///
    /// # Examples:
    ///
    /// ```
    /// use fs_tree::{FsTree, tree};
    ///
    /// let result = FsTree::from_path_text("a/b/c");
    ///
    /// let expected = tree! {
    ///     a: {
    ///         b: {
    ///             c
    ///         }
    ///     }
    /// };
    ///
    /// // The expected tree
    /// assert_eq!(result, expected);
    ///
    /// // Nodes are nested
    /// assert!(result.is_dir());
    /// assert!(result["a"].is_dir());
    /// assert!(result["a"]["b"].is_dir());
    /// assert!(result["a"]["b"]["c"].is_regular());
    /// ```
    pub fn from_path_text(path: impl AsRef<Path>) -> Self {
        Self::from_path_pieces(path.as_ref().iter())
    }

    /// Generic iterator version of [`from_path_text`](FsTree::from_path_text).
    pub fn from_path_pieces<I, P>(path_iter: I) -> Self
    where
        I: IntoIterator<Item = P>,
        P: Into<PathBuf>,
    {
        let mut path_iter = path_iter.into_iter();

        if let Some(popped_piece) = path_iter.next() {
            let child = (popped_piece.into(), Self::from_path_pieces(path_iter));
            Self::Directory(TrieMap::from([child]))
        } else {
            Self::Regular
        }
    }

    /// Creates an iterator that yields `(&FsTree, PathBuf)`.
    ///
    /// See iterator docs at the [`iter` module documentation](crate::iter).
    pub fn iter(&self) -> Iter {
        Iter::new(self)
    }

    /// Creates an iterator that yields `&FsTree`.
    ///
    /// See iterator docs at the [`iter` module documentation](crate::iter).
    pub fn nodes(&self) -> NodesIter {
        NodesIter::new(self)
    }

    /// Creates an iterator that yields `PathBuf`.
    ///
    /// See iterator docs at the [`iter` module documentation](crate::iter).
    pub fn paths(&self) -> PathsIter {
        PathsIter::new(self)
    }

    /// Returns `true` if `self` type matches `other` type.
    pub fn is_same_type_as(&self, other: &Self) -> bool {
        mem::discriminant(self) == mem::discriminant(other)
    }

    /// Returns `Ok(true)` if all nodes exist in the filesystem.
    ///
    /// # Errors:
    ///
    /// Similar to how [`Path::try_exists`] works, this function returns `false` if any IO error
    /// occurred when checking [`std::fs::symlink_metadata`] (except [`io::ErrorKind::NotFound`]).
    pub fn try_exists(&mut self) -> io::Result<bool> {
        for path in self.paths() {
            match fs::symlink_metadata(path) {
                Ok(_) => continue,
                Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(false),
                Err(error) => return Err(error),
            }
        }

        Ok(true)
    }

    /// Merge two trees.
    ///
    /// # Errors:
    ///
    /// - Returns `None` if contents of both trees conflict.
    pub fn try_merge(mut self, other: Self) -> Option<Self> {
        use FsTree::{Directory, Regular, Symlink};

        // If types match, check if their contents match, otherwise, return `None`
        match (&mut self, other) {
            (Regular, Regular) => Some(self),
            (Symlink(left_target), Symlink(right_target)) => {
                (*left_target == right_target).then_some(self)
            },
            (Directory(left_children), Directory(right_children)) => {
                // Just to clarify, we're merging the right onto the left
                let left_children: &mut TrieMap = left_children;
                let right_children: TrieMap = right_children;

                for (path, right_node) in right_children {
                    if let Some(left_node) = left_children.remove(&path) {
                        let new_node = left_node.try_merge(right_node)?;
                        left_children.insert(path, new_node);
                    } else {
                        left_children.insert(path, right_node);
                    }
                }

                Some(self)
            },
            // Types mismatch, not possible to merge
            (_, _) => None,
        }
    }

    /// Reference to children vec if self.is_directory().
    pub fn children(&self) -> Option<&TrieMap> {
        match &self {
            Self::Directory(children) => Some(children),
            _ => None,
        }
    }

    /// Reference to children vec if self.is_directory(), mutable.
    pub fn children_mut(&mut self) -> Option<&mut TrieMap> {
        match self {
            Self::Directory(children) => Some(children),
            _ => None,
        }
    }

    /// Reference to target path, if self is a symlink.
    pub fn target(&self) -> Option<&Path> {
        match &self {
            Self::Symlink(target_path) => Some(target_path),
            _ => None,
        }
    }

    /// Reference to target path, if self is a symlink, mutable.
    pub fn target_mut(&mut self) -> Option<&mut PathBuf> {
        match self {
            Self::Symlink(target_path) => Some(target_path),
            _ => None,
        }
    }

    // /// Apply a closure for each direct child of this FsTree.
    // ///
    // /// Only 1 level deep.
    // pub fn apply_to_children0(&mut self, f: impl FnMut(&mut Self)) {
    //     if let Some(children) = self.children_mut() {
    //         children.iter_mut().for_each(f);
    //     }
    // }

    // /// Apply a closure to all direct and indirect descendants inside of this structure.
    // ///
    // /// Calls recursively for all levels.
    // pub fn apply_to_all_children1(&mut self, f: impl FnMut(&mut Self) + Copy) {
    //     if let Some(children) = self.children_mut() {
    //         children
    //             .iter_mut()
    //             .for_each(|x| x.apply_to_all_children1(f));
    //         children.iter_mut().for_each(f);
    //     }
    // }

    // /// Apply a closure to all direct and indirect descendants inside, also includes root.
    // ///
    // /// Calls recursively for all levels.
    // pub fn apply_to_all(&mut self, mut f: impl FnMut(&mut Self) + Copy) {
    //     f(self);
    //     if let Some(children) = self.children_mut() {
    //         for child in children.iter_mut() {
    //             child.apply_to_all(f);
    //         }
    //     }
    // }

    /// Returns `true` if `self` is a leaf node.
    pub fn is_leaf(&self) -> bool {
        match self {
            Self::Regular | Self::Symlink(_) => true,
            Self::Directory(children) => children.is_empty(),
        }
    }

    /// The variant string.
    pub fn variant_str(&self) -> &'static str {
        match self {
            Self::Regular => "regular file",
            Self::Directory(_) => "directory",
            Self::Symlink(_) => "symlink",
        }
    }

    /// Returns `true` if self matches the regular variant.
    pub fn is_regular(&self) -> bool {
        matches!(self, Self::Regular)
    }

    /// Returns `true` if self matches the directory variant.
    pub fn is_dir(&self) -> bool {
        matches!(self, Self::Directory(_))
    }

    /// Returns `true` if self matches the symlink variant.
    pub fn is_symlink(&self) -> bool {
        matches!(self, Self::Symlink(_))
    }

    // /// Generate a diff from two different trees.
    // pub fn diff(&self, other: &Self) {
    //     if !self.has_same_type_as(other) {
    //         println!("Types differ! ");
    //     }

    //     let (self_children, other_children) = match (&self.file_type, &other.file_type) {
    //         (Self::Directory(self_children), Self::Directory(other_children)) => {
    //             (self_children, other_children)
    //         },
    //         _ => panic!(),
    //     };

    //     let mut lookup = self_children
    //         .iter()
    //         .map(|x| (&x.path, x))
    //         .collect::<HashMap<&PathBuf, &FsTree>>();

    //     for other_child in other_children {
    //         if let Some(self_child) = lookup.remove(&other_child.path) {
    //             if self_child.has_same_type_as(other_child) {
    //                 if self_child.is_dir() {
    //                     self_child.diff(other_child);
    //                 }
    //             } else {
    //                 println!(
    //                     "File {:?} is a {} while file {:?} is a {}",
    //                     self_child.path,
    //                     self_child.file_type.file_type_display(),
    //                     other_child.path,
    //                     other_child.file_type.file_type_display(),
    //                 );
    //             }
    //         } else {
    //             let path = &other_child.path;
    //             println!(
    //                 "2Only in {:?}: {:?}",
    //                 path.parent().unwrap(),
    //                 path.file_name().unwrap()
    //             );
    //         }
    //     }

    //     for child_left in lookup.values() {
    //         let path = &child_left.path;
    //         println!(
    //             "1Only in {:?}: {:?}",
    //             path.parent().unwrap(),
    //             path.file_name().unwrap()
    //         );
    //     }
    // }

    /// Write the tree structure in the path.
    ///
    /// # Errors:
    ///
    /// - If provided folder doesn't exist, or is not a directory.
    /// - If any other IO error occurs.
    pub fn write_at(&self, folder: impl AsRef<Path>) -> Result<()> {
        let folder = folder.as_ref();

        #[cfg(feature = "fs-err")]
        let symlink_function = fs_err::os::unix::fs::symlink;
        #[cfg(not(feature = "fs-err"))]
        let symlink_function = std::os::unix::fs::symlink;

        for (node, path) in self.iter().skip(1) {
            let path = folder.join(&path);

            match &node {
                Self::Regular => {
                    fs::File::create(path)?;
                },
                Self::Directory(_) => {
                    fs::create_dir(path)?;
                },
                Self::Symlink(target) => {
                    symlink_function(target, path)?;
                },
            }
        }

        Ok(())
    }

    /// Returns a reference to the node at the path, if any.
    ///
    /// # Errors:
    ///
    /// - Returns `None` if there is no node at the given path.
    ///
    /// # Examples:
    ///
    /// ```
    /// use fs_tree::FsTree;
    ///
    /// let root = FsTree::from_path_text("a/b/c");
    ///
    /// // Indexing is relative from `root`, so `root` cannot be indexed.
    /// assert_eq!(root, FsTree::from_path_text("a/b/c"));
    /// assert_eq!(root["a"], FsTree::from_path_text("b/c"));
    /// assert_eq!(root["a/b"], FsTree::from_path_text("c"));
    /// assert_eq!(root["a"]["b"], FsTree::from_path_text("c"));
    /// assert_eq!(root["a/b/c"], FsTree::Regular);
    /// assert_eq!(root["a/b"]["c"], FsTree::Regular);
    /// assert_eq!(root["a"]["b/c"], FsTree::Regular);
    /// assert_eq!(root["a"]["b"]["c"], FsTree::Regular);
    /// ```
    pub fn get(&self, path: impl AsRef<Path>) -> Option<&Self> {
        let path = path.as_ref();

        // Split first piece from the rest
        let (popped, path_rest) = {
            let mut iter = path.iter();
            let popped: Option<&Path> = iter.next().map(OsStr::as_ref);
            (popped, iter.as_path())
        };

        // If path ended, we reached the desired node
        let Some(popped) = popped else {
            return Some(self);
        };

        // Corner case: if `.`, ignore it and call again with the rest
        if popped == Path::new(".") {
            return self.get(path_rest);
        }

        self.children()?
            .get(popped)
            .and_then(|child| child.get(path_rest))
    }

    /// Returns a mutable reference to the node at the path, if any.
    ///
    /// This is the mutable version of [`FsTree::get`].
    pub fn get_mut(&mut self, path: impl AsRef<Path>) -> Option<&mut Self> {
        let path = path.as_ref();

        // Split first piece from the rest
        let (popped, path_rest) = {
            let mut iter = path.iter();
            let popped: Option<&Path> = iter.next().map(OsStr::as_ref);
            (popped, iter.as_path())
        };

        // If path ended, we reached the desired node
        let Some(popped) = popped else {
            return Some(self);
        };

        // Corner case: if `.`, ignore it and call again with the rest
        if popped == Path::new(".") {
            return self.get_mut(path_rest);
        }

        self.children_mut()?
            .get_mut(popped)
            .and_then(|child| child.get_mut(path_rest))
    }

    /// Inserts a node at the given path.
    ///
    /// # Panics:
    ///
    /// - If there are no directories up to the path node in order to insert it.
    /// - If path is empty.
    pub fn insert(&mut self, path: impl AsRef<Path>, node: Self) {
        use FsTree::*;

        let mut iter = path.as_ref().iter();

        let Some(node_name) = iter.next_back().map(Path::new) else {
            *self = node;
            return;
        };

        let mut tree = self;

        // Traverse tree
        for next in iter {
            // Give a better error message than the one below
            if !tree.is_dir() {
                panic!(
                    "Failed to insert node, while traversing, one of the parent directories \
                    ({next:?}) isn't a directory, but a {}",
                    tree.variant_str()
                );
            }

            tree = if let Some(tree) = tree.get_mut(next) {
                tree
            } else {
                panic!("Failed to insert node, parent directory {next:?} doesn't exist");
            };
        }

        match tree {
            Regular | Symlink(_) => {
                panic!(
                    "Failed to insert node, parent directory is not a directory, but a {}",
                    tree.variant_str(),
                );
            },
            Directory(children) => {
                children.insert(node_name.into(), node);
            },
        }
    }
}

#[cfg(feature = "libc-file-type")]
impl FsTree {
    /// Returns the file type equivalent [`libc::mode_t`] value.
    pub fn as_mode_t(&self) -> libc::mode_t {
        match self {
            Self::Regular => libc::S_IFREG,
            Self::Directory(_) => libc::S_IFDIR,
            Self::Symlink(_) => libc::S_IFCHR,
        }
    }
}

impl<P> Index<P> for FsTree
where
    P: AsRef<Path>,
{
    type Output = FsTree;

    fn index(&self, path: P) -> &Self::Output {
        self.get(path.as_ref())
            .unwrap_or_else(|| panic!("no node found for path '{}'", path.as_ref().display()))
    }
}

#[cfg(test)]
mod tests {
    use std::{io, path::Path};

    use pretty_assertions::{assert_eq, assert_ne};

    use super::*;
    use crate::tree;

    fn testdir() -> io::Result<(tempfile::TempDir, &'static Path)> {
        let dir = tempfile::tempdir()?;
        let path = dir.path().to_path_buf().into_boxed_path();
        Ok((dir, Box::leak(path)))
    }

    // #[test]
    // fn test_diff() {
    //     let left = FsTree::from_path_text(".config/i3/file").unwrap();
    //     let right = FsTree::from_path_text(".config/i3/folder/file/oie").unwrap();
    //     left.diff(&right);
    //     panic!();
    // }

    #[test]
    fn test_insert_basic() {
        let mut tree = FsTree::new_dir();

        let paths = ["a", "a/b", "a/b/c", "a/b/c/d", "a/b/c/d/e"];
        for path in paths {
            tree.insert(path, FsTree::new_dir());
        }

        tree.insert("a/b/c/d/e/f", FsTree::Regular);

        let expected = tree! {
            a: { b: { c: { d: { e: { f } } } } }
        };

        assert_eq!(tree, expected);
    }

    #[rustfmt::skip]
    #[test]
    fn test_insert_complete() {
        let result = {
            let mut tree = FsTree::new_dir();
            tree.insert("config1", FsTree::Regular);
            tree.insert("config2", FsTree::Regular);
            tree.insert("outer_dir", FsTree::new_dir());
            tree.insert("outer_dir/file1", FsTree::Regular);
            tree.insert("outer_dir/file2", FsTree::Regular);
            tree.insert("outer_dir/inner_dir", FsTree::new_dir());
            tree.insert("outer_dir/inner_dir/inner1", FsTree::Regular);
            tree.insert("outer_dir/inner_dir/inner2", FsTree::Regular);
            tree.insert("outer_dir/inner_dir/inner3", FsTree::Regular);
            tree.insert("outer_dir/inner_dir/inner_link", FsTree::Symlink("inner_target".into()));
            tree.insert("link", FsTree::Symlink("target".into()));
            tree.insert("config3", FsTree::Regular);
            tree
        };

        let expected = tree! {
            config1
            config2
            outer_dir: {
                file1
                file2
                inner_dir: {
                    inner1
                    inner2
                    inner3
                    inner_link -> inner_target
                }
            }
            link -> target
            config3
        };

        assert_eq!(result, expected);
    }

    #[test]
    fn test_write_at() {
        let (_dropper, test_dir) = testdir().unwrap();

        let tree = tree! {
            a: {
                b: {
                    c
                    empty: {}
                    link -> target
                }
            }
        };

        tree.write_at(&test_dir).unwrap();

        let result = FsTree::symlink_read_at(&test_dir).unwrap();

        assert_eq!(result, tree);
    }

    #[test]
    fn test_get() {
        let tree = FsTree::from_path_text("a/b/c");

        assert_eq!(tree["a"], FsTree::from_path_text("b/c"));
        assert_eq!(tree["a/b"], FsTree::from_path_text("c"));
        assert_eq!(tree["a"]["b"], FsTree::from_path_text("c"));
        assert_eq!(tree["a/b/c"], FsTree::Regular);
        assert_eq!(tree["a/b"]["c"], FsTree::Regular);
        assert_eq!(tree["a"]["b/c"], FsTree::Regular);
        assert_eq!(tree["a"]["b"]["c"], FsTree::Regular);

        // Paths are relative, so empty path returns the node itself
        assert_eq!(tree[""], tree);
        assert_eq!(tree[""], tree[""]);

        // "."s are ignored
        assert_eq!(tree["."], tree[""]);
        assert_eq!(tree["././"], tree["."]);
        assert_eq!(tree["././."], tree);
        assert_eq!(tree["./a/."]["././b/./."], FsTree::from_path_text("c"));
        assert_eq!(tree["./a/./b"]["c/."], FsTree::Regular);
    }

    // #[test]
    // fn test_simple_merge() {
    //     let left = FsTree::from_path_text(".config/i3/file");
    //     let right = FsTree::from_path_text(".config/i3/folder/file");
    //     let result = left.try_merge(right);

    //     let expected = tree! {
    //         ".config": {
    //             i3: {
    //                 file
    //                 folder: {
    //                     file
    //                 }
    //             }
    //         }
    //     };

    //     assert_eq!(result, Some(expected));
    // }

    #[test]
    fn test_partial_eq_fails() {
        let left = FsTree::from_path_text(".config/i3/a");
        let right = FsTree::from_path_text(".config/i3/b");

        assert_ne!(left, right);
    }
}