oma_apt_sources_lists/
source_line.rs

1use super::*;
2use std::fmt;
3use std::str::FromStr;
4
5/// A line from an apt source list.
6#[derive(Clone, Debug, PartialEq)]
7pub enum SourceLine {
8    Comment(String),
9    Empty,
10    Entry(SourceEntry),
11}
12
13impl fmt::Display for SourceLine {
14    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
15        match *self {
16            SourceLine::Comment(ref comment) => write!(fmt, "{}", comment),
17            SourceLine::Empty => Ok(()),
18            SourceLine::Entry(ref entry) => write!(fmt, "{}", entry),
19        }
20    }
21}
22
23impl FromStr for SourceLine {
24    type Err = SourceError;
25    fn from_str(line: &str) -> Result<Self, Self::Err> {
26        let line = line.trim();
27        if let Some(inner) = line.strip_prefix('#') {
28            let inner = inner.trim();
29            let entry = if !inner.is_empty() {
30                line.parse::<SourceEntry>().ok()
31            } else {
32                None
33            };
34
35            Ok(entry.map_or_else(
36                || SourceLine::Comment(line.into()),
37                |mut entry| {
38                    entry.enabled = false;
39                    SourceLine::Entry(entry)
40                },
41            ))
42        } else if line.is_empty() {
43            Ok(SourceLine::Empty)
44        } else {
45            Ok(SourceLine::Entry(line.parse::<SourceEntry>()?))
46        }
47    }
48}