From: Sondre Wold Date: Wed, 1 May 2024 18:20:13 +0000 (+0200) Subject: Simple output format X-Git-Url: https://letsjmore.com/?a=commitdiff_plain;ds=inline;p=mitok.git Simple output format --- diff --git a/mitok/src/lib.rs b/mitok/src/lib.rs index da5e82b..138ff3f 100644 --- a/mitok/src/lib.rs +++ b/mitok/src/lib.rs @@ -1,26 +1,35 @@ 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 { - 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> { - 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(()) }