proc_mounts/mounts/
tab.rs

1use super::MountInfo;
2use std::{
3    fmt::{self, Display, Formatter},
4    io,
5    ops::{Deref, DerefMut},
6    str::FromStr,
7};
8
9/// An element in an abtract representation of the mount tab that was read into memory.
10#[derive(Clone, Debug, Eq, PartialEq)]
11pub enum AbstractMountElement {
12    /// An element which is a comment
13    Comment(String),
14    /// An element which is an empty line.
15    Empty,
16    /// An element which defines a mount point
17    Mount(MountInfo),
18}
19
20impl Display for AbstractMountElement {
21    fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
22        match self {
23            AbstractMountElement::Comment(ref comment) => fmt.write_str(comment),
24            AbstractMountElement::Empty => Ok(()),
25            AbstractMountElement::Mount(ref entry) => fmt.write_fmt(format_args!("{}", entry)),
26        }
27    }
28}
29
30impl From<String> for AbstractMountElement {
31    fn from(comment: String) -> Self { AbstractMountElement::Comment(comment) }
32}
33
34impl From<()> for AbstractMountElement {
35    fn from(_: ()) -> Self { AbstractMountElement::Empty }
36}
37
38impl From<MountInfo> for AbstractMountElement {
39    fn from(info: MountInfo) -> Self { AbstractMountElement::Mount(info) }
40}
41
42/// Provides an abstract representation of the contents of a mount tab.
43///
44/// The use case for this type is to enable editing of the original file, or creating new copies,
45/// in a type-safe manner. Each element is an individual line from the original file. Elements
46/// may be inserted, removed, and replaced.
47#[derive(Clone, Default, Debug, Eq, PartialEq)]
48pub struct MountTab(pub Vec<AbstractMountElement>);
49
50impl MountTab {
51    pub fn iter_mounts(&self) -> impl Iterator<Item = &MountInfo> {
52        self.0.iter().filter_map(|e| {
53            if let AbstractMountElement::Mount(e) = e {
54                Some(e)
55            } else {
56                None
57            }
58        })
59    }
60
61    pub fn iter_mounts_mut(&mut self) -> impl Iterator<Item = &mut MountInfo> {
62        self.0.iter_mut().filter_map(|e| {
63            if let AbstractMountElement::Mount(e) = e {
64                Some(e)
65            } else {
66                None
67            }
68        })
69    }
70
71    pub fn push<E: Into<AbstractMountElement>>(&mut self, element: E) {
72        self.0.push(element.into());
73    }
74}
75
76impl Deref for MountTab {
77    type Target = Vec<AbstractMountElement>;
78
79    fn deref(&self) -> &Self::Target { &self.0 }
80}
81
82impl DerefMut for MountTab {
83    fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 }
84}
85
86impl Display for MountTab {
87    fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
88        for entry in &self.0 {
89            writeln!(fmt, "{}", entry)?;
90        }
91
92        Ok(())
93    }
94}
95
96impl FromStr for MountTab {
97    type Err = io::Error;
98
99    fn from_str(input: &str) -> Result<Self, Self::Err> {
100        let mut entries = Vec::new();
101
102        for line in input.lines() {
103            let line = line.trim_start();
104            if line.is_empty() {
105                entries.push(AbstractMountElement::Empty);
106            } else if line.starts_with('#') {
107                entries.push(AbstractMountElement::Comment(line.to_owned()));
108            } else {
109                let info = line.parse::<MountInfo>()?;
110                entries.push(AbstractMountElement::Mount(info));
111            }
112        }
113
114        Ok(MountTab(entries))
115    }
116}