diff options
| author | Denis Evsyukov <[email protected]> | 2020-03-08 16:09:09 +0300 |
|---|---|---|
| committer | Denis Evsyukov <[email protected]> | 2020-03-08 16:12:30 +0300 |
| commit | 31d2dd764d7d08a110c0d4b8699edd0da6e0448d (patch) | |
| tree | d282b65954016b7856e9b2a64824b38f8721bd9c | |
| parent | [+] read file (diff) | |
| download | t-31d2dd764d7d08a110c0d4b8699edd0da6e0448d.tar.xz t-31d2dd764d7d08a110c0d4b8699edd0da6e0448d.zip | |
[+] read_files
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | Cargo.lock | 23 | ||||
| -rw-r--r-- | Cargo.toml | 3 | ||||
| -rw-r--r-- | src/main.rs | 44 |
4 files changed, 46 insertions, 26 deletions
@@ -1 +1,3 @@ /target + +/Cargo.lock diff --git a/Cargo.lock b/Cargo.lock deleted file mode 100644 index 2d96995..0000000 --- a/Cargo.lock +++ /dev/null @@ -1,23 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -[[package]] -name = "getopts" -version = "0.2.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5" -dependencies = [ - "unicode-width", -] - -[[package]] -name = "t" -version = "0.1.0" -dependencies = [ - "getopts", -] - -[[package]] -name = "unicode-width" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479" @@ -7,4 +7,5 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -getopts = "0.2"
\ No newline at end of file +getopts = "0.2" +rust-crypto = "0.2"
\ No newline at end of file diff --git a/src/main.rs b/src/main.rs index f1ec884..6bdc1fb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,14 @@ extern crate getopts; +use crypto::digest::Digest; +use crypto::sha2::Sha256; use getopts::*; +use std::collections::HashMap; use std::env; use std::fs; +use std::path::Path; use std::path::PathBuf; +use std::process::exit; fn main() { let args: Vec<String> = env::args().collect(); let mut opts = Options::new(); @@ -53,12 +58,13 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; let mut taskpath = PathBuf::from(&taskdir); taskpath.push(&taskfile); + println!("taskdir: {}", taskpath.to_str().unwrap().to_string()); + let donefile = format!(".{}.done", taskfile); let mut donepath = PathBuf::from(&taskdir); donepath.push(donefile); - let contents = fs::read_to_string(taskpath).expect("Something went wrong reading the file"); - println!("{}", contents); + let (tasks, done) = read_files(taskpath, donepath); let input = if !matches.free.is_empty() { matches.free[0].clone() @@ -67,3 +73,37 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; }; println!("{}", input); } + +fn hash(str: String) -> String { + let mut hasher = Sha256::new(); + hasher.input_str(&str); + hasher.result_str() +} + +fn read_files( + taskpath: PathBuf, + donefile: PathBuf, +) -> (HashMap<String, String>, HashMap<String, String>) { + if !Path::new(&taskpath).exists() { + println!("File {} does not exist...", taskpath.to_str().unwrap()); + exit(1); + } + let contents = fs::read_to_string(taskpath).unwrap_or_else(|_| "".to_string()); + println!("{}", contents); + + let contents_done = fs::read_to_string(donefile).unwrap_or_else(|_| "".to_string()); + println!("{}", contents); + + let mut tasks: HashMap<String, String> = HashMap::new(); + let mut done: HashMap<String, String> = HashMap::new(); + + for line in contents.lines() { + tasks.insert(hash(line.to_string()), line.to_string()); + } + + for line in contents_done.lines() { + done.insert(hash(line.to_string()), line.to_string()); + } + + (tasks, done) +} |