diff options
| author | Dennis Brentjes <d.brentjes@gmail.com> | 2019-08-12 21:54:45 +0200 |
|---|---|---|
| committer | Dennis Brentjes <d.brentjes@gmail.com> | 2019-08-12 21:54:45 +0200 |
| commit | 0bcf9757e332be1000fa9df22920ebdd59f1de31 (patch) | |
| tree | 939d38579e856558a2f3090405045dd37385d8e2 /src/config.rs | |
| download | cdart-0bcf9757e332be1000fa9df22920ebdd59f1de31.tar.gz cdart-0bcf9757e332be1000fa9df22920ebdd59f1de31.tar.bz2 cdart-0bcf9757e332be1000fa9df22920ebdd59f1de31.zip | |
Diffstat (limited to 'src/config.rs')
| -rw-r--r-- | src/config.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..d690ed2 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,48 @@ +use std::io::{BufRead, Error}; +use std::path::PathBuf; + +#[derive(Debug)] +pub(crate) struct Entry { + filename: PathBuf +} + +#[derive(Debug)] +pub(crate) struct Config { + entries: Vec<Entry> +} + +impl Entry {} + +impl Config {} + +pub(crate) fn create_config(reader: impl BufRead) -> Result<Config, String> { + let lines= reader.lines(); + let(successful_lines, failed_lines) : (Vec<Result<String,Error>>, Vec<Result<String, Error>>) = lines.partition(Result::is_ok); + + let successful_lines : Vec<_> = successful_lines.into_iter().map(Result::unwrap).collect(); + let failed_lines : Vec<_> = failed_lines.into_iter().map(Result::unwrap_err).collect(); + + if !failed_lines.is_empty() { + return Result::Err(format!("Something went wrong when parsing file lines")); + } + + let (entries, failures) : (Vec<Result<Entry, String>>, Vec<Result<Entry, String>>) = successful_lines.into_iter().map(|line| { + match line.split_whitespace().collect::<Vec<&str>>().as_slice() { + [s] => { + return Result::Ok(Entry {filename: PathBuf::from(s)}); + } + _ => { + return Result::Err(format!("Line contains invalid Entry: {}", line)); + } + } + }).partition(Result::is_ok); + + let entries : Vec<_> = entries.into_iter().map(Result::unwrap).collect(); + let failures : Vec<_> = failures.into_iter().map(Result::unwrap_err).collect(); + + if !failures.is_empty() { + return Result::Err(failures.first().unwrap().to_string()) + } + + return Result::Ok(Config {entries}); +}
\ No newline at end of file |
