From 2010c906b150bd4f7e318a13cd02a6ba49897bc8 Mon Sep 17 00:00:00 2001 From: xqtc Date: Thu, 10 Oct 2024 22:21:51 +0200 Subject: [PATCH] Handle substance file not existing when ingestion is added --- src/substances.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/substances.rs b/src/substances.rs index 5d67f33..947ac68 100644 --- a/src/substances.rs +++ b/src/substances.rs @@ -87,7 +87,18 @@ pub fn list_substances() -> Result<(), std::io::Error> { } pub fn substances_to_vec() -> Vec { - let sub_read = std::fs::read(SUBSTANCES_FILE.to_string()).unwrap(); + let sub_read_res = std::fs::read(SUBSTANCES_FILE.to_string()); + let sub_read = match sub_read_res { + Ok(sub_contents) => sub_contents, + Err(_) => { + println!("Error! Substance file does not exist. Creating file..."); + let hash: HashMap = HashMap::new(); + let hash_ser = bincode::serialize(&hash).unwrap(); + std::fs::write(SUBSTANCES_FILE.to_string(), hash_ser).unwrap(); + let ret: Vec = vec![]; + ret + } + }; let sub_dec: HashMap = bincode::deserialize(&sub_read).unwrap(); let mut sub_vec: Vec = vec![]; for (id, substance) in sub_dec.clone().into_iter() {