From: Sondre Wold Date: Wed, 1 May 2024 14:39:46 +0000 (+0200) Subject: Setup read from file, tokenize on whitespace X-Git-Url: https://letsjmore.com/?a=commitdiff_plain;h=8cf7e47b76431cd5f142e631f6a93b608ffdc694;p=mitok.git Setup read from file, tokenize on whitespace --- diff --git a/mitok/Cargo.toml b/mitok/Cargo.toml new file mode 100644 index 0000000..a0c9a6e --- /dev/null +++ b/mitok/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "mitok" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/mitok/src/.lib.rs.swp b/mitok/src/.lib.rs.swp new file mode 100644 index 0000000..803a2e0 Binary files /dev/null and b/mitok/src/.lib.rs.swp differ diff --git a/mitok/src/.main.rs.swp b/mitok/src/.main.rs.swp new file mode 100644 index 0000000..441de99 Binary files /dev/null and b/mitok/src/.main.rs.swp differ diff --git a/mitok/src/lib.rs b/mitok/src/lib.rs new file mode 100644 index 0000000..b9a9b4d --- /dev/null +++ b/mitok/src/lib.rs @@ -0,0 +1,68 @@ +use std::error::Error; +use std::fs; + +pub struct Config { + file_path: String, +} + +impl Config { + pub fn new(args: &[String]) -> Result { + if args.len() < 2 { + return Err("Not enough provided arguments..."); + } + let file_path = args[1].clone(); + Ok(Config { file_path }) + } +} + +pub fn run(config: Config) -> Result<(), Box> { + let contents = fs::read_to_string(config.file_path)?; + let tokens = tokenize(&contents); + println!("{contents}"); + for line in tokens { + println!("{:?}", line); + } + Ok(()) +} + +pub fn tokenize<'a>(contents: &'a str) -> Vec> { + let mut tokens = Vec::new(); + for line in contents.lines() { + let mut line_tokens: Vec<&str> = line.split(' ').collect(); + line_tokens.retain(|t| t.len() > 0); + tokens.push(line_tokens) + } + tokens +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn single_line_whitespace_simple() { + let contents = "\ + dette er en banal test."; + + let mut left_side = Vec::new(); + let c = vec!["dette", "er", "en", "banal", "test."]; + left_side.push(c); + let right_side = tokenize(&contents); + + assert_eq!(left_side, right_side); + } + #[test] + fn multi_line_whitespace_simple() { + let contents = "\ + dette er en banal test \n som inneholder to linjer"; + + let mut left_side = Vec::new(); + let c1 = vec!["dette", "er", "en", "banal", "test"]; + let c2 = vec!["som", "inneholder", "to", "linjer"]; + left_side.push(c1); + left_side.push(c2); + let right_side = tokenize(&contents); + + assert_eq!(left_side, right_side); + } +} diff --git a/mitok/src/main.rs b/mitok/src/main.rs new file mode 100644 index 0000000..8b56d68 --- /dev/null +++ b/mitok/src/main.rs @@ -0,0 +1,14 @@ +use std::env; +use std::process; +use mitok::Config; + + +fn main() { + let args: Vec = env::args().collect(); + let config = Config::new(&args).unwrap_or_else(|_err| { + process::exit(1); + }); + if let Err(_e) = mitok::run(config) { + process::exit(1); + }; +}