use std::error::Error;
use std::fs;
+use std::io::Write;
+use std::io::prelude::*;
+use std::fs::File;
pub struct Config {
- file_path: String,
+ input_path: String,
+ output_path: String,
}
impl Config {
pub fn new(args: &[String]) -> Result<Config, &'static str> {
- if args.len() < 2 {
+ if args.len() < 3 {
return Err("Not enough provided arguments...");
}
- let file_path = args[1].clone();
- Ok(Config { file_path })
+ let input_path = args[1].clone();
+ let output_path = args[2].clone();
+ Ok(Config {
+ input_path,
+ output_path,
+ })
}
}
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
- let contents = fs::read_to_string(config.file_path)?;
+ let contents = fs::read_to_string(config.input_path)?;
let tokens = tokenize(&contents);
- println!("{contents}");
- for line in tokens {
- println!("{:?}", line);
+ let mut f = File::create(config.output_path).expect("Unable to create file");
+ for sentence in &tokens {
+ let line = sentence.join(", ");
+ writeln!(f, "{}", line)?;
}
Ok(())
}