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
use std::borrow::Cow;
use std::fmt;
use std::net::IpAddr;
use std::str::FromStr;
use super::{DataLine, DataParseError, Hosts};
use super::data_line::empty_hosts;

/// Formatted line in `/etc/hosts`.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Line<'a> {
    data: Option<DataLine>,
    comment: Option<Cow<'a, str>>,
}
impl Line<'static> {
    /// Creates an empty line.
    pub fn empty() -> Line<'static> {
        Line {
            data: None,
            comment: None,
        }
    }

    /// Creates a line directly from data
    pub fn from_data(data: DataLine) -> Line<'static> {
        Line {
            data: Some(data),
            comment: None,
        }
    }
}


impl<'a> Line<'a> {
    /// Creates a line from a string.
    pub fn new(line: &str) -> Result<Line, DataParseError> {
        let (comment, stripped) = if let Some(idx) = line.find('#') {
            (Some(Cow::from(line[idx + 1..].trim_left())), &line[..idx])
        } else {
            (None, line)
        };
        let stripped = stripped.trim_right();
        let data = if stripped.is_empty() {
            None
        } else {
            Some(stripped.parse()?)
        };
        Ok(Line { data, comment })
    }

    /// Creates a line directly from a comment.
    pub fn from_comment(comment: &str) -> Line {
        Line {
            data: None,
            comment: Some(comment.into()),
        }
    }

    /// Creates a line from data and a comment.
    pub fn from_raw(data: DataLine, comment: &str) -> Line {
        Line {
            data: Some(data),
            comment: Some(comment.into()),
        }
    }

    /// Gets the IP for this line.
    pub fn ip(&self) -> Option<IpAddr> {
        self.data.as_ref().map(|data| data.ip())
    }

    /// Gets the IP for this line.
    pub fn hosts(&self) -> Hosts {
        if let Some(ref data) = self.data.as_ref() {
            data.hosts()
        } else {
            empty_hosts()
        }
    }

    /// Gets the data from this line.
    pub fn data(&self) -> Option<&DataLine> {
        self.data.as_ref()
    }

    /// Gets the comment from this line.
    pub fn comment<'b>(&'b self) -> Option<&'b str> where 'a: 'b {
        self.comment.as_ref().map(|s| &**s)
    }

    /// Strips the comment from the line.
    pub fn into_data(self) -> Option<DataLine> {
        self.data
    }

    /// Makes an owned version of the line.
    pub fn into_owned(self) -> Line<'static> {
        Line {
            data: self.data,
            comment: self.comment.map(Cow::into_owned).map(Cow::Owned)
        }
    }
}

impl FromStr for Line<'static> {
    type Err = DataParseError;
    fn from_str(s: &str) -> Result<Line<'static>, DataParseError> {
        Line::new(s).map(Line::into_owned)
    }
}

impl<'a> fmt::Display for Line<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match (self.data(), self.comment()) {
            (Some(data), Some(comment)) => write!(f, "{}  # {}", data, comment),
            (None, Some(comment)) => write!(f, "# {}", comment),
            (Some(data), None) => fmt::Display::fmt(data, f),
            (None, None) => Ok(()),
        }
    }
}

#[cfg(test)]
mod tests {
    use std::net::{IpAddr, Ipv4Addr};
    use super::Line;

    #[test]
    fn parse_empty() {
        let empty: Line = "      \t    ".parse().unwrap();
        assert!(empty.comment().is_none());
        assert!(empty.data().is_none());
        assert!(empty.ip().is_none());
        let hosts: Vec<&str> = empty.hosts().collect();
        assert!(hosts.is_empty());
    }

    #[test]
    fn parse_comment() {
        let comment: Line = "   #   \t what? ".parse().unwrap();
        assert_eq!(comment.comment().unwrap(), "what? ");
        assert!(comment.data().is_none());
        assert!(comment.ip().is_none());
        let hosts: Vec<&str> = comment.hosts().collect();
        assert!(hosts.is_empty());
    }

    #[test]
    fn parse_full() {
        let full: Line = "127.0.0.1  \tlocalhost  \t   localhost.localdomain    lh#localhosts".parse().unwrap();
        assert!(full.data().is_some());
        assert_eq!(full.comment().unwrap(), "localhosts");
        assert_eq!(full.ip().unwrap(), IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
        let hosts: Vec<&str> = full.hosts().collect();
        assert_eq!(hosts, vec!["localhost", "localhost.localdomain", "lh"]);
    }
}