Add empty line support

This commit is contained in:
dalance 2020-02-20 10:31:31 +09:00
parent b6c346b61c
commit 032a64ef49
2 changed files with 17 additions and 3 deletions

View file

@ -45,11 +45,11 @@ impl Filelist {
} }
/// Parses a filelist file. /// Parses a filelist file.
/// ///
/// Environment variables represented with paranthesis or /// Environment variables represented with paranthesis or
/// curly braces (i.e. `$()` or `${}`) will be automatically /// curly braces (i.e. `$()` or `${}`) will be automatically
/// substituted. /// substituted.
/// ///
/// # Arguments /// # Arguments
/// ///
/// * `path` - The path to the filelist /// * `path` - The path to the filelist
@ -84,6 +84,7 @@ pub fn parse_file(path: impl AsRef<Path>) -> Result<Filelist, Box<dyn Error>> {
} }
LineType::Comment => filelist.comments_present = true, LineType::Comment => filelist.comments_present = true,
LineType::Unknown => filelist.unknowns_present = true, LineType::Unknown => filelist.unknowns_present = true,
LineType::Empty => (),
LineType::Filelist(path) => { LineType::Filelist(path) => {
filelist.extend(parse_file(path)?); filelist.extend(parse_file(path)?);
} }

View file

@ -8,6 +8,7 @@ pub enum LineType<'a> {
Filelist(&'a str), Filelist(&'a str),
Comment, Comment,
Unknown, Unknown,
Empty,
} }
pub fn parse_line(line: &str) -> LineType { pub fn parse_line(line: &str) -> LineType {
@ -37,6 +38,8 @@ pub fn parse_line(line: &str) -> LineType {
LineType::Comment LineType::Comment
} else if line.starts_with('-') || line.starts_with('+') { } else if line.starts_with('-') || line.starts_with('+') {
LineType::Unknown LineType::Unknown
} else if line.is_empty() {
LineType::Empty
} else { } else {
// Mark everything else as a File // Mark everything else as a File
LineType::File(line) LineType::File(line)
@ -109,6 +112,16 @@ mod test {
assert_eq!(parse_line(line), LineType::Unknown); assert_eq!(parse_line(line), LineType::Unknown);
} }
#[test]
fn parse_line_empty() {
let line = "";
assert_eq!(parse_line(line), LineType::Empty);
let line = " ";
assert_eq!(parse_line(line), LineType::Empty);
let line = "\t";
assert_eq!(parse_line(line), LineType::Empty);
}
#[test] #[test]
fn parse_line_file() { fn parse_line_file() {
let line = "any_random_line_is_a_file"; let line = "any_random_line_is_a_file";