actual nice code

This commit is contained in:
yuni 2024-04-12 23:18:07 +02:00
parent ad8efd60d5
commit 14ef824b17

View file

@ -28,36 +28,15 @@ impl ChatDB {
pub fn get_chat_by_id(&self, id: &String) -> Result<u32, String> { pub fn get_chat_by_id(&self, id: &String) -> Result<u32, String> {
let mut found: Option<u32> = None; let mut found: Option<u32> = None;
for (index, object_yaml) in self.0.iter().enumerate() { for (index, object_yaml) in self.0.iter().enumerate() {
let obj_vec = object_yaml.as_vec(); if let Some(chat_id) = object_yaml[0][TOKEN_CHAT].as_str() {
if obj_vec.is_none() { if chat_id == id {
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() { if found.is_some() {
return Err("Found multiple chats with the same id!".to_string()); return Err("Found multiple chats with the same id!".to_string());
} }
found = Some(index as u32); found = Some(index as u32);
} }
}
}
if let Some(result) = found { if let Some(result) = found {
return Ok(result); return Ok(result);
} }