diff --git a/src/ingestions.rs b/src/ingestions.rs index c547a0a..308c806 100644 --- a/src/ingestions.rs +++ b/src/ingestions.rs @@ -2,7 +2,7 @@ use chrono::{NaiveDateTime, Utc}; use color_eyre::Section; use inquire; use serde::{self, Deserialize, Serialize}; -use std::collections::HashMap; +use std::{collections::HashMap, process::exit}; use strum::{EnumIter, IntoEnumIterator}; use uuid::Uuid; @@ -54,8 +54,15 @@ pub fn add_ingestion() { } else { std::fs::File::create(INGESTIONS_FILE.to_string()).unwrap(); ingesstions_bytes_loaded_des = HashMap::new(); + let ingesstions_bytes_loaded_ser = + bincode::serialize(&ingesstions_bytes_loaded_des).unwrap(); + std::fs::write(INGESTIONS_FILE.to_string(), ingesstions_bytes_loaded_ser).unwrap(); } let substances = crate::substances::substances_to_vec(); + if substances.is_empty() { + eprintln!("Add a substance before you log an ingestions"); + exit(1) + } let substance = inquire::Select::new("What did yout ingest?", substances) .prompt() .unwrap(); @@ -139,3 +146,9 @@ pub fn list_ingestions() -> Result<(), std::io::Error> { Ok(()) } + +pub fn create_ingestions_file() -> Result<(), std::io::Error> { + let hash: HashMap = HashMap::new(); + let hash_ser = bincode::serialize(&hash).unwrap(); + std::fs::write(INGESTIONS_FILE.to_string(), hash_ser) +} diff --git a/src/main.rs b/src/main.rs index 7394e9f..bfb7820 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ use std::{collections::HashMap, path::Path}; use bincode::serialize; use chrono::Utc; use clap::{Parser, Subcommand}; -use config::LOCAL_PATH; +use config::{INGESTIONS_FILE, LOCAL_PATH, SUBSTANCES_FILE}; use git2; use inquire; use serde::{self, Deserialize, Serialize}; @@ -64,6 +64,34 @@ fn main() { } } } + if !substances::path_exists(SUBSTANCES_FILE.to_string()) { + match substances::create_substances_file() { + Ok(_) => { + println!( + "Created substances file at {:?}", + SUBSTANCES_FILE.to_string() + ) + } + Err(_) => { + eprintln!("Could not create substances file"); + panic!() + } + }; + } + if !substances::path_exists(INGESTIONS_FILE.to_string()) { + match ingestions::create_ingestions_file() { + Ok(_) => { + println!( + "Created ingestions file at {:?}", + INGESTIONS_FILE.to_string() + ) + } + Err(_) => { + eprintln!("Could not create substances file"); + panic!() + } + }; + } let cli = Cli::parse(); match &cli.command { diff --git a/src/substances.rs b/src/substances.rs index 947ac68..9d39606 100644 --- a/src/substances.rs +++ b/src/substances.rs @@ -106,3 +106,9 @@ pub fn substances_to_vec() -> Vec { } sub_vec } + +pub fn create_substances_file() -> Result<(), std::io::Error> { + let hash: HashMap = HashMap::new(); + let hash_ser = bincode::serialize(&hash).unwrap(); + std::fs::write(SUBSTANCES_FILE.to_string(), hash_ser) +}