From 14ef824b1734c34ead6a3df520880901e521d5c0 Mon Sep 17 00:00:00 2001 From: hut Date: Fri, 12 Apr 2024 23:18:07 +0200 Subject: [PATCH] actual nice code --- src/chat.rs | 35 +++++++---------------------------- 1 file changed, 7 insertions(+), 28 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index 02b1bce..a8f0c9e 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -28,35 +28,14 @@ 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() { - let obj_vec = object_yaml.as_vec(); - if obj_vec.is_none() { - warn!("Non-list YAML object found while processing chat specs"); - continue; + if let Some(chat_id) = object_yaml[0][TOKEN_CHAT].as_str() { + if chat_id == id { + if found.is_some() { + return Err("Found multiple chats with the same id!".to_string()); + } + found = Some(index as u32); + } } - 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);