From 8cf7e47b76431cd5f142e631f6a93b608ffdc694 Mon Sep 17 00:00:00 2001 From: Sondre Wold Date: Wed, 1 May 2024 16:39:46 +0200 Subject: [PATCH] Setup read from file, tokenize on whitespace --- mitok/Cargo.toml | 8 +++++ mitok/src/.lib.rs.swp | Bin 0 -> 12288 bytes mitok/src/.main.rs.swp | Bin 0 -> 12288 bytes mitok/src/lib.rs | 68 +++++++++++++++++++++++++++++++++++++++++ mitok/src/main.rs | 14 +++++++++ 5 files changed, 90 insertions(+) create mode 100644 mitok/Cargo.toml create mode 100644 mitok/src/.lib.rs.swp create mode 100644 mitok/src/.main.rs.swp create mode 100644 mitok/src/lib.rs create mode 100644 mitok/src/main.rs 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 0000000000000000000000000000000000000000..803a2e031c7364cba2dae064e7d885fb8b43b2f8 GIT binary patch literal 12288 zcmeI2L5Lew7{}l0L9JHB9u$i3HVeBG$8{DHbuua5#Qok)Xhz_^l2Ly?|m0d@wlN-q?zQUps$+;xTA6T=>`2CRYo8aPOv zt*@9%eXcS~4?S^azkzH&Yrqa`+)6kKg}C|Nnpg7$M() zufeC_D)<1r58ePLK^6S*C?P+CFTpkN9(WhL1I~cM;ID&(`~bcJAA=9U+u$wmCRhcF z-~hP(2;zV(AV3c=5Q4MdC2#~h2WsGc@Y}qpMk63 zELaCF_!D#g99#oC;51kTbKq&9-TeL_AzuKySp(L9HDC=`1J=M@Gq6J_w)|D^`BE~W zI_z#8e|6SLSfn~K4q1(gxYsxPh`+k8_F!bwt0&g4vCVOb3-3t;C$Oewu_#=uaxu zuSv|$<8!+_Cin!j*&NiL02R%a* znleEdrzRp*2uIIxiiGJTTR5Uco>OTS#xO|b9MK^oP6t_{;ttxg&SpPG#GxNBtnXl$ zFsDklO}n`bJyhH5?^$2=|1ae9TKxyz?!J8A+O=D`1EDhJy@cJVQKK}IjLK}tgy%ie zAcHhCD=Q&?ZX<|#j+S3-Qa|Q9?8>%*_@Y3v|wLHbLB9Jm9sz>s&0@b2_8SzHLC}UQem2)D(UDt zb#)&(E-8Iw>ml;!Q*}Sgy1F*aIb7_`1;6Et;&^m^!KcWhN*7xuDU(=!tkr_F!qe#s zj#JGAOlo#0Vy+TC>r}SAV>`Gebyl~?x=1T8|5B$>7CV)qlb*+T4x@zk+?{l*QCG}0 zlAb4+AHr=1GeO1Z=|yqRjbhZ!&{zHHu?8tLh>RDfJ^H+9W~TlMlel)9PPImlr(4bN zHBL_ok&3c|$;4qJ?@PDoco4c%DiEYNIdPQwq9-?2+HR1f9Cw0AsyRJKOru)T_8KEe@Cao8@nqtoS2BG**$K}UxC9nbfp z3t=)9#9AfkAmrcj>}k=l@EJ>4K4{iMIUn<8A(Wg1ue)Yo!zfb|(;auT>Bym#!C6Nd zb5^ALUVoS1Z5i*koJrN-J;g|MB80yXgi)-AL)tSR&_WW${YLq~FOIRy%K5ryAKS>M Ptm_L=a4gJFWP{xSSG|>g literal 0 HcmV?d00001 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); + }; +} -- 2.39.5