|
| 1 | +//! File list reading functionality for --files-from option |
| 2 | +//! |
| 3 | +//! This module provides the `FilesFrom` struct which handles reading input file |
| 4 | +//! lists from any reader, with support for comments and empty line filtering. |
| 5 | +
|
| 6 | +use anyhow::{Context, Result}; |
| 7 | +use std::io::{BufRead, BufReader, Read}; |
| 8 | +use std::path::Path; |
| 9 | + |
| 10 | +/// Comment marker for ignoring lines in files-from input |
| 11 | +const COMMENT_MARKER: &str = "#"; |
| 12 | + |
| 13 | +/// Represents a source of input file paths that can be read from any reader |
| 14 | +#[derive(Debug, Clone)] |
| 15 | +pub(crate) struct FilesFrom { |
| 16 | + /// The list of input file paths |
| 17 | + pub(crate) inputs: Vec<String>, |
| 18 | +} |
| 19 | + |
| 20 | +impl FilesFrom { |
| 21 | + /// Create `FilesFrom` from any reader |
| 22 | + pub(crate) fn from_reader<R: Read>(reader: R) -> Result<Self> { |
| 23 | + let buf_reader = BufReader::new(reader); |
| 24 | + let lines: Vec<String> = buf_reader |
| 25 | + .lines() |
| 26 | + .collect::<Result<Vec<_>, _>>() |
| 27 | + .context("Cannot read lines from reader")?; |
| 28 | + |
| 29 | + let inputs = Self::filter_lines(lines); |
| 30 | + Ok(FilesFrom { inputs }) |
| 31 | + } |
| 32 | + |
| 33 | + /// Filter out comments and empty lines from input |
| 34 | + fn filter_lines(lines: Vec<String>) -> Vec<String> { |
| 35 | + lines |
| 36 | + .into_iter() |
| 37 | + .filter(|line| { |
| 38 | + let line = line.trim(); |
| 39 | + !line.is_empty() && !line.starts_with(COMMENT_MARKER) |
| 40 | + }) |
| 41 | + .collect() |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl TryFrom<&Path> for FilesFrom { |
| 46 | + type Error = anyhow::Error; |
| 47 | + |
| 48 | + fn try_from(path: &Path) -> Result<Self, Self::Error> { |
| 49 | + if path == Path::new("-") { |
| 50 | + Self::from_reader(std::io::stdin()) |
| 51 | + } else { |
| 52 | + let file = std::fs::File::open(path) |
| 53 | + .with_context(|| format!("Cannot open --files-from file: {}", path.display()))?; |
| 54 | + Self::from_reader(file) |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +#[cfg(test)] |
| 60 | +mod tests { |
| 61 | + use super::*; |
| 62 | + use std::fs; |
| 63 | + use std::io::Cursor; |
| 64 | + use tempfile::tempdir; |
| 65 | + |
| 66 | + #[test] |
| 67 | + fn test_filter_lines() { |
| 68 | + let input = vec![ |
| 69 | + "file1.md".to_string(), |
| 70 | + String::new(), |
| 71 | + "# This is a comment".to_string(), |
| 72 | + "file2.md".to_string(), |
| 73 | + " ".to_string(), |
| 74 | + " # Another comment".to_string(), |
| 75 | + "file3.md".to_string(), |
| 76 | + ]; |
| 77 | + |
| 78 | + let result = FilesFrom::filter_lines(input); |
| 79 | + assert_eq!(result, vec!["file1.md", "file2.md", "file3.md"]); |
| 80 | + } |
| 81 | + |
| 82 | + #[test] |
| 83 | + fn test_from_reader() -> Result<()> { |
| 84 | + let input = "# Comment\nfile1.md\n\nfile2.md\n# Another comment\nfile3.md\n"; |
| 85 | + let reader = Cursor::new(input); |
| 86 | + |
| 87 | + let files_from = FilesFrom::from_reader(reader)?; |
| 88 | + assert_eq!(files_from.inputs, vec!["file1.md", "file2.md", "file3.md"]); |
| 89 | + |
| 90 | + Ok(()) |
| 91 | + } |
| 92 | + |
| 93 | + #[test] |
| 94 | + fn test_from_reader_empty() -> Result<()> { |
| 95 | + let input = "# Only comments\n\n# More comments\n \n"; |
| 96 | + let reader = Cursor::new(input); |
| 97 | + |
| 98 | + let files_from = FilesFrom::from_reader(reader)?; |
| 99 | + assert_eq!(files_from.inputs, Vec::<String>::new()); |
| 100 | + |
| 101 | + Ok(()) |
| 102 | + } |
| 103 | + |
| 104 | + #[test] |
| 105 | + fn test_try_from_file() -> Result<()> { |
| 106 | + let temp_dir = tempdir()?; |
| 107 | + let file_path = temp_dir.path().join("files.txt"); |
| 108 | + |
| 109 | + fs::write( |
| 110 | + &file_path, |
| 111 | + "# Comment\nfile1.md\n\nfile2.md\n# Another comment\nfile3.md\n", |
| 112 | + )?; |
| 113 | + |
| 114 | + let files_from = FilesFrom::try_from(file_path.as_path())?; |
| 115 | + assert_eq!(files_from.inputs, vec!["file1.md", "file2.md", "file3.md"]); |
| 116 | + |
| 117 | + Ok(()) |
| 118 | + } |
| 119 | + |
| 120 | + #[test] |
| 121 | + fn test_try_from_nonexistent_file() { |
| 122 | + let result = FilesFrom::try_from(Path::new("/nonexistent/file.txt")); |
| 123 | + assert!(result.is_err()); |
| 124 | + assert!( |
| 125 | + result |
| 126 | + .unwrap_err() |
| 127 | + .to_string() |
| 128 | + .contains("Cannot open --files-from file") |
| 129 | + ); |
| 130 | + } |
| 131 | +} |
0 commit comments