cargo fmt
This commit is contained in:
@@ -14,8 +14,8 @@ use futures::{
|
||||
Stream,
|
||||
};
|
||||
|
||||
|
||||
const SERVICE_NAME: &str = "net.buzzert.kordophonecd\0";
|
||||
|
||||
const GET_VERSION_METHOD: &str = "GetVersion";
|
||||
const GET_CONVERSATIONS_METHOD: &str = "GetConversations";
|
||||
|
||||
@@ -32,12 +32,16 @@ impl XPCClient {
|
||||
pub fn connect(name: impl AsRef<CStr>) -> Self {
|
||||
use block::ConcreteBlock;
|
||||
use xpc_connection::xpc_object_to_message;
|
||||
use xpc_connection_sys::xpc_connection_set_event_handler;
|
||||
use xpc_connection_sys::xpc_connection_resume;
|
||||
use xpc_connection_sys::xpc_connection_set_event_handler;
|
||||
|
||||
let name = name.as_ref();
|
||||
let connection = unsafe {
|
||||
xpc_connection_sys::xpc_connection_create_mach_service(name.as_ptr(), std::ptr::null_mut(), 0)
|
||||
xpc_connection_sys::xpc_connection_create_mach_service(
|
||||
name.as_ptr(),
|
||||
std::ptr::null_mut(),
|
||||
0,
|
||||
)
|
||||
};
|
||||
|
||||
let (sender, receiver) = unbounded_channel();
|
||||
@@ -78,8 +82,8 @@ impl XPCClient {
|
||||
|
||||
impl Drop for XPCClient {
|
||||
fn drop(&mut self) {
|
||||
use xpc_connection_sys::xpc_release;
|
||||
use xpc_connection_sys::xpc_object_t;
|
||||
use xpc_connection_sys::xpc_release;
|
||||
|
||||
unsafe { xpc_release(self.connection as xpc_object_t) };
|
||||
}
|
||||
@@ -118,16 +122,30 @@ impl XpcDaemonInterface {
|
||||
Ok(CString::new(service_name)?)
|
||||
}
|
||||
|
||||
fn build_request(method: &str, args: Option<HashMap<CString, Message>>) -> HashMap<CString, Message> {
|
||||
fn build_request(
|
||||
method: &str,
|
||||
args: Option<HashMap<CString, Message>>,
|
||||
) -> HashMap<CString, Message> {
|
||||
let mut request = HashMap::new();
|
||||
request.insert(CString::new("method").unwrap(), Message::String(CString::new(method).unwrap()));
|
||||
request.insert(
|
||||
CString::new("method").unwrap(),
|
||||
Message::String(CString::new(method).unwrap()),
|
||||
);
|
||||
if let Some(arguments) = args {
|
||||
request.insert(CString::new("arguments").unwrap(), Message::Dictionary(arguments));
|
||||
request.insert(
|
||||
CString::new("arguments").unwrap(),
|
||||
Message::Dictionary(arguments),
|
||||
);
|
||||
}
|
||||
request
|
||||
}
|
||||
|
||||
async fn call_method(&self, client: &mut XPCClient, method: &str, args: Option<HashMap<CString, Message>>) -> anyhow::Result<HashMap<CString, Message>> {
|
||||
async fn call_method(
|
||||
&self,
|
||||
client: &mut XPCClient,
|
||||
method: &str,
|
||||
args: Option<HashMap<CString, Message>>,
|
||||
) -> anyhow::Result<HashMap<CString, Message>> {
|
||||
let request = Self::build_request(method, args);
|
||||
client.send_message(Message::Dictionary(request));
|
||||
|
||||
@@ -138,10 +156,15 @@ impl XpcDaemonInterface {
|
||||
}
|
||||
}
|
||||
|
||||
fn key(k: &str) -> CString { CString::new(k).unwrap() }
|
||||
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(&Self::key(key)).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> {
|
||||
@@ -157,7 +180,9 @@ impl DaemonInterface for XpcDaemonInterface {
|
||||
let mut client = XPCClient::connect(&mach_port_name);
|
||||
|
||||
// Call generic method and parse reply
|
||||
let map = self.call_method(&mut client, GET_VERSION_METHOD, None).await?;
|
||||
let map = self
|
||||
.call_method(&mut client, GET_VERSION_METHOD, None)
|
||||
.await?;
|
||||
if let Some(ver) = Self::get_string(&map, "version") {
|
||||
println!("Server version: {}", ver.to_string_lossy());
|
||||
Ok(())
|
||||
@@ -165,7 +190,9 @@ impl DaemonInterface for XpcDaemonInterface {
|
||||
println!("XPC replied with type: {}", ty.to_string_lossy());
|
||||
Ok(())
|
||||
} else {
|
||||
Err(anyhow::anyhow!("Unexpected XPC reply payload for GetVersion"))
|
||||
Err(anyhow::anyhow!(
|
||||
"Unexpected XPC reply payload for GetVersion"
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,8 +204,14 @@ impl DaemonInterface for XpcDaemonInterface {
|
||||
|
||||
// Build arguments: limit=100, offset=0 (string-encoded for portability)
|
||||
let mut args = HashMap::new();
|
||||
args.insert(CString::new("limit").unwrap(), Message::String(CString::new("100").unwrap()));
|
||||
args.insert(CString::new("offset").unwrap(), Message::String(CString::new("0").unwrap()));
|
||||
args.insert(
|
||||
CString::new("limit").unwrap(),
|
||||
Message::String(CString::new("100").unwrap()),
|
||||
);
|
||||
args.insert(
|
||||
CString::new("offset").unwrap(),
|
||||
Message::String(CString::new("0").unwrap()),
|
||||
);
|
||||
|
||||
// Call
|
||||
let reply = self
|
||||
@@ -193,15 +226,26 @@ impl DaemonInterface for XpcDaemonInterface {
|
||||
for item in items {
|
||||
if let Message::Dictionary(map) = item {
|
||||
// Convert to PrintableConversation
|
||||
let guid = Self::get_string(map, "guid").map(|s| s.to_string_lossy().into_owned()).unwrap_or_default();
|
||||
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 guid = Self::get_string(map, "guid")
|
||||
.map(|s| s.to_string_lossy().into_owned())
|
||||
.unwrap_or_default();
|
||||
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 = Self::get_i64_from_str(map, "unread_count").unwrap_or(0) as i32;
|
||||
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(&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(),
|
||||
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(),
|
||||
};
|
||||
|
||||
@@ -211,7 +255,8 @@ impl DaemonInterface for XpcDaemonInterface {
|
||||
display_name,
|
||||
last_message_preview: last_preview,
|
||||
unread_count,
|
||||
date: time::OffsetDateTime::from_unix_timestamp(date_ts).unwrap_or_else(|_| time::OffsetDateTime::UNIX_EPOCH),
|
||||
date: time::OffsetDateTime::from_unix_timestamp(date_ts)
|
||||
.unwrap_or_else(|_| time::OffsetDateTime::UNIX_EPOCH),
|
||||
participants,
|
||||
};
|
||||
|
||||
@@ -220,7 +265,10 @@ impl DaemonInterface for XpcDaemonInterface {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Some(other) => Err(anyhow::anyhow!("Unexpected conversations payload: {:?}", other)),
|
||||
Some(other) => Err(anyhow::anyhow!(
|
||||
"Unexpected conversations payload: {:?}",
|
||||
other
|
||||
)),
|
||||
None => Err(anyhow::anyhow!("Missing conversations in reply")),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user