an attempt at nicer code...

This commit is contained in:
yuni 2024-04-12 23:13:55 +02:00
parent a572959df3
commit ad8efd60d5

View file

@ -28,15 +28,29 @@ 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() {
if let Some(object_vec) = object_yaml.as_vec() { let obj_vec = object_yaml.as_vec();
if object_vec.len() == 0 { if obj_vec.is_none() {
warn!("Non-list YAML object found while processing chat specs");
continue; continue;
} }
let first_item = &object_vec[0]; let obj_vec = obj_vec.unwrap();
if let Some(hash) = first_item.as_hash() { if obj_vec.len() == 0 {
if let Some(chat_id_yaml) = hash.get(&Yaml::String(TOKEN_CHAT.to_string())) { continue;
if let Some(chat_id) = chat_id_yaml.as_str() { }
if chat_id != id { 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; continue;
} }
if found.is_some() { if found.is_some() {
@ -44,12 +58,6 @@ impl ChatDB {
} }
found = Some(index as u32); found = Some(index as u32);
} }
}
}
} else {
warn!("Non-list YAML object found while processing chat specs");
}
}
if let Some(result) = found { if let Some(result) = found {
return Ok(result); return Ok(result);
} }