Private
Public Access
1
0

xpc: Better type unpacking

This commit is contained in:
2025-08-23 20:01:13 -07:00
parent b7fabd6c05
commit 6f90e1c749
2 changed files with 58 additions and 22 deletions

View File

@@ -138,8 +138,14 @@ impl XpcDaemonInterface {
}
}
fn key(k: &str) -> CString { CString::new(k).unwrap() }
fn get_string<'a>(map: &'a HashMap<CString, Message>, key: &str) -> Option<&'a CStr> {
map.get(&CString::new(key).ok()?).and_then(|v| match v { Message::String(s) => Some(s.as_c_str()), _ => None })
map.get(&Self::key(key)).and_then(|v| match v { Message::String(s) => Some(s.as_c_str()), _ => None })
}
fn get_i64_from_str(map: &HashMap<CString, Message>, key: &str) -> Option<i64> {
Self::get_string(map, key).and_then(|s| s.to_string_lossy().parse().ok())
}
}
@@ -180,7 +186,7 @@ impl DaemonInterface for XpcDaemonInterface {
.await?;
// Expect an array under "conversations"
match reply.get(&CString::new("conversations").unwrap()) {
match reply.get(&Self::key("conversations")) {
Some(Message::Array(items)) => {
println!("Number of conversations: {}", items.len());
@@ -191,10 +197,10 @@ impl DaemonInterface for XpcDaemonInterface {
let display_name = Self::get_string(map, "display_name").map(|s| s.to_string_lossy().into_owned());
let last_preview = Self::get_string(map, "last_message_preview").map(|s| s.to_string_lossy().into_owned());
let unread_count = match map.get(&CString::new("unread_count").unwrap()) { Some(Message::String(v)) => v.to_string_lossy().parse().unwrap_or(0), _ => 0 };
let date_ts: i64 = match map.get(&CString::new("date").unwrap()) { Some(Message::String(v)) => v.to_string_lossy().parse().unwrap_or(0), _ => 0 };
let unread_count = Self::get_i64_from_str(map, "unread_count").unwrap_or(0) as i32;
let date_ts: i64 = Self::get_i64_from_str(map, "date").unwrap_or(0);
let participants: Vec<String> = match map.get(&CString::new("participants").unwrap()) {
let participants: Vec<String> = match map.get(&Self::key("participants")) {
Some(Message::Array(arr)) => arr.iter().filter_map(|m| match m { Message::String(s) => Some(s.to_string_lossy().into_owned()), _ => None }).collect(),
_ => Vec::new(),
};