diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/config.rs | 48 | ||||
| -rw-r--r-- | src/main.rs | 25 |
2 files changed, 73 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 diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..d4de3cc --- /dev/null +++ b/src/main.rs @@ -0,0 +1,25 @@ +#![feature(fmt_internals)] + +use std::{env, io}; +use std::fs::File; +use std::io::{BufReader, Write}; + +mod config; + +fn main() { + let args: Vec<String> = env::args().collect(); + if args.len() != 2 { + ::std::process::exit(-1); + } + let file = File::open(&args[1]); + if file.is_err() { + ::std::process::exit(-2); + } + let reader = BufReader::new(file.unwrap()); + let config = config::create_config(reader); + + match io::stdout().write(format!("{:?}\n", config).as_bytes()){ + Ok(_) => {} + Err(_) => std::process::exit(-3) + } +} |
