From e44a7a7c705eb6e5c3461520a8301a61f3a7b3b3 Mon Sep 17 00:00:00 2001 From: Sondre Wold Date: Wed, 1 May 2024 20:20:13 +0200 Subject: [PATCH] Simple output format --- mitok/src/lib.rs | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) 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(()) } -- 2.39.5