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,27 +28,35 @@ impl ChatDB {
pub fn get_chat_by_id(&self, id: &String) -> Result<u32, String> {
let mut found: Option<u32> = 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);