]> git.sondrewold.no Git - mitok.git/commitdiff
Setup read from file, tokenize on whitespace
authorSondre Wold <[email protected]>
Wed, 1 May 2024 14:39:46 +0000 (16:39 +0200)
committerSondre Wold <[email protected]>
Wed, 1 May 2024 14:39:46 +0000 (16:39 +0200)
mitok/Cargo.toml [new file with mode: 0644]
mitok/src/.lib.rs.swp [new file with mode: 0644]
mitok/src/.main.rs.swp [new file with mode: 0644]
mitok/src/lib.rs [new file with mode: 0644]
mitok/src/main.rs [new file with mode: 0644]

diff --git a/mitok/Cargo.toml b/mitok/Cargo.toml
new file mode 100644 (file)
index 0000000..a0c9a6e
--- /dev/null
@@ -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 (file)
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 (file)
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 (file)
index 0000000..b9a9b4d
--- /dev/null
@@ -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<Config, &'static str> {
+        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<dyn Error>> {
+    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<Vec<&'a str>> {
+    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 (file)
index 0000000..8b56d68
--- /dev/null
@@ -0,0 +1,14 @@
+use std::env;
+use std::process;
+use mitok::Config;
+
+
+fn main() {
+    let args: Vec<String> = 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);
+    };
+}