mount-fstab 0.1.1

Type-safe /etc/fstab parsing, editing, and validation library
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
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
//! Core fstab types: `Entry`, `Fstab`, `MountPoint`, `EntryBuilder`.
//!
//! These are the primary data structures used to represent an `/etc/fstab`
//! file and its individual entries.

use crate::error::{EntryBuilderError, MountPointError};
use crate::fstype::FsType;
use crate::options::Options;
use crate::spec::Spec;
use std::fmt;
use std::path::{Path, PathBuf};

/// Mount point — fstab(5) field 2.
///
/// An absolute path or the special value `none` for swap entries.
///
/// # Examples
///
/// ```
/// # use mount_fstab::types::MountPoint;
/// let mp = MountPoint::new("/").unwrap();
/// assert!(mp.is_root());
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MountPoint(PathBuf);

impl MountPoint {
    /// Create a mount point from a path.
    ///
    /// The path must be absolute (start with `/`) or be the special value
    /// `none`.
    ///
    /// # Errors
    ///
    /// Returns [`MountPointError::Empty`] if the path is empty,
    /// [`MountPointError::NotAbsolute`] if the path is relative.
    pub fn new(path: impl Into<PathBuf>) -> Result<Self, MountPointError> {
        let path = path.into();
        let s = path.to_string_lossy();
        if s.is_empty() {
            return Err(MountPointError::Empty);
        }
        if s == "none" || s.starts_with('/') {
            Ok(MountPoint(path))
        } else {
            Err(MountPointError::NotAbsolute)
        }
    }

    /// Create the special `none` mount point for swap.
    #[must_use]
    pub fn swap() -> Self {
        MountPoint(PathBuf::from("none"))
    }

    /// Whether this is a swap mount point (`none`).
    #[must_use]
    pub fn is_swap(&self) -> bool {
        self.0.to_string_lossy() == "none"
    }

    /// Whether this is the root filesystem (`/`).
    ///
    /// Normalizes paths like `//` to `/` via `Path::components()`.
    #[must_use]
    pub fn is_root(&self) -> bool {
        let normalized: PathBuf = self.0.components().collect();
        normalized == Path::new("/")
    }

    /// View the mount point as a `Path`.
    #[must_use]
    pub fn as_path(&self) -> &Path {
        &self.0
    }
}

impl std::ops::Deref for MountPoint {
    type Target = Path;

    /// Dereference to the underlying `Path`.
    ///
    /// This allows `&MountPoint` to be used wherever `&Path` is expected.
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl fmt::Display for MountPoint {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0.display())
    }
}

impl AsRef<Path> for MountPoint {
    fn as_ref(&self) -> &Path {
        &self.0
    }
}

/// Try to convert a string into a `MountPoint`.
///
/// Equivalent to [`MountPoint::new`].
impl TryFrom<&str> for MountPoint {
    type Error = MountPointError;

    fn try_from(s: &str) -> Result<Self, Self::Error> {
        MountPoint::new(s)
    }
}

/// Try to convert a `String` into a `MountPoint`.
///
/// Equivalent to [`MountPoint::new`].
impl TryFrom<String> for MountPoint {
    type Error = MountPointError;

    fn try_from(s: String) -> Result<Self, Self::Error> {
        MountPoint::new(s)
    }
}

/// A single fstab entry — corresponds to libmount `struct libmnt_fs`.
///
/// Each entry represents one line in `/etc/fstab`, consisting of six
/// whitespace-separated fields plus an optional preceding comment block.
///
/// # Examples
///
/// ```
/// # use mount_fstab::{Entry, Spec, MountPoint, FsType, Options};
/// let entry = Entry {
///     spec: Spec::parse("UUID=root").unwrap(),
///     file: MountPoint::new("/").unwrap(),
///     vfstype: FsType::parse("ext4").unwrap(),
///     options: Options::parse("defaults").unwrap(),
///     freq: 0,
///     passno: 1,
///     comment: None,
/// };
/// assert!(entry.is_root());
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Entry {
    /// Filesystem source (device, UUID, label, etc.) — fstab(5) field 1.
    pub spec: Spec,
    /// Mount point path — fstab(5) field 2 (`fs_file`).
    pub file: MountPoint,
    /// Filesystem type — fstab(5) field 3 (`fs_vfstype`).
    pub vfstype: FsType,
    /// Mount options — fstab(5) field 4 (`fs_mntops`).
    pub options: Options,
    /// Dump frequency — fstab(5) field 5 (`fs_freq`). Default: 0.
    pub freq: u32,
    /// Filesystem check order — fstab(5) field 6 (`fs_passno`). Default: 0.
    pub passno: u32,
    /// Comment lines immediately above this entry.
    pub comment: Option<String>,
}

impl Entry {
    /// Create a new entry with the required fields.
    ///
    /// `freq` and `passno` default to 0, and `comment` defaults to `None`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use mount_fstab::{Entry, Spec, MountPoint, FsType, Options};
    /// let entry = Entry::new(
    ///     Spec::parse("UUID=root").unwrap(),
    ///     MountPoint::new("/").unwrap(),
    ///     FsType::parse("ext4").unwrap(),
    ///     Options::parse("defaults").unwrap(),
    /// );
    /// assert!(entry.file.is_root());
    /// assert_eq!(entry.freq, 0);
    /// assert_eq!(entry.passno, 0);
    /// assert!(entry.comment.is_none());
    /// ```
    #[must_use]
    pub fn new(spec: Spec, file: MountPoint, vfstype: FsType, options: Options) -> Self {
        Entry {
            spec,
            file,
            vfstype,
            options,
            freq: 0,
            passno: 0,
            comment: None,
        }
    }

    /// Create an [`EntryBuilder`] for ergonomic construction with optional fields.
    ///
    /// # Examples
    ///
    /// ```
    /// # use mount_fstab::{Entry, Spec, MountPoint, FsType, Options};
    /// let entry = Entry::builder()
    ///     .spec(Spec::parse("UUID=root").unwrap())
    ///     .file(MountPoint::new("/").unwrap())
    ///     .vfstype(FsType::parse("ext4").unwrap())
    ///     .options(Options::parse("defaults,noatime").unwrap())
    ///     .freq(0)
    ///     .passno(1)
    ///     .comment("# Root filesystem")
    ///     .build()
    ///     .unwrap();
    /// assert!(entry.is_root());
    /// ```
    #[must_use]
    pub fn builder() -> EntryBuilder {
        EntryBuilder::default()
    }

    /// Whether this is a swap entry.
    #[must_use]
    pub fn is_swap(&self) -> bool {
        self.vfstype.is_swap()
    }

    /// Whether this is a bind mount.
    #[must_use]
    pub fn is_bind_mount(&self) -> bool {
        self.vfstype.is_bind()
    }

    /// Whether this is the root filesystem entry.
    ///
    /// Returns `true` only when the mount point is `/` **and** `passno == 1`,
    /// following the fstab(5) convention that the root filesystem must have
    /// `fs_passno` set to 1. An entry at `/` with `passno != 1` is not
    /// considered the root entry by this method.
    #[must_use]
    pub fn is_root(&self) -> bool {
        self.file.is_root() && self.passno == 1
    }

    /// Whether this is a network filesystem.
    #[must_use]
    pub fn is_network(&self) -> bool {
        self.vfstype.is_network() || self.options.is_netdev()
    }

    /// Get the comment lines before this entry, if any.
    #[must_use]
    pub fn comment(&self) -> Option<&str> {
        self.comment.as_deref()
    }
}

/// Builder for [`Entry`] with ergonomic construction of optional fields.
///
/// Created via [`Entry::builder()`]. All fields are optional except `spec`,
/// `file`, and `vfstype`, which are required by [`build`](EntryBuilder::build).
///
/// # Examples
///
/// ```
/// # use mount_fstab::{Entry, Spec, MountPoint, FsType, Options};
/// let entry = Entry::builder()
///     .spec(Spec::parse("/dev/sda1").unwrap())
///     .file(MountPoint::new("/").unwrap())
///     .vfstype(FsType::parse("ext4").unwrap())
///     .comment("# Root")
///     .build()
///     .unwrap();
/// ```
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct EntryBuilder {
    spec: Option<Spec>,
    file: Option<MountPoint>,
    vfstype: Option<FsType>,
    options: Options,
    freq: u32,
    passno: u32,
    comment: Option<String>,
}

impl EntryBuilder {
    /// Set the filesystem spec (required).
    pub fn spec(mut self, spec: Spec) -> Self {
        self.spec = Some(spec);
        self
    }
    /// Set the mount point (required).
    pub fn file(mut self, file: MountPoint) -> Self {
        self.file = Some(file);
        self
    }
    /// Set the filesystem type (required).
    pub fn vfstype(mut self, vfstype: FsType) -> Self {
        self.vfstype = Some(vfstype);
        self
    }
    /// Set the mount options (default: empty).
    pub fn options(mut self, options: Options) -> Self {
        self.options = options;
        self
    }
    /// Set the dump frequency (default: 0).
    pub fn freq(mut self, freq: u32) -> Self {
        self.freq = freq;
        self
    }
    /// Set the filesystem check order (default: 0).
    pub fn passno(mut self, passno: u32) -> Self {
        self.passno = passno;
        self
    }
    /// Set the comment preceding this entry.
    pub fn comment(mut self, comment: impl Into<String>) -> Self {
        self.comment = Some(comment.into());
        self
    }

    /// Build the [`Entry`], consuming the builder.
    ///
    /// # Errors
    ///
    /// Returns [`EntryBuilderError`] if any required fields (`spec`, `file`,
    /// `vfstype`) are missing.
    pub fn build(self) -> Result<Entry, EntryBuilderError> {
        Ok(Entry {
            spec: self.spec.ok_or(EntryBuilderError::MissingSpec)?,
            file: self.file.ok_or(EntryBuilderError::MissingFile)?,
            vfstype: self.vfstype.ok_or(EntryBuilderError::MissingFsType)?,
            options: self.options,
            freq: self.freq,
            passno: self.passno,
            comment: self.comment,
        })
    }
}

/// A complete `/etc/fstab` file representation.
///
/// Corresponds to libmount `struct libmnt_table`. Contains the file's
/// entries along with any leading (intro) and trailing comments.
///
/// # Examples
///
/// ```
/// # use mount_fstab::Fstab;
/// let input = "UUID=root / ext4 defaults 0 1\n";
/// let fstab = Fstab::parse_str(input).unwrap();
/// assert_eq!(fstab.len(), 1);
/// ```
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Fstab {
    /// Comment block at the beginning of the file (before the first entry).
    pub intro_comment: Option<String>,
    /// All entries, in original file order.
    pub entries: Vec<Entry>,
    /// Comment block at the end of the file (after the last entry).
    pub trailing_comment: Option<String>,
}

impl Fstab {
    /// Create an empty fstab.
    #[must_use]
    pub fn new() -> Self {
        Fstab::default()
    }

    /// Number of entries.
    #[must_use]
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Whether the fstab has no entries.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Return a slice of all entries.
    #[must_use]
    pub fn entries(&self) -> &[Entry] {
        &self.entries
    }

    /// Create an [`Fstab`] from a vector of entries.
    ///
    /// # Examples
    ///
    /// ```
    /// # use mount_fstab::{Entry, Fstab, Spec, MountPoint, FsType, Options};
    /// let entry = Entry::new(
    ///     Spec::parse("/dev/sda1").unwrap(),
    ///     MountPoint::new("/").unwrap(),
    ///     FsType::parse("ext4").unwrap(),
    ///     Options::defaults(),
    /// );
    /// let fstab = Fstab::from_entries(vec![entry]);
    /// assert_eq!(fstab.len(), 1);
    /// ```
    #[must_use]
    pub fn from_entries(entries: Vec<Entry>) -> Self {
        Fstab {
            entries,
            intro_comment: None,
            trailing_comment: None,
        }
    }

    /// Consume the `Fstab` and return its entries.
    ///
    /// # Examples
    ///
    /// ```
    /// # use mount_fstab::{Entry, Fstab, Spec, MountPoint, FsType, Options};
    /// let mut fstab = Fstab::new();
    /// fstab.add(Entry::new(
    ///     Spec::parse("/dev/sda1").unwrap(),
    ///     MountPoint::new("/").unwrap(),
    ///     FsType::parse("ext4").unwrap(),
    ///     Options::defaults(),
    /// ));
    /// let entries = fstab.into_entries();
    /// assert_eq!(entries.len(), 1);
    /// ```
    #[must_use]
    pub fn into_entries(self) -> Vec<Entry> {
        self.entries
    }

    /// Add an entry to the end.
    ///
    /// Returns `&mut Self` for chaining.
    ///
    /// # Examples
    ///
    /// ```
    /// # use mount_fstab::{Entry, Fstab, Spec, MountPoint, FsType, Options};
    /// let mut fstab = Fstab::new();
    /// let entry = Entry::new(
    ///     Spec::parse("UUID=root").unwrap(),
    ///     MountPoint::new("/").unwrap(),
    ///     FsType::parse("ext4").unwrap(),
    ///     Options::defaults(),
    /// );
    /// fstab.add(entry);
    /// assert_eq!(fstab.len(), 1);
    /// ```
    pub fn add(&mut self, entry: Entry) -> &mut Self {
        self.entries.push(entry);
        self
    }

    /// Insert an entry at the given position.
    ///
    /// Returns `&mut Self` for chaining.
    ///
    /// # Errors
    ///
    /// Returns [`FstabError`](crate::error::FstabError) as
    /// `IndexOutOfBounds(index, len)` if `index > len`.
    pub fn insert(
        &mut self,
        index: usize,
        entry: Entry,
    ) -> Result<&mut Self, crate::error::FstabError> {
        if index > self.entries.len() {
            return Err(crate::error::FstabError::IndexOutOfBounds(
                index,
                self.entries.len(),
            ));
        }
        self.entries.insert(index, entry);
        Ok(self)
    }

    /// Remove an entry at the given position, returning it.
    ///
    /// # Examples
    ///
    /// ```
    /// # use mount_fstab::{Entry, Fstab, Spec, MountPoint, FsType, Options};
    /// let mut fstab = Fstab::new();
    /// fstab.add(Entry::new(
    ///     Spec::parse("UUID=root").unwrap(),
    ///     MountPoint::new("/").unwrap(),
    ///     FsType::parse("ext4").unwrap(),
    ///     Options::defaults(),
    /// ));
    /// let removed = fstab.remove(0);
    /// assert!(removed.is_some());
    /// assert!(fstab.is_empty());
    /// ```
    pub fn remove(&mut self, index: usize) -> Option<Entry> {
        if index < self.entries.len() {
            Some(self.entries.remove(index))
        } else {
            None
        }
    }

    /// Replace an entry at the given position, returning the old entry.
    pub fn replace(&mut self, index: usize, entry: Entry) -> Option<Entry> {
        if index < self.entries.len() {
            Some(std::mem::replace(&mut self.entries[index], entry))
        } else {
            None
        }
    }

    /// Remove all entries and comments.
    pub fn clear(&mut self) {
        self.entries.clear();
        self.intro_comment = None;
        self.trailing_comment = None;
    }

    /// Find entries by source string match (substring match on spec).
    #[must_use]
    pub fn find_by_source(&self, source: &str) -> Vec<&Entry> {
        self.entries
            .iter()
            .filter(|e| e.spec.to_string().contains(source))
            .collect()
    }

    /// Find an entry by mount point.
    ///
    /// # Examples
    ///
    /// ```
    /// # use mount_fstab::{Entry, Fstab, Spec, MountPoint, FsType, Options};
    /// # use std::path::Path;
    /// let mut fstab = Fstab::new();
    /// fstab.add(Entry::new(
    ///     Spec::parse("UUID=root").unwrap(),
    ///     MountPoint::new("/").unwrap(),
    ///     FsType::parse("ext4").unwrap(),
    ///     Options::defaults(),
    /// ));
    /// assert!(fstab.find_by_mountpoint(Path::new("/")).is_some());
    /// assert!(fstab.find_by_mountpoint(Path::new("/nonexistent")).is_none());
    /// ```
    #[must_use]
    pub fn find_by_mountpoint(&self, mp: &Path) -> Option<&Entry> {
        self.entries.iter().find(|e| e.file.as_path() == mp)
    }

    /// Get the root filesystem entry (passno == 1 at `/`).
    #[must_use]
    pub fn root(&self) -> Option<&Entry> {
        self.entries
            .iter()
            .find(|e| e.passno == 1 && e.file.is_root())
    }
}

impl IntoIterator for Fstab {
    type Item = Entry;
    type IntoIter = std::vec::IntoIter<Entry>;

    /// Consume the `Fstab` and iterate over its entries.
    fn into_iter(self) -> Self::IntoIter {
        self.entries.into_iter()
    }
}

impl<'a> IntoIterator for &'a Fstab {
    type Item = &'a Entry;
    type IntoIter = std::slice::Iter<'a, Entry>;

    /// Iterate over references to entries.
    fn into_iter(self) -> Self::IntoIter {
        self.entries.iter()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::error::EntryBuilderError;
    use crate::fstype::FsType;
    use crate::options::Options;
    use crate::spec::Spec;
    use std::path::Path;

    // ── MountPoint tests ──

    #[test]
    fn mount_point_new_absolute_path() {
        let mp = MountPoint::new("/mnt/data").unwrap();
        assert_eq!(mp.as_path(), Path::new("/mnt/data"));
        assert!(!mp.is_swap());
        assert!(!mp.is_root());
    }

    #[test]
    fn mount_point_new_root() {
        let mp = MountPoint::new("/").unwrap();
        assert!(mp.is_root());
        assert!(!mp.is_swap());
    }

    #[test]
    fn mount_point_new_none() {
        let mp = MountPoint::new("none").unwrap();
        assert!(mp.is_swap());
        assert!(!mp.is_root());
    }

    #[test]
    fn mount_point_new_empty() {
        let err = MountPoint::new("").unwrap_err();
        assert_eq!(err, MountPointError::Empty);
    }

    #[test]
    fn mount_point_new_not_absolute() {
        let err = MountPoint::new("relative/path").unwrap_err();
        assert_eq!(err, MountPointError::NotAbsolute);
    }

    #[test]
    fn mount_point_swap_constructor() {
        let mp = MountPoint::swap();
        assert!(mp.is_swap());
        assert_eq!(mp.as_path(), Path::new("none"));
    }

    #[test]
    fn mount_point_deref() {
        let mp = MountPoint::new("/etc").unwrap();
        let path: &Path = &*mp;
        assert_eq!(path, Path::new("/etc"));
        // Can call Path methods directly
        assert!(mp.is_absolute());
        assert!(mp.parent() == Some(Path::new("/")));
    }

    // ── Entry tests ──

    #[test]
    fn entry_new_defaults() {
        let spec = Spec::Device("/dev/sda1".into());
        let file = MountPoint::new("/").unwrap();
        let fstype = FsType::new("ext4").unwrap();
        let opts = Options::defaults();
        let entry = Entry::new(spec.clone(), file.clone(), fstype.clone(), opts.clone());

        assert_eq!(entry.spec, spec);
        assert_eq!(entry.file, file);
        assert_eq!(entry.vfstype, fstype);
        assert_eq!(entry.options, opts);
        assert_eq!(entry.freq, 0);
        assert_eq!(entry.passno, 0);
        assert_eq!(entry.comment, None);
    }

    #[test]
    fn entry_is_swap() {
        let entry = Entry {
            spec: Spec::Keyword("none".into()),
            file: MountPoint::new("none").unwrap(),
            vfstype: FsType::swap(),
            options: Options::defaults(),
            freq: 0,
            passno: 0,
            comment: None,
        };
        assert!(entry.is_swap());
    }

    #[test]
    fn entry_is_bind_mount() {
        let entry = Entry {
            spec: Spec::Device("/dev/sda1".into()),
            file: MountPoint::new("/mnt/bind").unwrap(),
            vfstype: FsType::bind(),
            options: Options::defaults(),
            freq: 0,
            passno: 0,
            comment: None,
        };
        assert!(entry.is_bind_mount());
    }

    #[test]
    fn entry_is_root() {
        let entry = Entry {
            spec: Spec::Device("/dev/sda1".into()),
            file: MountPoint::new("/").unwrap(),
            vfstype: FsType::new("ext4").unwrap(),
            options: Options::defaults(),
            freq: 0,
            passno: 1,
            comment: None,
        };
        assert!(entry.is_root());
    }

    #[test]
    fn entry_is_root_requires_passno() {
        let entry = Entry {
            spec: Spec::Device("/dev/sda1".into()),
            file: MountPoint::new("/").unwrap(),
            vfstype: FsType::new("ext4").unwrap(),
            options: Options::defaults(),
            freq: 0,
            passno: 0,
            comment: None,
        };
        assert!(!entry.is_root());
    }

    #[test]
    fn entry_is_network_by_fstype() {
        let entry = Entry {
            spec: Spec::NetworkMount {
                host: "server".into(),
                path: "/export".into(),
            },
            file: MountPoint::new("/mnt/nfs").unwrap(),
            vfstype: FsType::new("nfs").unwrap(),
            options: Options::new(),
            freq: 0,
            passno: 0,
            comment: None,
        };
        assert!(entry.is_network());
    }

    #[test]
    fn entry_is_network_by_netdev_option() {
        let entry = Entry {
            spec: Spec::Device("/dev/sda1".into()),
            file: MountPoint::new("/mnt/data").unwrap(),
            vfstype: FsType::new("ext4").unwrap(),
            options: Options::parse("_netdev").unwrap(),
            freq: 0,
            passno: 0,
            comment: None,
        };
        assert!(entry.is_network());
    }

    #[test]
    fn entry_builder_minimal() {
        let entry = Entry::builder()
            .spec(Spec::parse("/dev/sda1").unwrap())
            .file(MountPoint::new("/").unwrap())
            .vfstype(FsType::parse("ext4").unwrap())
            .build()
            .unwrap();
        assert_eq!(entry.spec, Spec::Device("/dev/sda1".into()));
        assert!(entry.file.is_root());
        assert_eq!(entry.vfstype.as_str(), "ext4");
        assert!(entry.options.is_empty());
        assert_eq!(entry.freq, 0);
        assert_eq!(entry.passno, 0);
    }

    #[test]
    fn entry_builder_all_fields() {
        let entry = Entry::builder()
            .spec(Spec::parse("UUID=root").unwrap())
            .file(MountPoint::new("/").unwrap())
            .vfstype(FsType::parse("ext4").unwrap())
            .options(Options::parse("defaults,noatime").unwrap())
            .freq(0)
            .passno(1)
            .comment("# Root filesystem")
            .build()
            .unwrap();
        assert_eq!(entry.spec, Spec::Uuid("root".into()));
        assert!(entry.file.is_root());
        assert_eq!(entry.passno, 1);
        assert_eq!(entry.comment, Some("# Root filesystem".into()));
    }

    #[test]
    fn entry_builder_missing_spec() {
        let err = Entry::builder()
            .file(MountPoint::new("/").unwrap())
            .vfstype(FsType::parse("ext4").unwrap())
            .build()
            .unwrap_err();
        assert_eq!(err, EntryBuilderError::MissingSpec);
    }

    #[test]
    fn entry_builder_missing_file() {
        let err = Entry::builder()
            .spec(Spec::parse("/dev/sda1").unwrap())
            .vfstype(FsType::parse("ext4").unwrap())
            .build()
            .unwrap_err();
        assert_eq!(err, EntryBuilderError::MissingFile);
    }

    #[test]
    fn entry_builder_missing_vfstype() {
        let err = Entry::builder()
            .spec(Spec::parse("/dev/sda1").unwrap())
            .file(MountPoint::new("/").unwrap())
            .build()
            .unwrap_err();
        assert_eq!(err, EntryBuilderError::MissingFsType);
    }

    #[test]
    fn entry_comment_accessor() {
        let entry = Entry {
            comment: Some("# Test".into()),
            ..Entry::new(
                Spec::Device("/dev/sda1".into()),
                MountPoint::new("/").unwrap(),
                FsType::new("ext4").unwrap(),
                Options::defaults(),
            )
        };
        assert_eq!(entry.comment(), Some("# Test"));
    }

    // ── Fstab tests ──

    #[test]
    fn fstab_new_is_empty() {
        let fstab = Fstab::new();
        assert!(fstab.is_empty());
        assert_eq!(fstab.len(), 0);
    }

    #[test]
    fn fstab_add_entry() {
        let mut fstab = Fstab::new();
        let entry = Entry::new(
            Spec::Device("/dev/sda1".into()),
            MountPoint::new("/").unwrap(),
            FsType::new("ext4").unwrap(),
            Options::defaults(),
        );
        fstab.add(entry);
        assert_eq!(fstab.len(), 1);
        assert!(!fstab.is_empty());
    }

    #[test]
    fn fstab_add_chaining() {
        let mut fstab = Fstab::new();
        let e1 = Entry::new(
            Spec::Device("/dev/sda1".into()),
            MountPoint::new("/").unwrap(),
            FsType::new("ext4").unwrap(),
            Options::defaults(),
        );
        let e2 = Entry::new(
            Spec::Device("/dev/sda2".into()),
            MountPoint::new("/home").unwrap(),
            FsType::new("ext4").unwrap(),
            Options::defaults(),
        );
        fstab.add(e1).add(e2);
        assert_eq!(fstab.len(), 2);
    }

    #[test]
    fn fstab_insert_valid() {
        let mut fstab = Fstab::new();
        let e1 = Entry::new(
            Spec::Device("/dev/sda1".into()),
            MountPoint::new("/").unwrap(),
            FsType::new("ext4").unwrap(),
            Options::defaults(),
        );
        let e2 = Entry::new(
            Spec::Device("/dev/sda2".into()),
            MountPoint::new("/home").unwrap(),
            FsType::new("ext4").unwrap(),
            Options::defaults(),
        );
        fstab.add(e1);
        let e3 = Entry::new(
            Spec::Device("/dev/sdb1".into()),
            MountPoint::new("/mnt/data").unwrap(),
            FsType::new("xfs").unwrap(),
            Options::defaults(),
        );
        // Insert at end with chaining
        assert!(fstab.insert(1, e3).is_ok());
        assert_eq!(fstab.len(), 2);
        // Insert at beginning
        assert!(fstab.insert(0, e2).is_ok());
        assert_eq!(fstab.len(), 3);
    }

    #[test]
    fn fstab_insert_out_of_bounds() {
        let mut fstab = Fstab::new();
        let entry = Entry::new(
            Spec::Device("/dev/sda1".into()),
            MountPoint::new("/").unwrap(),
            FsType::new("ext4").unwrap(),
            Options::defaults(),
        );
        let result = fstab.insert(1, entry);
        assert!(result.is_err());
    }

    #[test]
    fn fstab_remove() {
        let mut fstab = Fstab::new();
        let entry = Entry::new(
            Spec::Device("/dev/sda1".into()),
            MountPoint::new("/").unwrap(),
            FsType::new("ext4").unwrap(),
            Options::defaults(),
        );
        fstab.add(entry);
        let removed = fstab.remove(0);
        assert!(removed.is_some());
        assert!(fstab.is_empty());

        let none = fstab.remove(0);
        assert!(none.is_none());
    }

    #[test]
    fn fstab_replace() {
        let mut fstab = Fstab::new();
        let e1 = Entry::new(
            Spec::Device("/dev/sda1".into()),
            MountPoint::new("/").unwrap(),
            FsType::new("ext4").unwrap(),
            Options::defaults(),
        );
        let e2 = Entry::new(
            Spec::Device("/dev/sdb1".into()),
            MountPoint::new("/mnt/data").unwrap(),
            FsType::new("xfs").unwrap(),
            Options::defaults(),
        );
        fstab.add(e1);
        let old = fstab.replace(0, e2);
        assert!(old.is_some());
        assert_eq!(fstab.len(), 1);

        let none = fstab.replace(5, old.unwrap());
        assert!(none.is_none());
    }

    #[test]
    fn fstab_clear() {
        let mut fstab = Fstab::new();
        fstab.intro_comment = Some("# intro".into());
        fstab.add(Entry::new(
            Spec::Device("/dev/sda1".into()),
            MountPoint::new("/").unwrap(),
            FsType::new("ext4").unwrap(),
            Options::defaults(),
        ));
        fstab.trailing_comment = Some("# trailing".into());
        fstab.clear();
        assert!(fstab.is_empty());
        assert!(fstab.intro_comment.is_none());
        assert!(fstab.trailing_comment.is_none());
    }

    #[test]
    fn fstab_find_by_source() {
        let mut fstab = Fstab::new();
        fstab.add(Entry::new(
            Spec::Device("/dev/sda1".into()),
            MountPoint::new("/").unwrap(),
            FsType::new("ext4").unwrap(),
            Options::defaults(),
        ));
        fstab.add(Entry::new(
            Spec::Uuid("abc-123".into()),
            MountPoint::new("/home").unwrap(),
            FsType::new("ext4").unwrap(),
            Options::defaults(),
        ));
        let results = fstab.find_by_source("sda1");
        assert_eq!(results.len(), 1);
        let results = fstab.find_by_source("ext4");
        assert_eq!(results.len(), 0);
    }

    #[test]
    fn fstab_find_by_source_label() {
        let mut fstab = Fstab::new();
        fstab.add(Entry::new(
            Spec::Label("ROOT".into()),
            MountPoint::new("/").unwrap(),
            FsType::new("ext4").unwrap(),
            Options::defaults(),
        ));
        let results = fstab.find_by_source("LABEL=ROOT");
        assert_eq!(results.len(), 1);
    }

    #[test]
    fn fstab_find_by_mountpoint() {
        let mut fstab = Fstab::new();
        fstab.add(Entry::new(
            Spec::Device("/dev/sda1".into()),
            MountPoint::new("/").unwrap(),
            FsType::new("ext4").unwrap(),
            Options::defaults(),
        ));
        fstab.add(Entry::new(
            Spec::Device("/dev/sda2".into()),
            MountPoint::new("/home").unwrap(),
            FsType::new("ext4").unwrap(),
            Options::defaults(),
        ));
        let found = fstab.find_by_mountpoint(Path::new("/home"));
        assert!(found.is_some());
        assert!(
            fstab
                .find_by_mountpoint(Path::new("/nonexistent"))
                .is_none()
        );
    }

    #[test]
    fn fstab_root() {
        let mut fstab = Fstab::new();
        let root_entry = Entry {
            spec: Spec::Device("/dev/sda1".into()),
            file: MountPoint::new("/").unwrap(),
            vfstype: FsType::new("ext4").unwrap(),
            options: Options::defaults(),
            freq: 0,
            passno: 1,
            comment: None,
        };
        fstab.add(Entry::new(
            Spec::Device("/dev/sda2".into()),
            MountPoint::new("/home").unwrap(),
            FsType::new("ext4").unwrap(),
            Options::defaults(),
        ));
        fstab.add(root_entry);
        let root = fstab.root();
        assert!(root.is_some());
        assert!(root.unwrap().is_root());
    }

    #[test]
    fn fstab_root_no_root_entry() {
        let mut fstab = Fstab::new();
        fstab.add(Entry::new(
            Spec::Device("/dev/sda1".into()),
            MountPoint::new("/home").unwrap(),
            FsType::new("ext4").unwrap(),
            Options::defaults(),
        ));
        assert!(fstab.root().is_none());
    }

    #[test]
    fn fstab_entries_slice() {
        let mut fstab = Fstab::new();
        fstab.add(Entry::new(
            Spec::Device("/dev/sda1".into()),
            MountPoint::new("/").unwrap(),
            FsType::new("ext4").unwrap(),
            Options::defaults(),
        ));
        assert_eq!(fstab.entries().len(), 1);
    }

    #[test]
    fn fstab_into_iter() {
        let mut fstab = Fstab::new();
        fstab.add(Entry::new(
            Spec::Device("/dev/sda1".into()),
            MountPoint::new("/").unwrap(),
            FsType::new("ext4").unwrap(),
            Options::defaults(),
        ));
        fstab.add(Entry::new(
            Spec::Device("/dev/sda2".into()),
            MountPoint::new("/home").unwrap(),
            FsType::new("ext4").unwrap(),
            Options::defaults(),
        ));

        let count = fstab.into_iter().count();
        assert_eq!(count, 2);
    }

    #[test]
    fn fstab_ref_into_iter() {
        let mut fstab = Fstab::new();
        fstab.add(Entry::new(
            Spec::Device("/dev/sda1".into()),
            MountPoint::new("/").unwrap(),
            FsType::new("ext4").unwrap(),
            Options::defaults(),
        ));

        let entries: Vec<&Entry> = (&fstab).into_iter().collect();
        assert_eq!(entries.len(), 1);
    }

    // ── Spec Display tests ──

    #[test]
    fn spec_display_device() {
        let spec = Spec::Device("/dev/sda1".into());
        assert_eq!(spec.to_string(), "/dev/sda1");
    }

    #[test]
    fn spec_display_label() {
        let spec = Spec::Label("ROOT".into());
        assert_eq!(spec.to_string(), "LABEL=ROOT");
    }

    #[test]
    fn spec_display_uuid() {
        let spec = Spec::Uuid("abc-123".into());
        assert_eq!(spec.to_string(), "UUID=abc-123");
    }

    #[test]
    fn spec_display_partlabel() {
        let spec = Spec::PartLabel("System".into());
        assert_eq!(spec.to_string(), "PARTLABEL=System");
    }

    #[test]
    fn spec_display_partuuid() {
        let spec = Spec::PartUuid("abc-def".into());
        assert_eq!(spec.to_string(), "PARTUUID=abc-def");
    }

    #[test]
    fn spec_display_id() {
        #[allow(deprecated)]
        let spec = Spec::Id("wwn-0x50014ee2".into());
        assert_eq!(spec.to_string(), "ID=wwn-0x50014ee2");
    }

    #[test]
    fn spec_display_network() {
        let spec = Spec::NetworkMount {
            host: "server".into(),
            path: "/export".into(),
        };
        assert_eq!(spec.to_string(), "server:/export");
    }

    #[test]
    fn spec_display_keyword() {
        let spec = Spec::Keyword("proc".into());
        assert_eq!(spec.to_string(), "proc");
    }
}