From ad8efd60d5c12f20a2457056ef01bfba4a8b2a73 Mon Sep 17 00:00:00 2001 From: hut Date: Fri, 12 Apr 2024 23:13:55 +0200 Subject: [PATCH] an attempt at nicer code... --- src/chat.rs | 46 +++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index b0f2853..02b1bce 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -28,27 +28,35 @@ impl ChatDB { pub fn get_chat_by_id(&self, id: &String) -> Result { let mut found: Option = None; for (index, object_yaml) in self.0.iter().enumerate() { - if let Some(object_vec) = object_yaml.as_vec() { - if object_vec.len() == 0 { - continue; - } - let first_item = &object_vec[0]; - if let Some(hash) = first_item.as_hash() { - if let Some(chat_id_yaml) = hash.get(&Yaml::String(TOKEN_CHAT.to_string())) { - if let Some(chat_id) = chat_id_yaml.as_str() { - if chat_id != id { - continue; - } - if found.is_some() { - return Err("Found multiple chats with the same id!".to_string()); - } - found = Some(index as u32); - } - } - } - } else { + let obj_vec = object_yaml.as_vec(); + if obj_vec.is_none() { warn!("Non-list YAML object found while processing chat specs"); + continue; } + let obj_vec = obj_vec.unwrap(); + if obj_vec.len() == 0 { + continue; + } + let first_item = &obj_vec[0].as_hash(); + if first_item.is_none() { + continue; + } + let hash = first_item.unwrap(); + let chat_id = hash.get(&Yaml::String(TOKEN_CHAT.to_string())); + if chat_id.is_none() { + continue; + } + let chat_id = chat_id.unwrap().as_str(); + if chat_id.is_none() { + continue; + } + if chat_id.unwrap() != id { + continue; + } + if found.is_some() { + return Err("Found multiple chats with the same id!".to_string()); + } + found = Some(index as u32); } if let Some(result) = found { return Ok(result);