-
-
Save rust-play/60ca40a5af3fe9133886d5150692b106 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashMap; | |
fn main() { | |
let action = std::env::args().nth(1).expect("Please provide an action"); | |
let item = std::env::args().nth(2).expect("Please provide an item"); | |
let _getitem = std::env::args().nth(3).expect("Please provide 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), | |
}, | |
} | |
} else if action == "edit" { | |
match todo.edit(&item, &_getitem) { | |
None => println!("'{}' is not present in the list", item), | |
Some(_) => match todo.save() { | |
Ok(_) => println!("todo saved"), | |
Err(why) => println!("An error occurred: {}", why), | |
}, | |
} | |
} else if action == "remove" { | |
match todo.remove(&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), | |
}, | |
} | |
} | |
} | |
struct Todo { | |
map: HashMap<String, bool>, | |
} | |
impl Todo { | |
fn new() -> Result<Todo, std::io::Error> { | |
let f = std::fs::OpenOptions::new() | |
.write(true) | |
.create(true) | |
.read(true) | |
.open("db.json")?; | |
match serde_json::from_reader(f) { | |
Ok(map) => Ok(Todo { map }), | |
Err(e) if e.is_eof() => Ok(Todo { | |
map: HashMap::new(), | |
}), | |
Err(e) => panic!("An error occurred: {}", e), | |
} | |
} | |
fn insert(&mut self, key: String) { | |
// insert a new item into our map. | |
// active state is set to true by default. | |
self.map.insert(key, true); | |
} | |
fn save(self) -> Result<(), Box<dyn std::error::Error>> { | |
let f = std::fs::OpenOptions::new() | |
.write(true) | |
.create(true) | |
.truncate(true) | |
.open("db.json")?; | |
serde_json::to_writer_pretty(f, &self.map)?; | |
Ok(()) | |
} | |
fn complete(&mut self, key: &String) -> Option<()> { | |
match self.map.get_mut(key) { | |
Some(v) => Some(*v = !*v), | |
None => None, | |
} | |
} | |
fn remove(&mut self, key: &String) -> Option<bool> { | |
self.map.remove(key) | |
} | |
fn edit(&mut self, key: &String, value: String) -> Option<String> { | |
self.map.insert(key.to_string(), value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment