diff --git a/Cargo.lock b/Cargo.lock index 6cd956a..7f286b9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -18,6 +18,18 @@ dependencies = [ "winapi", ] +[[package]] +name = "arrayref" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544" + +[[package]] +name = "arrayvec" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cff77d8686867eceff3105329d4698d96c2391c176d5d03adc90c7389162b5b8" + [[package]] name = "atty" version = "0.2.14" @@ -35,17 +47,34 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" +[[package]] +name = "base64" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b41b7ea54a0c9d92199de89e20e58d49f02f8e699814ef3fdf266f6f748d15c7" + [[package]] name = "bitflags" version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +[[package]] +name = "blake2b_simd" +version = "0.5.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8fb2d74254a3a0b5cac33ac9f8ed0e44aa50378d9dbb2e5d83bd21ed1dc2c8a" +dependencies = [ + "arrayref", + "arrayvec", + "constant_time_eq", +] + [[package]] name = "cfg-if" -version = "0.1.10" +version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +checksum = "b486ce3ccf7ffd79fdeb678eac06a9e6c09fc88d33836340becb8fffe87c5e33" [[package]] name = "chrono" @@ -74,6 +103,44 @@ dependencies = [ "vec_map", ] +[[package]] +name = "constant_time_eq" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" + +[[package]] +name = "crossbeam-utils" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" +dependencies = [ + "autocfg", + "cfg-if", + "lazy_static", +] + +[[package]] +name = "dirs" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fddc3610d8f9552384e06ebc87f714e1d0b2b64a99194d2faf36d7ae5f48549" +dependencies = [ + "cfg-if", + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e93d7f5705de3e49895a2b5e0b8855a1c27f080192ae9c32a6432d50741a57a" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + [[package]] name = "env_logger" version = "0.7.1" @@ -239,6 +306,17 @@ version = "0.1.56" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84" +[[package]] +name = "redox_users" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09b23093265f8d200fa7b4c2c76297f47e681c655f6f1285a8780d6a022f7431" +dependencies = [ + "getrandom", + "redox_syscall", + "rust-argon2", +] + [[package]] name = "regex" version = "1.3.7" @@ -257,6 +335,18 @@ version = "0.6.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fe5bd57d1d7414c6b5ed48563a2c855d995ff777729dcd91c369ec7fea395ae" +[[package]] +name = "rust-argon2" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bc8af4bda8e1ff4932523b94d3dd20ee30a87232323eda55903ffd71d2fb017" +dependencies = [ + "base64", + "blake2b_simd", + "constant_time_eq", + "crossbeam-utils", +] + [[package]] name = "serde" version = "1.0.104" @@ -344,6 +434,7 @@ version = "0.1.0" dependencies = [ "chrono", "clap", + "dirs", "env_logger", "log", "serde", diff --git a/Cargo.toml b/Cargo.toml index e72eee8..d1b4f70 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ edition = "2018" [dependencies] chrono = { version = "0.4", features = ["serde"] } clap = "2.33" +dirs = "3.0" env_logger = "0.7" log = "*" serde = "*" diff --git a/src/eventlist.rs b/src/eventlist.rs index 80bab1a..9a955fb 100644 --- a/src/eventlist.rs +++ b/src/eventlist.rs @@ -16,9 +16,12 @@ along with this program. If not, see . */ +use std::ffi::OsString; use std::fs::File; use std::io::{Read, Write}; +use std::path::PathBuf; +use dirs::config_dir; use serde_derive::Deserialize; use serde_derive::Serialize; use toml; @@ -47,6 +50,7 @@ pub struct EventListIterator<'a> { pub enum EventListError { InvalidDate, TooOld, + NoStorage, } impl From for EventListError { @@ -65,15 +69,15 @@ impl EventList { } // TODO hide this - pub fn load() -> Self { - if let Ok(mut fp) = File::open(FILENAME) { + pub fn load() -> Result { + if let Ok(mut fp) = File::open(EventList::event_file()?) { let mut content = String::new(); fp.read_to_string(&mut content) .expect("Your event file is corrupted"); // TODO remove toml - toml::from_str(&content).unwrap_or(EventList::empty()) + Ok(toml::from_str(&content).unwrap_or(EventList::empty())) } else { - EventList::empty() + Ok(EventList::empty()) } } @@ -84,22 +88,23 @@ impl EventList { // TODO turn this into the destructor // TODO if so, track changes - pub fn save(&self) { + pub fn save(&self) -> Result<(), EventListError> { // TODO remove toml let content = toml::to_string(&self).unwrap(); - if let Ok(mut fp) = File::create(FILENAME) { + if let Ok(mut fp) = File::create(EventList::event_file()?) { fp.write_all(content.as_bytes()).unwrap(); } + Ok(()) } /// Load the event list, add an all day event, and save it back. /// Returns the ID of the new event. pub fn add_event_with_date(description: &str, date: &Date) -> Result { - let mut list = EventList::load(); + let mut list = EventList::load()?; let event = Event::new_on_date(description, date)?; let id = String::from(&event.id); list.push(event); - list.save(); + list.save()?; Ok(id) } @@ -109,13 +114,23 @@ impl EventList { description: &str, datetime: &DateTime, ) -> Result { - let mut list = EventList::load(); + let mut list = EventList::load()?; let event = Event::new_on_date_time(description, datetime)?; let id = String::from(&event.id); list.push(event); - list.save(); + list.save()?; Ok(id) } + + /// Full path for the event file. + fn event_file() -> Result { + let base = config_dir().ok_or(EventListError::NoStorage)?; + let mut path = PathBuf::new(); + path.push(base); + path.push(FILENAME); + + Ok(path.into_os_string()) + } } impl<'a> IntoIterator for &'a EventList { diff --git a/src/main.rs b/src/main.rs index 96a02d6..27a9f54 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,7 +52,9 @@ fn main() { } fn list() { - let event_list = EventList::load(); // TODO hide load from outside + // TODO hide load from outside + // TODO unwrap + let event_list = EventList::load().unwrap(); println!("{:^8} | {:^7} | {}", "ID", "ETA", "Description"); // TODO: EventList::iter() for event in event_list.into_iter() {