From d6874a6775ae3ab47c992f611c5131a8b3853bfb Mon Sep 17 00:00:00 2001 From: Daiki48 Date: Fri, 26 Aug 2022 16:30:28 +0900 Subject: [PATCH] first commit --- .gitignore | 1 + Cargo.lock | 39 +++++++++++++++++++++++++++++ Cargo.toml | 9 +++++++ README.md | 19 ++++++++++++++ data/db.json | 5 ++++ src/main.rs | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 144 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 README.md create mode 100644 data/db.json create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..6aa8701 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,39 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "itoa" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754" + +[[package]] +name = "ryu" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" + +[[package]] +name = "serde" +version = "1.0.144" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f747710de3dcd43b88c9168773254e809d8ddbdf9653b84e2554ab219f17860" + +[[package]] +name = "serde_json" +version = "1.0.85" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e55a28e3aaef9d5ce0506d0a14dbba8054ddc7e499ef522dd8b26859ec9d4a44" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "todoapp_cli" +version = "0.1.0" +dependencies = [ + "serde_json", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..9f3dd8d --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "todoapp_cli" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +serde_json = "1.0.60" diff --git a/README.md b/README.md new file mode 100644 index 0000000..c28a95b --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# This is Todo App with Rust lang. + +CLI's Todo App. + +## Usage + +### For Example + +Input command + +``` +$ cargo run -- add "todo test1" +``` + +confirmation + +``` +$ cat ./data/db.json +``` diff --git a/data/db.json b/data/db.json new file mode 100644 index 0000000..91ad1f3 --- /dev/null +++ b/data/db.json @@ -0,0 +1,5 @@ +{ + "second test": true, + "code test": true, + "first test": false +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..0b10691 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,71 @@ +use std::collections::HashMap; + +struct Todo { + map: HashMap, +} + +impl Todo { + fn insert(&mut self, key: String) { + // insert a new item into our map. + // we pass true as value + self.map.insert(key, true); + } + + fn save(self) -> Result<(), Box> { + let content = std::fs::OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open("./data/db.json")?; + serde_json::to_writer_pretty(content, &self.map)?; + Ok(()) + } + + fn new() -> Result { + let list = std::fs::OpenOptions::new() + .write(true) + .create(true) + .read(true) + .open("./data/db.json")?; + + match serde_json::from_reader(list) { + Ok(map) => Ok(Todo {map}), + Err(e) if e.is_eof() => Ok(Todo { + map: HashMap::new(), + }), + Err(e) => panic!("An error occurred: {}", e), + } + } + + fn complete(&mut self, key: &String) -> Option<()> { + match self.map.get_mut(key) { + Some(v) => Some(*v = false), + None => None, + } + } + + +} + +fn main() { + let action = std::env::args().nth(1).expect("Please specify an action"); + let item = std::env::args().nth(2).expect("Please specify an item"); + + let mut todo = Todo::new().expect("Initialisation of db failed"); + + if action == "add" { + todo.insert(item); + match todo.save() { + Ok(_) => println!("todo saved"), + Err(why) => println!("An error occurred: {}", why), + } + } else if action == "complete" { + match todo.complete(&item) { + None => println!("'{}' is not present in the list", item), + Some(_) => match todo.save() { + Ok(_) => println!("todo saved"), + Err(why) => println!("An error occurred: {}", why), + }, + } + } +}